7 ms·
for(i = 0; i < 10; i++){ case 1:{ I just threw up a little in my mouth. All these years I didn't know such a monstrous thing was legal in C code. Is
by hinkley 6y ago
for(i = 0; i < 10; i++){
case 1:{
I just threw up a little in my mouth.
All these years I didn't know such a monstrous thing was legal in C code. Is it possible to make amendments to the Geneva Convention, and if so, who should I call?
- souprock 6y agoSo... I did this: switch(override){ default: if(foo==42){ case THING1: code_here(); }else if(bar&0x42){ case THING2: other_code(); }else{ case THING3: more_code(); } } I thought it was more readable than the alternatives.
- jolmg 6y agoIf override can only be those 3 and they're all non-zero, and default is when override is 0, then the following seems clearer in my opinion. if (override ? override == THING1 : foo == 42) { code_here(); } else if (override ? override == THING2 : bar & 0x42) { other_code(); } else if (override ? override == THING3 : true) { more_code(); }
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]
- predakanga 6y agoThere's a particularly famous instance of this, called Duff's Device[0], with a great quote attached (regarding fall-through in case blocks): > "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against." [0]: https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device
- wahern 6y agoIt's also used to portably implement generators and coroutines. See https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html More generally, the semantics are useful for machine code generation and translation. Computed goto's are even more useful for the above, but they're an extension. I'd love to see computed goto's added to the C standard, but it's far too late to change the semantics of switch. Rather, just accept that their code flow semantics make them slightly more type safe syntactic sugar for goto--not just in how they're implemented, but in how they can be used.
- tasty_freeze 6y agoThe very article this thread is about talks about Duff's device. This comment confirms my suspicion that most HN readers, like me, read the comments before optionally reading the article.
- saagarjha 6y agoOften the comments are better than the article :( But yes, you need to be careful in order to not bring up things that were already mentioned. Often it is good enough to read the comments, skim the article, and then only post a comment.
- predakanga 6y agoA fair judgement - I had only skimmed the article before replying. I mostly wanted to provide the quote from Duff, it seemed relevant to the OP.
- akerro 6y agoFUCK, you might think it's funny, but I literally work with one senior developer (10+ years experience in pure Java). He write code like that, during review insists this is OK because he's been writing such code for over a decade! I inherited this project after he moved to another country and is not legally to work remotely for company in the UK. He has permissions to override jenkins+sonarqube, so he merges code without test coverage. His code has literally 0 tests. Only today I was rewriting code like: if(i == 1) {} else if(b == true){} else if(s == "string") {} else if(response.status() == Status.OK){} else if(hereIsARecursiveMethodAlwaysReturningFalse()){} ... 3 more notice each `if` block has different variable of different type!
- specialist 6y agoI gently encourage you to dust off your copy of the CIA Sabotage Field Manual and set about getting your tormenter fired.
- lmilcin 6y agoAt least he didn't put each one of those statements in separate classes to make you hunt for half a day to figure out what his code does.
- joshxyz 6y agoFucking hell that's a nightmare haha
- kazinator 6y agoIs there a bug in there? What's a better way to structure those tests, if they are logically valid?
- IggleSniggle 6y agoDepends on the actual situation, but just from looking at it I would guess that named functions instead of nebulous unrelated nested branches would be a start. Edit: it’s also just kind of a code smell that suggests the overall structure is not well thought out, with that particular collection of tests. Could be fine in that respect in context tho
- kijdnrs 6y agoThe International Committee of the Red Cross (ICRC) in Geneva is the traditional guardian of international humanitarian law, and has led the development of additions to the Geneva Conventions of 1949, such as the Additional Protocols 1 and 2 (https://www.icrc.org/en/doc/resources/documents/misc/additional-protocols-1977.htm https://www.icrc.org/en/doc/resources/documents/misc/additio...). You can reach them at +41-22-734-60-01
- wiredfool 6y agoYou need to call the IOCC.
- saagarjha 6y ago*IOCCC
- kazinator 6y agoI have the following pattern two or three times before. When it comes up, it's very useful. It can eliminate code repetition, without the bother of making a whole new function: for(i = 0; i < 10; i++){ switch (i) { case 1: // this is special for 1 break; case 3: // this is special for 3 // fallthrough case 5: // case 5 needs 3 processing, plus its own break; case ...: // ... } // a slightly long block here // common to all cases. } I don't think I've ever done anything quite like this though: switch (i) { case 0: // so we can enter the for at all for (i = 0; i < 10; i++) { case 1: ; } } which is what the parent comment is getting at, if taken literally.