Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Vanadium
Jan 8, 2005

Plorkyeran posted:

It's not very hard to write a generic RAII container for c api things:
C++ code:
template<class T, class Del = void(*)(T)>
class scoped_holder {
	T value;
	Del destructor;

	scoped_holder(scoped_holder const&);
	scoped_holder& operator=(scoped_holder const&);
public:
	operator T() const { return value; }
	T operator->() const { return value; }

	scoped_holder(T value, Del destructor)
	: value(value)
	, destructor(destructor)
	{
	}

	~scoped_holder() { if (value) destructor(value); }
};

...

{
  scoped_holder<api_handle_type> handle(api_alloc_a_thing(), api_delete_a_thing);
  if (!handle)
    return;

  api_do_stuff_with_a_thing(handle);
}

I never get the type conversion operators right :saddowns:

std::unique_ptr seems fairly reasonable for most such things, though. I'd be happier if I could squeeze the api_delete_a_thing in there at the type level so the call would be, like, inlined and everything, but that's probably just an imaginary concern.

Adbot
ADBOT LOVES YOU

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
That was meant to be C code. Unfortunately, there's no syntax highlighting for plain C. And yes, I know about the multiple labels trick, but I chose to keep it simple on purpose. And at least people aren't linking Djikstra on my rear end and calling me human scum this time.

Johnny Cache Hit
Oct 17, 2011

Suspicious Dish posted:

I'm curious how people feel about the goto statement as an approach to this.

I'm pretty sure this is the technique Rochkind uses in Advanced UNIX Programming. I think he wraps a macro or two around it, though...

It really is the only decent approach I've ever seen.

raminasi
Jan 25, 2005

a last drink with no ice

Lumpy posted:

So I had to poke around in a homegrown CMS system that a client's old web dev shop wrote ~6 years ago. It is full of horrors, but the README file takes the cake:

code:
THIS IS OUT OF DATE: UPDATE WITH INFO ABOUT HOW THIS WORKS!

This directory holds files that will eventually become actual documents on
a website: the editor scripts, etc.
:suicide:

That README is still leagues better than a straight-up incorrect one.

tef
May 30, 2004

-> some l-system crap ->

Suspicious Dish posted:

I'm curious how people feel about the goto statement as an approach to this.

C++ code:
  request = asprintf ("HTTP 1.1\nGET %s", url->path);
Yes, I know, it's an "HTTP client".

:argh: HTTP/1.1 sends \r\n as line separators, and has a Host: header :argh:

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Since you won't take "it was dumb example code" for an answer, how about "it was wrong on purpose so that a copy/paste programmer wouldn't have any chance of making it work, promoting proper software engineering practices instead".

Yeah, that's it.

pseudorandom name
May 6, 2007

tef posted:

:argh: HTTP/1.1 sends \r\n as line separators, and has a Host: header :argh:

If it makes you feel any better, it won't get that far because the check after the call to my_socket_open() is inverted.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

No Safe Word posted:

Yeah though I mean 99% of Stack Overflow is a horror anyway, those tags just ensure the deck is fully stacked.

At least those of us that hang out in the PHP chat room are competent. We've formed a little mob of sanity enforcers and copy/paste "DON'T DO THAT" commenters to help spread best practice.

It's sad that such things are necessary.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

Suspicious Dish posted:

And at least people aren't linking Djikstra on my rear end and calling me human scum this time.

The core Linux kernel developers are extremely competent C programmers, and their views on this subject are pretty informative.

geonetix
Mar 6, 2011


Lysidas posted:

The core Linux kernel developers are extremely competent C programmers, and their views on this subject are pretty informative.

I saw this excerpt in that conversation and felt it needed some attention:

quote:

> I thought Edsger Dijkstra coined the "gotos are evil" bit in his
> structured programming push?

Yeah, he did, but he's dead, and we shouldn't talk ill of the dead. So
these days I can only rant about Niklaus Wirth,

Oh Linus, never stop being a horrible dick.

csammis
Aug 26, 2003

Mental Institution

geonetix posted:

I saw this excerpt in that conversation and felt it needed some attention:

Oh Linus, never stop being a horrible dick.



My new favorite image

ohgodwhat
Aug 6, 2005

"Hey guys, I'm not very good at C, but you're doing it all wrong, could you change it to be more like what I learned about in my introductory programming class?"

These people :psyduck:

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
basically gently caress people who talk about something despite not knowing anything about it beyond the name of a famous paper

Zamujasa
Oct 27, 2010



Bread Liar
Haha. Not a coding horror in itself, but I just realized that the UPS we have here at work is apparently powering 4 outlet strips, three of which make up a huge daisy-chain. It's really starting to reflect the way coding is done at this place.

Basically:
code:
UPS
  |
  +-Power strip 1
  |  |-3 unknown items
  |  `-Power strip 3
  |     |-2 unknown items
  |     `-Power strip 4
  |        `-Several screens and two computers
  |
  +-Power strip 2
  |  `-6 unknown items
  |
  `-A cord that goes behind some shelves
Somehow I do not think this UPS will be very useful in the event of a power outage.


E: Oh, they managed to plug the microwave into the UPS at one point a few months ago too. That was all sorts of fun!

Zamujasa fucked around with this message at 23:22 on Jun 20, 2012

Safe and Secure!
Jun 14, 2008

OFFICIAL SA THREAD RUINER
SPRING 2013
That was actually really neat. I had just accepted that gotos are bad, but after reading that, I've changed my mind. The arguments against goto are pretty similar to the arguments I've seen against including pass-by-reference and operator-overloading in Java.

Safe and Secure! fucked around with this message at 23:36 on Jun 20, 2012

PrBacterio
Jul 19, 2000

Safe and Secure! posted:

That was actually really neat. I had just accepted that gotos are bad, but after reading that, I've changed my mind. The arguments against goto are pretty similar to the arguments I've seen against including pass-by-reference and operator-overloading in Java.
Please don't do that. Yes, goto's aren't in and of themselves evil, and sometimes it's better to use a well-placed goto in place of some more complicated circumscription where it makes for cleaner and more readable code, but nobody should take that as a license to pepper their code with gotos all over the place. In practice, using a goto statement is a bad idea *most of the time*, which is why the rule exists and people are usually taught never to use them, because that is *generally speaking* correct. If you come across one of the exceptions to this rule, where your code would be cleaner when using a goto, you'll know and should probably just go ahead and do it anyway. But knowing when it is OK to break such a rule, which is so commonly taught for a very good reason, first requires you to understand why it is in place in the first place and thus, adhere to it in most cases anyway.

N.Z.'s Champion
Jun 8, 2003

Yam Slacker
code:
.nowrap { display: inline-block; }

No Safe Word
Feb 26, 2005

PrBacterio posted:

Please don't do that. Yes, goto's aren't in and of themselves evil, and sometimes it's better to use a well-placed goto in place of some more complicated circumscription where it makes for cleaner and more readable code, but nobody should take that as a license to pepper their code with gotos all over the place. In practice, using a goto statement is a bad idea *most of the time*, which is why the rule exists and people are usually taught never to use them, because that is *generally speaking* correct. If you come across one of the exceptions to this rule, where your code would be cleaner when using a goto, you'll know and should probably just go ahead and do it anyway. But knowing when it is OK to break such a rule, which is so commonly taught for a very good reason, first requires you to understand why it is in place in the first place and thus, adhere to it in most cases anyway.

Pretty much. Really when it's appropriate to use a goto you should be at the point of already asking yourself, "Really? Really?? Is this one of those 0.01% of cases? Well goddamn..."

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

LOOK I AM A TURTLE posted:

God drat do I hate "single point of exit" as a design philosophy. It's the cause of conditional blocks the size of skyscrapers that are impossible to parse when all you need is if (gently caress == null) return false;

Granted, this only happens if the code is also full of massive functions. It's probably okay to use single point of exit if you manage to keep functions small, and it's probably (somewhat) okay to make a lot of long functions if you can make them readable, but combine single point of exit with behemoth functions and you have planted the seeds of despair.

But of course when the functions are small single-point-of-exit is pointless because you can easily scan the whole function for returns.

I have no use for single-point-of-exit as a design philosophy. If I'm going to use it for clean-up or whatever it's because that's the best way to write that particular function not because it's a good approach to programming in general.

Dicky B
Mar 23, 2004

"Blanket rules" in programming are the worst thing.

teamdest
Jul 1, 2007

Dicky B posted:

"Blanket rules" in programming are the worst thing.

You have a funny way of spelling "PHP".

Zhentar
Sep 28, 2003

Brilliant Master Genius

Dicky B posted:

"Blanket rules" in programming are the worst thing.

Spoken like someone who hasn't had a co-worker try to sneak a for loop implemented with goto through a code review.

Doctor w-rw-rw-
Jun 24, 2008

teamdest posted:

You have a funny way of spelling "PHP".

This joke is old and tired. Please stop.

PHP doesn't cause bad code (though it certainly helps), people do. PHP is a very practical and productive tool, and in the hands of a capable programmer, can avoid a lot of badness. I dislike almost everything about PHP, but since I have the privilege of working amongst good programmers, the PHP code I see at work is not crap. Good PHP is still going to be better than bad Python or bad Scala or what-have-you.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Zhentar posted:

Spoken like someone who hasn't had a co-worker try to sneak a for loop implemented with goto through a code review.

Idiot coworkers aren't a reason to abandon useful tools wholesale.

xf86enodev
Mar 27, 2010

dis catte!

Suspicious Dish posted:

I'm curious how people feel about the goto statement as an approach to this.

C++ code:
char *
grab_http_response(char *string_url)
{
  char *response = NULL;
  URL *url = NULL;
  my_socket *sock = NULL;
  char *request;

  url = parse_url (string_url, url);
  if (url == NULL)
    goto out;

  sock = my_socket_open (url->host, url->port);
  if (sock != NULL)
    goto out;

  request = asprintf ("HTTP 1.1\nGET %s", url->path);
  my_socket_send (sock, request);

  response = my_socket_recvall (sock);

 out:
  if (sock != NULL)
    my_socket_close (sock);
  if (url != NULL)
    free (url);

  return response;
}
Yes, I know, it's an "HTTP client". The code is probably incorrect, too. It was the simplest example that I think everybody would understand that required multiple resources to be closed in an error path.

If this were C I'd completely agree with the goto usage. OTOH, I don't know C++ that well, but wouldn't it be possible to wrap the response in an object and have the clean-up done be its destructor? ~*patterns*~

In Python you'd do it something similar. You'd have with response(butt) as poop: where response has its clean-up defined somewhat like a finally iirc.

edit: I guess what I'm getting at is why wouldn't you use exceptions if this is C++?

edit2: vvv

Suspicious Dish posted:

It only says "C++ code" because there's no C highlighter.
gently caress C++ and gently caress radium for his inferior syntax highlighting

xf86enodev fucked around with this message at 03:59 on Jun 21, 2012

shrughes
Oct 11, 2008

(call/cc call/cc)

quote:

I guess what I'm getting at is why wouldn't you use exceptions if this is C++?

Because they're bad and dangerous and unclear.

Other reasons may include legacy systems or legacy compilers that don't support them.

A good reason is because your coworkers don't know how to write exception-safe code. Probably you don't either, assuming you're a randomly distributed C++ developer.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

xf86enodev posted:

If this were C

It is. It only says "C++ code" because there's no C highlighter.

Vanadium
Jan 8, 2005

Suspicious Dish posted:

It is. It only says "C++ code" because there's no C highlighter.

We should get Radium to make it say C/C++ and drive all the C nerds and the C++ nerds into a rage.

xf86enodev
Mar 27, 2010

dis catte!

Vanadium posted:

We should get Radium to make it say C/C++ and drive all the C nerds and the C++ nerds into a rage.

Posting C/C++ code should just crash your browser.
Posting Java would be fine though. But it will crash browsers trying to look at the post unless they have signed up to Oracle's Open Post Streaming Intellisense (Enterprise Edition).

LOOK I AM A TURTLE
May 22, 2003

"I'm actually a tortoise."
Grimey Drawer

xf86enodev posted:

Posting C/C++ code should just crash your browser.
Posting Java would be fine though. But it will crash browsers trying to look at the post unless they have signed up to Oracle's Open Post Streaming Intellisense (Enterprise Edition).

In the future, probations will be automatically handed out for posts that don't follow PEP8.

Zhentar
Sep 28, 2003

Brilliant Master Genius

Suspicious Dish posted:

I'm curious how people feel about the goto statement as an approach to this.

And what's wrong with doing this instead?

C++ code:
char *
grab_http_response(char *string_url)
{
  char *response = NULL;
  URL *url = NULL;
  my_socket *sock = NULL;
  char *request;

  url = parse_url (string_url, url);
  if (url != NULL) {    
  
    sock = my_socket_open (url->host, url->port);
    if (sock != NULL) {

      request = asprintf ("HTTP 1.1\nGET %s", url->path);
      my_socket_send (sock, request);

      response = my_socket_recvall (sock);

      my_socket_close (sock);
    }
    free (url);
  }
  return response;
}
I keep hearing smart people insist that goto is a useful tool, but they make it hard to believe them when practically every example I ever see is either a workaround for a language shortcoming or an allergy to nested ifs.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Because code that looks like

code:
///////////
  ///////////
    ///////////
      ///////////
        ///////////
          ///////////
            ///////////
            ///////////
          ///////////
        ///////////
      ///////////
    ///////////
  ///////////
///////////
Is actually pretty horrible. Two levels is fine, three or four is pushing it, once you get to 5 it's horrendous unless you want to waste an entire monitor for a single bit of code.

But that said, yes, goto is often a workaround for "missing" language features. Structured programming is important. When your programming language doesn't support the ideal structure for this situation, emulating the proper structure with gotos is often far better than trying to hack something up with the control-flow structures it does support, which will end up even worse.

As a quick example, nesting ifs like this for cleanup purposes is a lovely hack to compensate for the lack of deterministic destructors. Faking it with gotos is still a hack, but it's far less lovely than doing it with if statements.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Gotos and returns keep your code from looking like >

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I noticed that I never free(request); in my code sample, so I'm actually leaking memory. Oh well.

The Gripper
Sep 14, 2004
i am winner

Zhentar posted:

And what's wrong with doing this instead?

C++ code:
snip
I keep hearing smart people insist that goto is a useful tool, but they make it hard to believe them when practically every example I ever see is either a workaround for a language shortcoming or an allergy to nested ifs.
I prefer that over goto any day, plus it adds the convenience of being able to fold code blocks properly and not have to gently caress around following gotos to skim program flow.

Maybe goto is a useful tool but if your only reason for using it is because you hate nesting, I hate you.

Edit;

redleader posted:

Today I saw some of the html output from our platform:
Haha it's like one of those chain emails with "SCROLL DOWN --------> boo" in it.

The Gripper fucked around with this message at 09:39 on Jun 21, 2012

redleader
Aug 18, 2005

Engage according to operational parameters
Today I saw some of the html output from our platform:

code:
<script>
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
foo();
//
</script>
I actually laughed at this.

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.

Doctor w-rw-rw- posted:

This joke is old and tired. Please stop.

PHP doesn't cause bad code (though it certainly helps), people do. PHP is a very practical and productive tool, and in the hands of a capable programmer, can avoid a lot of badness. I dislike almost everything about PHP, but since I have the privilege of working amongst good programmers, the PHP code I see at work is not crap. Good PHP is still going to be better than bad Python or bad Scala or what-have-you.

I'm of the personal opinion that if you can't write clean, clear, and consistent code in PHP, the language is not the problem.

Sure, it's missing a few features that you'd expect in, say, Java or python, or whatever else, but it works and it doesn't force you to write your code in one particular way.

KaneTW
Dec 2, 2011

bobthecheese posted:

Sure, it's missing a few features that you'd expect in, say, Java or python, or whatever else, but it works and it doesn't force you to write your code in one particular way.

I don't quite think features are the problem in PHP.

Horrors. I used to work on a HL1 based mod and most weapons were created by copy-pasting and changing a few values. Hmm. Let's run simian against the server-side codebase:

code:
Found 22067 duplicate lines in 2005 blocks in 147 files
Processed a total of 105422 significant (206434 raw) lines in 249 files
:stare:

Compare that with regular HL1 MP SDK:
code:
Found 1395 duplicate lines in 181 blocks in 37 files
Processed a total of 25789 significant (51060 raw) lines in 94 files
E: And here the singleplayer SDK:
code:
Found 4779 duplicate lines in 405 blocks in 74 files
Processed a total of 48011 significant (95291 raw) lines in 144 files

KaneTW fucked around with this message at 12:01 on Jun 21, 2012

Zombywuf
Mar 29, 2008

tef posted:

:argh: HTTP/1.1 sends \r\n as line separators, and has a Host: header :argh:

It also requires the verb, then the url, then the version number.

Doctor w-rw-rw- posted:

This joke is old and tired. Please stop.
No joke.

quote:

PHP doesn't cause bad code
PHP is bad code.

Adbot
ADBOT LOVES YOU

Zombywuf
Mar 29, 2008

Zhentar posted:

And what's wrong with doing this instead?

A complete lack of RAII.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply