Programming Confessions Thread
-
@Zecc You could always do it like this to avoid that.
/* Do some work 0 */ if(allOk) { /* Do some work 1 */ } if(allOk) { /* Do some work 2 */ } /* Do some work 3 */
-
@anotherusername said in Programming Confessions Thread:
@Zecc said in Programming Confessions Thread:
This:
do { /* Do some work 0 */ if( !allOk ) break; /* Do some work 1 */ if( !allOk ) break; /* Do some work 2 */ } while(false); /* Do some work 3 */
is similar to this:
/* Do some work 0 */ try { if( !allOk ) throw new Exception(); /* Do some work 1 */ if( !allOk ) throw new Exception(); /* Do some work 2 */ } catch {}; /* Do some work 3 */
But without having to use exceptions for control flow inside just one function.
In particular it prevents actual exceptions for being thrown while still not needing a custom exception class.Or this.
/* Do some work 0 */ if(allOk) { /* Do some work 1 */ if(allOk) { /* Do some work 2 */ } } /* Do some work 3 */
Or, as one of our former team members liked to do:
output=-1; /* Do some work 0 */ if(allOk1) { /* Do some work 1 */ if(allOk2) { /* Do some work 2 */ } else { printf("All was not OK 2!"); } } else { printf("All was not OK 1!"); } return output;
-
@anotherusername said in Programming Confessions Thread:
Or this.
/* Do some work 0 */ if(allOk) { /* Do some work 1 */ if(allOk) { /* Do some work 2 */ } } /* Do some work 3 */
Except then you have the (very real!) mental overhead of the nesting.
-
@TimeBandit said in Programming Confessions Thread:
@Zecc said in Programming Confessions Thread:
while still not needing a custom exception class.
And, most importantly, no GOTO
That irrational hatred/fear of goto is a great example of cargo culting in programming.
It's just a jump, it has its uses.
-
@MrL said in Programming Confessions Thread:
It's just a jump, it has its uses.
Strictly, it's a true flow control primitive, a thing you use (along with conditional execution) to build pretty much all higher-level structured programming constructs. As much as reasonably possible, we should use other types of flow control pattern (
while
,foreach
,switch
,try
/catch
, etc.) where we can as that makes it much easier to understand what's going on and explain it to others, but there's always other patterns possible andgoto
means you can implement them if you must.Alas, some people mix up “this is a power tool that should usually be left well alone” with “NEVER EVER TOUCH THIS! WHARRRRGARBL!”
-
@MrL said in Programming Confessions Thread:
@TimeBandit said in Programming Confessions Thread:
And, most importantly, no GOTO
That irrational hatred/fear of goto is a great example of cargo culting in programming.
I'm betting the vast majority of the cargo cultists have never actually read anything of Dijkstra's piece beyond the title, which Dijkstra didn't even write and felt overstated his case. "GO TO Considered Harmful" was the heading given it by the editor when it ran in the journal's letter column.
-
@Watson said in Programming Confessions Thread:
I'm betting the vast majority of the cargo cultists have never actually read anything of Dijkstra's piece beyond the title, which Dijkstra didn't even write and felt overstated his case. "GO TO Considered Harmful" was the heading given it by the editor when it ran in the journal's letter column.
FWIW, I've read the original letter. It's really a plea in favour of the sorts of structural programming we do now, and against the
GOTO
stuff that was present in FORTRAN, BASIC and PL/1 programs of the 1970s (especially where the targets were computed numeric labels, which is just about the most evil programming thing you can do without the worst of runtime codegen).
-
@dkf said in Programming Confessions Thread:
Alas, some people mix up “this is a power tool that should usually be left well alone” with “NEVER EVER TOUCH THIS! WHARRRRGARBL!”
That's exactly what I meant.
@Watson said in Programming Confessions Thread:
I'm betting the vast majority of the cargo cultists have never actually read anything of Dijkstra's piece beyond the title
Most young programmers never even heard about the article.
-
@dkf said in Programming Confessions Thread:
@anotherusername said in Programming Confessions Thread:
Or this.
/* Do some work 0 */ if(allOk) { /* Do some work 1 */ if(allOk) { /* Do some work 2 */ } } /* Do some work 3 */
Except then you have the (very real!) mental overhead of the nesting.
I don't feel like the overhead of the nesting is more than the overhead of a
break
or an exception, though. If the logic gets too complex, then it probably needs to be simplified by breaking it into smaller, easier-to-understand parts. Moving some of it into a function can be an appropriate way of doing this, but you then have the added mental overhead of the function call, too, so it should be done carefully, not indiscriminately.
-
@dkf said in Programming Confessions Thread:
the GOTO stuff that was present in FORTRAN, BASIC and PL/1 programs of the 1970s
Yes, may I help you?
-
@TimeBandit said in Programming Confessions Thread:
@TimeBandit said in Programming Confessions Thread:
Obligatory xkcd
After some verification
Of course
Note:
The goto operator is available as of PHP 5.3.
So they added it in June 2009, it's not a mistake made at the beginning and kept for backward compatibility
Due to popular demand. PHP programmers regress rather than progress. Eventually they want what assembler can do.
-
The goto operator can be used to jump to another section in the program. The target point is specified by a label followed by a colon, and the instruction is given as goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break. [Emphasis mine]
IOW:
goto bailout;
instead ofbreak n;
where n is a number that depends on the depth of loop/switch nesting. Addinggoto
was followed by removing computed break/continue targets in 5.4.Of course, it allows jumping backwards, as long as the target label isn't inside another loop/switch. So you could rewrite all your control flow as gotos; Dijsktra just wants to know why you would if there are better flow control operators available and you're not a pot-smoking ex-politician from Canada.
@Gribnit said in Programming Confessions Thread:
Eventually they want what assembler can do.
Everything?
Or do they want this?
-
@Watson said in Programming Confessions Thread:
you're not a pot-smoking ex-politician from Canada.
I've never been a politician
-
@Watson said in Programming Confessions Thread:
a pot-smoking ex-politician from Canada.
He's not really an ex-politician; he's more of an unsuccessful politician, but he keeps trying.
-
@Watson said in Programming Confessions Thread:
The goto operator can be used to jump to another section in the program. The target point is specified by a label followed by a colon, and the instruction is given as goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break. [Emphasis mine]
IOW:
goto bailout;
instead ofbreak n;
where n is a number that depends on the depth of loop/switch nesting. Addinggoto
was followed by removing computed break/continue targets in 5.4.To be fair,
break 3;
is horrible syntax. Is it python which allowsbreak labelled_loop
?
-
@PleegWat Java definitely allows labeling loops and breaking on the labels. From a quick Google search it appears Python rejected the feature, not sure if there's been movement since then.
-
@Gribnit said in Programming Confessions Thread:
@TimeBandit said in Programming Confessions Thread:
@TimeBandit said in Programming Confessions Thread:
Obligatory xkcd
After some verification
Of course
Note:
The goto operator is available as of PHP 5.3.
So they added it in June 2009, it's not a mistake made at the beginning and kept for backward compatibility
Due to popular demand. PHP programmers regress rather than progress. Eventually they want what assembler can do.
Having the power to say “this is a very exceptional case (no pun intended),
goto
is the best solution here” is a good thing. But not in the hands of a PHP programmer.
-
@topspin said in Programming Confessions Thread:
Having the power to say “this is a very exceptional case (no pun intended), goto is the best solution here” is a good thing. But not in the hands of a PHP programmer
I've been using PHP for almost 20 years, and I never ever used a goto
-
@TimeBandit said in Programming Confessions Thread:
I've been using PHP for almost 20 years, and I never ever used a goto
I'd have thought someone who used PHP for 20 years would use
goto pub
quite a lot.
-
@Watson said in Programming Confessions Thread:
pot-smoking ex-politician from Canada
Did I miss when Jean Chretien came out in favour of goto?
-
@TimeBandit said in Programming Confessions Thread:
@topspin said in Programming Confessions Thread:
Having the power to say “this is a very exceptional case (no pun intended), goto is the best solution here” is a good thing. But not in the hands of a PHP programmer
I've been using PHP for almost 20 years, and I never ever used a goto
Likewise (this is the Programming Confessions thread, after all); that was before PHP had session handlers and objects were simple enough for the manual to fully describe in a dozen lines (passing an object to a function? The function didn't get the object, it got a clone of the object unless you passed "by reference") .
I've been trying to think of the last time I used a goto and I've been coming up blank. I've used it in C to leave a function by jumping to where all the local
free()
s happen before returning (#define quit goto cleanup
), but that's not a thing in PHP.Oh, I remember, now. Because in PHP a
continue
is the same as abreak
inside a switch statement (and now PHP warns you and asks if you meant to saybreak
after all), I've occasionally donereswitch: switch($foo) { case 1: .... break; case 2: .... break; .... case 17: .... $foo = 1; goto reswitch; .... case 20: .... $foo = 2; goto reswitch; }
if cases 17 and 20 are trivial variations of cases 1 and 2 respectively that just need a little massaging. Obviously there are other ways to do that (and I'm even using
switch
less, now), but still: sometimes going full-on state machine is overkill.
-
switch($foo) { case CASE_SEVENTEEN: ...; /* FALLTHROUGH */ case CASE_ONE: ...; break; ... }
Though that doesn't work if you have multiple variants of the same case. Also in C you can do the following, but it's an abomination:
switch(type) { if (0) { case VARIANT_ONE: ...; } else if (0) { case VARIANT_TWO: ....; } else if (0) { case VARIANT_THREE: ...; } /* shared code */ break; case DIFFERENT_CLASS: ...; break; .... }
If you have multiple switches on the same
type
variable in an object-oriented language, you could probably use objects more effectively.
-
@PleegWat You think that's an abomination, try spreading a for loop over several cases.
-
@pie_flavor Yes, I'm aware of duff's device.
-
@PleegWat said in Programming Confessions Thread:
Though that doesn't work if you have multiple variants of the same case.
Yeah, probably should have made that explicit.
@PleegWat said in Programming Confessions Thread:
If you have multiple switches on the same type variable in an object-oriented language, you could probably use objects more effectively.
One of the big reasons I'm using switches less (the other is that, with better support of functions as first-class entities, more declarative approaches are easier).
-
@PleegWat said in Programming Confessions Thread:
Also in C you can do the following, but it's an abomination
OMG. It's horrifying and beautiful at the same time.
So, uh, would changing the
(0)
conditions all to(1)
conditions change anything?
-
@brie Probably not, since you're already in an
if
branch theelse
won't be entered regardless.
-
@PleegWat That was what I thought also. And for some reason,
if (1)
seems less -y thanif (0)
does.
-
@brie Both of them scream 'here be dragons', but
if (0)
screams it a bit louder.
-
@PleegWat said in Programming Confessions Thread:
Both of them scream 'here be dragons',
More like “Codethulhu wants your brains!”
-
@dkf Good point. This is not the kind of C code a sane person writes.
-
@PleegWat ...would C allow
if (NULL)
?
-
-
@dkf Right, but the standard doesn't specify its value, so it could be anything. And if the
if
condition could also be anything for that syntax,NULL
it seems like an appropriately -y condition. The condition is , if you will.
-
@brie said in Programming Confessions Thread:
Right, but the standard doesn't specify its value, so it could be anything.
Sure, but null pointers are false-y. There's literally a ton of code out there that assumes this; tens of thousands of projects will break if it is ever not true.
-
@Cursorkeys said in The Official Status Thread:
@Tsaukpaetra said in The Official Status Thread:
@Cursorkeys said in The Official Status Thread:
MPLAB- X IDE | Microchip Technology
Oh fuck!
I'm so sorry...
It crashes slightly less than the old MPLAB-IDE so it's got that going for it I guess...
I'll admit I wrote a whole on-chip serial terminal UI to avoid having to recompile, upload, and test a robot. It was faster to dink around with a limited subset of instructions saved to EEPROM than Re uploading a build.
-
@dkf I'm pretty sure the standard prescribes falsey null pointers.
-
@dkf @PleegWat The standard specifies that
0
is a null pointer constant, and casting it to a pointer must result in a null pointer. That doesn't mean the bit representation forNULL
, or a null pointer, has to be the same as the bit representation of the integer0
, though. It does mean that if you writeif (p)
, which is exactly the same asif (p != 0)
, the compiler has to produce code that comparesp
against a null pointer, whatever that might be, so your code won't break.
-
@MrL said in Programming Confessions Thread:
code review passed through all senior developers in the company
I am not sure this justifies the goto or injustifies the senior developers.
-
@brie Re
0
andNULL
being different, blame real mode x86far
addresses. Nobody sane (especially with anything even approximating a flat memory model at the programming level) ever implemented those as having a different bit pattern, but x86 in real mode was not a sane platform at all.
-
I don't know how to do WPF right (anything I've done has basically been pseudo-WinForms with
OnClick
methods scattered everywhere; I have yet to truly learn MVVM). And I've failed miserably in my attempts to make either WPF or WinForms look pretty.So I'm about to rebuild this UI using Electron.NET.
-
@Unperverted-Vixen Good on you! WPF is spurned by Microsoft anyways.
-
-
@Tsaukpaetra said in Programming Confessions Thread:
@Unperverted-Vixen Good on you! WPF is spurned by Microsoft anyways.
wat?
-
@pie_flavor said in Programming Confessions Thread:
@Tsaukpaetra said in Programming Confessions Thread:
@Unperverted-Vixen Good on you! WPF is spurned by Microsoft anyways.
wat?
Is WPF even on the roadmap for .Net Core?
It's as good as shitcanned at this point.
-
@Tsaukpaetra said in Programming Confessions Thread:
@pie_flavor said in Programming Confessions Thread:
@Tsaukpaetra said in Programming Confessions Thread:
@Unperverted-Vixen Good on you! WPF is spurned by Microsoft anyways.
wat?
Is WPF even on the roadmap for .Net Core?
Yes. It's literally the flagship feature of .NET Core 3.0. How did this slip by you?
It's as good as shitcanned at this point.
Windows-only = completely and utterly dead. Interesting logic you have there.
-
@pie_flavor said in Programming Confessions Thread:
@Unperverted-Vixen said in Programming Confessions Thread:
Electron
please, please, please don't.
If this were something for public release I’d agree with you. (Although I don’t know what I’d do instead, other than maybe curling up in a corner, crying, and abandoning the whole thing.) This is for a tool for personal use only, so I’m not going to be inflicting an eldritch horror upon the wider world.
-
@Unperverted-Vixen said in Programming Confessions Thread:
I’m not going to be inflicting an eldritch horror upon the wider world.
That's what they all say.
-
@pie_flavor said in Programming Confessions Thread:
@Tsaukpaetra said in Programming Confessions Thread:
@pie_flavor said in Programming Confessions Thread:
@Tsaukpaetra said in Programming Confessions Thread:
@Unperverted-Vixen Good on you! WPF is spurned by Microsoft anyways.
wat?
Is WPF even on the roadmap for .Net Core?
Yes. It's literally the flagship feature of .NET Core 3.0. How did this slip by you?
It's as good as shitcanned at this point.
Windows-only = completely and utterly dead. Interesting logic you have there.
Yep. You'll notice this beautiful graphic that you apparently quoted the phrase "Windows-only" from.
To my understanding, the whole point of Core was to get cross-platform compatibility , so explicitly calling out that they're not going to even fake it just demonstrates what they really intend.
I wouldn't be surprised if all it ended up being was a collection of shims to the framework 4 libraries.
-
@Tsaukpaetra said in Programming Confessions Thread:
@pie_flavor said in Programming Confessions Thread:
@Tsaukpaetra said in Programming Confessions Thread:
@pie_flavor said in Programming Confessions Thread:
@Tsaukpaetra said in Programming Confessions Thread:
@Unperverted-Vixen Good on you! WPF is spurned by Microsoft anyways.
wat?
Is WPF even on the roadmap for .Net Core?
Yes. It's literally the flagship feature of .NET Core 3.0. How did this slip by you?
It's as good as shitcanned at this point.
Windows-only = completely and utterly dead. Interesting logic you have there.
Yep. You'll notice this beautiful graphic that you apparently quoted the phrase "Windows-only" from.
I didn't. What graphic?
To my understanding, the whole point of Core was to get cross-platform compatibility , so explicitly calling out that they're not going to even fake it just demonstrates what they really intend.
As far as I can tell, it meant they intend to put it in .NET Core. If you meant something else you should probably say so.
I wouldn't be surprised if all it ended up being was a collection of shims to the framework 4 libraries.
Since when has .NET Core not been a fully standalone install on Windows?