Comment On Else... where?

"I had a professor once who said that given enough NAND gates, he could rule the world," writes Rob B. "This was a roundabout way of saying that, using a whole bunch of NAND gates, you could create the function of any other logic gate. You shouldn't, because the other logic gates exist and it would be hugely wasteful to use NAND gates to do the same thing, but it can be done. It turns out this applies to code as well." [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Re: Else... where?

2010-02-01 09:04 • by Anon (unregistered)
Well on embedded systems without a filesystem, etc., etc....

Re: Else... where?

2010-02-01 09:05 • by Severity One
Maybe the developer didn't know about else-if, but apparently, Rob (the submitter) didn't know about the switch statement either.

Re: Else... where?

2010-02-01 09:05 • by Eli (unregistered)
Yeah, this code sucks. He should have used do { ... } where (false) so he didn't have to break at the end!

CAPTCHA: nulla - stereotypical Italian undefined value.

Re: Else... where?

2010-02-01 09:06 • by MycroftMkIV (unregistered)
He also seems to have no clue about the C++ switch statement....

Re: Else... where?

2010-02-01 09:07 • by steenbergh
I'm not familiar with C++, but wouldn't a switch() be better in this case than an if-elseif, because you're testing the same variable?

(3rd attempt... when will the human finally look into this?)

Edit: I've cracked it: The errors are there to give the ninjas the time to do their thing!

Re: Else... where?

2010-02-01 09:07 • by muzzylogic
What's wrong with a swtich/case construct?

Re: Else... where?

2010-02-01 09:07 • by The_Assimilator
297569 in reply to 297563
Wow. Just, wow.

Re: Else... where?

2010-02-01 09:07 • by frits
Any procedural solution for what this code is attempting would be smelly. However, I must say this is probably one of the ugliest ways to do it.

Re: Else... where?

2010-02-01 09:08 • by Anonymous coward (unregistered)
Don't know if it was mentioned already, but what about switch?

Re: Else... where?

2010-02-01 09:08 • by halcyon1234
Else if and switch sound good, but what are you going to do when mainType == 7 AND mainType == 9, huh?

Re: Else... where?

2010-02-01 09:08 • by FriedDan
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

It could be written:

if(mainType == 7)
subType = 4;
if(mainType == 9)
subType = 6;
if(mainType == 11)
subType = 9;

There's no potential overlap so no NEED for an else (even if it might make it more readable).

Re: Else... where?

2010-02-01 09:10 • by MainCoder (unregistered)
297575 in reply to 297567
I tend to avoid switch constructs myself, unless each case ends with a return statement (e.g. the switch is the main part of some conversion function). The reason being that I dislike having breaks in each case, as well as avoiding potential logic bugs that can arise due to forgetting one.

Re: Else... where?

2010-02-01 09:11 • by Ed (unregistered)
297576 in reply to 297574
FriedDan:
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

It could be written:

if(mainType == 7)
subType = 4;
if(mainType == 9)
subType = 6;
if(mainType == 11)
subType = 9;

There's no potential overlap so no NEED for an else (even if it might make it more readable).


But what if mainType is 7, 9 AND 11? Then you're just screwed!

CAPTCH: dignissim (missingID backwards, perhaps?)

Re: Else... where?

2010-02-01 09:12 • by sjakie (unregistered)
Why can you mortals not accept this highly optimized piece of code?
Everybody knows switch statements are a CPU killer, even worse than an if...else construct. This sub contractor really knew what he was doing, although I expect he would have rather written it in a proper language like assembly to begin with.

CAPTCHA - Genitus - somebody with highly intellectual genitals?

Re: Else... where?

2010-02-01 09:12 • by dkf
297578 in reply to 297573
halcyon1234:
Else if and switch sound good, but what are you going to do when mainType == 7 AND mainType == 9, huh?
That could actually be possible if it was a volatile variable that is tied to some piece of memory mapped hardware. But I'd rather not think about that.

Re: Else... where?

2010-02-01 09:14 • by ParkinT

while(subcontractor==nitiwt)
{
if (Paula == "Brillant")
return true;
if (Table == "wooden")
return true;
if (XML == true)
return true;
if (Post == "FIRST")
return null;
if (Post == "FRIST")
return subcontractor;
}

Re: Else... where?

2010-02-01 09:14 • by frits
297580 in reply to 297576
Ed:
FriedDan:
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

It could be written:

if(mainType == 7)
subType = 4;
if(mainType == 9)
subType = 6;
if(mainType == 11)
subType = 9;

There's no potential overlap so no NEED for an else (even if it might make it more readable).


But what if mainType is 7, 9 AND 11? Then you're just screwed!

CAPTCH: dignissim (missingID backwards, perhaps?)


If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.

Re: Else... where?

2010-02-01 09:14 • by Anonymous (unregistered)
I think you're right, he probably had no idea about the 'else' clause. Seems unlikely but then it was only the other day that we had someone who didn't know about 'break'. The simple fact is that we're working in a profession where proper understanding is the exception, not the rule.

Re: Else... where?

2010-02-01 09:16 • by Lennart (unregistered)
Ed:
FriedDan:
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

It could be written:

if(mainType == 7)
subType = 4;
if(mainType == 9)
subType = 6;
if(mainType == 11)
subType = 9;

There's no potential overlap so no NEED for an else (even if it might make it more readable).


But what if mainType is 7, 9 AND 11? Then you're just screwed!

CAPTCH: dignissim (missingID backwards, perhaps?)


if(mainType == 11)
subType = 9;
if(mainType == 9)
subType = 6;
if(mainType == 7)
subType = 4;

fixed

Re: Else... where?

2010-02-01 09:17 • by Ed (unregistered)
297584 in reply to 297580
frits:
Ed:
FriedDan:
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

It could be written:

if(mainType == 7)
subType = 4;
if(mainType == 9)
subType = 6;
if(mainType == 11)
subType = 9;

There's no potential overlap so no NEED for an else (even if it might make it more readable).


But what if mainType is 7, 9 AND 11? Then you're just screwed!

CAPTCH: dignissim (missingID backwards, perhaps?)


If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.


Damn, thanks for clearing that up. My sarcasm chip must be on indecipherable today.

Re: Else... where?

2010-02-01 09:20 • by frits
297585 in reply to 297584
Ed:
frits:
Ed:
FriedDan:
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

It could be written:

if(mainType == 7)
subType = 4;
if(mainType == 9)
subType = 6;
if(mainType == 11)
subType = 9;

There's no potential overlap so no NEED for an else (even if it might make it more readable).


But what if mainType is 7, 9 AND 11? Then you're just screwed!

CAPTCH: dignissim (missingID backwards, perhaps?)


If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.


Damn, thanks for clearing that up. My sarcasm chip must be on indecipherable today.


Sorry, you threw me off by posting your captcha, which usually is an indicator the post will not be funny.

Re: Else... where?

2010-02-01 09:22 • by Ed (unregistered)
297586 in reply to 297585
frits:
Ed:
frits:
Ed:
FriedDan:
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

It could be written:

if(mainType == 7)
subType = 4;
if(mainType == 9)
subType = 6;
if(mainType == 11)
subType = 9;

There's no potential overlap so no NEED for an else (even if it might make it more readable).


But what if mainType is 7, 9 AND 11? Then you're just screwed!

CAPTCH: dignissim (missingID backwards, perhaps?)


If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.


Damn, thanks for clearing that up. My sarcasm chip must be on indecipherable today.


Sorry, you threw me off by posting your captcha, which usually is an indicator the post will not be funny.


Point taken, sarcastic response retracted ;)

Re: Else... where?

2010-02-01 09:24 • by the beholder (unregistered)
297587 in reply to 297580
frits:
If mainType is 7, 9 AND 11 your screwed anyway because you either have major thread sync issues, or you have landed in an alternate universe.
No, he's just using Schrodinger's integers.

Re: Else... where?

2010-02-01 09:29 • by Drew (unregistered)
297588 in reply to 297579
ParkinT:

while(subcontractor==nitiwt)
{
if (Paula == "Brillant")
return true;
if (Table == "wooden")
return true;
if (XML == true)
return true;
if (Post == "FIRST")
return null;
if (Post == "FRIST")
return subcontractor;
}

You for got

if(fileSystem == "embedded")
return "embedAllImages";

Re: Else... where?

2010-02-01 09:33 • by apaq11
It's been a while since my Computer Engineering classes but from what I remember the reason for using NAND gates instead of OR/AND gates was because they had less transistors making them cheaper. This also meant their propagation delay was lower and thus faster than an equivalent AND/OR circuit.

I vaguely recall exercises in converting AND/OR circuits into only NAND gates.

Re: Else... where?

2010-02-01 09:33 • by Consultuning (unregistered)
Erm... my optimizing mind only can say... why is that nobody is not pointing that branching is mostly unnecessary? All that discussion about switch is puzzling, because the whole thing can be reduced to

subType = mainType - 3 ;
if( subType == 8 )
++subType;

True, the original code does not alter the value of the subType if mainType is outside the 7,9,11 range. Let's add another twist, assuming that mainType is an integer

if( mainType > 6 && mainType < 12) {
subType = mainType - 3 ;
if( subType == 8 )
++subType;
}

This is in my mind way more legible and does exactly the same. Or I'm completely confused?

If the code was about mapping arbitrary mainType values to subType values, then a lookup table would be better, by the way.

Re: Else... where?

2010-02-01 09:37 • by BBT (unregistered)
"so he cobbled together an 'else if' in the most ridiculous way possible."

That sounds like a challenge. I'm sure someone here could do more ridiculous!

Re: Else... where?

2010-02-01 09:39 • by Nicolai Jørgensen (unregistered)
Actually, the professor is right about the NAND gates (and the poster should have paid more attention in class).

It IS the most basic 2-input-gate to use and all other gates are designed from NAND-gates (using only 4 transistors) and then the redundant transistors are removed for optimization.

Even an AND-gate is just a NAND-gate with and inverter (2 transistors) in any processor design you'll find.

Check this wiki for further info:
http://en.wikipedia.org/wiki/NAND_logic

Re: Else... where?

2010-02-01 09:40 • by Douglas (unregistered)
297594 in reply to 297580
No need for threads or alternative universes:

#include <iostream>

#define P(_X) std::cerr << _X << std::endl;

class MainType
{
public:
bool operator==(int)
{
return true;
}
};

int main()
{
MainType mainType;

if(mainType == 7)
P(4);
if(mainType == 9)
P(6);
if(mainType == 11)
P(9);
}

Re: Else... where?

2010-02-01 09:41 • by Jonesey (unregistered)
break;

/*
Wow, that's a sexy piece of code! It looks so beautiful I want to slip something in it's drink and take it back to mine, where it'll wake up in the morning confused and with a sore rear!
*/

continue;

Re: Else... where?

2010-02-01 09:41 • by Skilldrick` (unregistered)
so he cobbled together an 'else if' in the most ridiculous way possible


I think without the knowledge of else if, this isn't the most ridiculous way to cobble the semantics of an else if statement together. I think it's pretty damn elegant actually...

Re: Else... where?

2010-02-01 09:44 • by Carl (unregistered)
297597 in reply to 297590
apaq11:
I vaguely recall exercises in converting AND/OR circuits into only NAND gates.
And you've gone on to use that in your Information Systems career how, exactly? Because at my University they were obsessed with making us slog thru this crap while never saying a word about SQL Injection or Cross-Site Scripting.

"A human will eventually look at it." riiight.

Re: Else... where?

2010-02-01 09:48 • by Anon (unregistered)
297599 in reply to 297576
Ed:

But what if mainType is 7, 9 AND 11? Then you're just screwed!


People who think this is a comment on thread synchronization are TRWTF. Clearly we're talking about quantum computing here.

Re: Else... where?

2010-02-01 09:49 • by Anon (unregistered)
297600 in reply to 297564
Severity One:
Maybe the developer didn't know about else-if, but apparently, Rob (the submitter) didn't know about the switch statement either.


Yes, a switch makes more sense, but, given that the original programmer knows about "if" how is it conceivable that they don't know about "else" and "else if"? You could (almost) forgive that they just plain didn't know about switch, but to know "if" and not "else"/"else if" is just mind blowing.

Re: Else... where?

2010-02-01 09:56 • by Bub (unregistered)
Haven't you ever seen .Net-optimized code before?

Re: Else... where?

2010-02-01 09:58 • by Manos (unregistered)
It's the infinite loop that always breaks after the first execution.
That should get patented immediately.

Re: Else... where?

2010-02-01 09:59 • by po8crg
297604 in reply to 297575
MainCoder:
I tend to avoid switch constructs myself, unless each case ends with a return statement (e.g. the switch is the main part of some conversion function). The reason being that I dislike having breaks in each case, as well as avoiding potential logic bugs that can arise due to forgetting one.


As an ex-VB programmer, this is the one time I miss VB. Select Case ... Case ... End Select was a much better construct than switch.

I'd like C# to include a switch statement where each case had to be followed by a block, so it would look like:

select (mainType)
{
case (7)
{ subType = 4; }
case (9)
{ subType = 6; }
case (11)
{ subType = 9; }
}

I don't have a problem with fall-through switch, but can we have a non-fall-through version where you can't mess yourself up by forgetting a break?

I've used the select keyword in my example because that's what VB uses - if C# can steal good ideas from non-Microsoft languages, why not one from their own VB?

Re: Else... where?

2010-02-01 09:59 • by Zylon
297605 in reply to 297591
Consultuning:
Erm... my optimizing mind only can say... why is that nobody is not pointing that branching is mostly unnecessary? All that discussion about switch is puzzling, because the whole thing can be reduced to

subType = mainType - 3 ;
if( subType == 8 )
++subType;

There's a special place in Maintenance Hell for coders like you.

Re: Else... where?

2010-02-01 10:00 • by JamesCurran
Everyone here who's talking about "not knowing about switch statements" is missings the bigger picture. Look at that code again. That's very close to a switch statement. Clearly he knows about switch statements -- I'm guessing from VB --- But doesn't realize that C/C++ has them also.

I'll bet he's thinking that he's created a way to "do select/case in C++", and is feeling very proud of himself....

Re: Else... where?

2010-02-01 10:01 • by z (unregistered)
297607 in reply to 297574
using else if gives you the benefit of not checking the conditionals after the first match has been found.

if(mainType == 7)
subType = 4;
else if(mainType == 9)
subType = 6;
else if(mainType == 11)
subType = 9;

This should be a bit more efficient then -- disregarding the fact that switch would be even better.

Re: Else... where?

2010-02-01 10:03 • by Alan (unregistered)
Pfft. You C guys and your silly switch statements. All we really need is a map!

types = {
7: 4,
9: 6,
11: 9,
}

if baseType not in types:
return None

return types[baseType]


Python FTW!

(of course it'd be better to avoid the magic numbers altogether, but we don't seem to have that luxury here)

Re: Else... where?

2010-02-01 10:16 • by Mikuso (unregistered)
297609 in reply to 297573
subType would be 4, just like in the original code.

Re: Else... where?

2010-02-01 10:16 • by PITA (unregistered)
297610 in reply to 297576
Ed:
FriedDan:
As for the WTF in the WTF, I'm not even sure why the else-if statement is relevant without a switch.

It could be written:

if(mainType == 7)
subType = 4;
if(mainType == 9)
subType = 6;
if(mainType == 11)
subType = 9;

There's no potential overlap so no NEED for an else (even if it might make it more readable).


But what if mainType is 7, 9 AND 11? Then you're just screwed!

CAPTCH: dignissim (missingID backwards, perhaps?)
Where is Dr. Who when you need him?

Re: Else... where?

2010-02-01 10:23 • by toth
This is a stupid block of code. They obviously should have used GOTOs.

Re: Else... where?

2010-02-01 10:26 • by Chuck (unregistered)
297612 in reply to 297579
ParkinT:

while(subcontractor==nitiwt)
{
if (Paula == "Brillant")
return true;
if (Table == "wooden")
return true;
if (XML == true)
return true;
if (Post == "FIRST")
return null;
if (Post == "FRIST")
return subcontractor;
}


frawress

Re: Else... where?

2010-02-01 10:26 • by pete (unregistered)
Other than the use a switch and you don't even need an else statement comments which are obviously true. I'm guessing that code has had some bad refactoring done to it. I'm guessing a giant method operating on a while loop for multiple records was used initially and broken into sub methods (badly).

CAPTCHA: jumentum

Re: Else... where?

2010-02-01 10:27 • by Brian Manahan (unregistered)
Nobody here seems to know about a map or an associative array.

Re: Else... where?

2010-02-01 10:29 • by lesle (unregistered)
297615 in reply to 297587
Who'd've thunk!

Re: Else... where?

2010-02-01 10:31 • by pete (unregistered)
297616 in reply to 297605
That's assuming a mainType always changes the subType. What if the mainType is not 7, 9 or 11.

Re: Else... where?

2010-02-01 10:33 • by pete (unregistered)
297618 in reply to 297605
Zylon:
Consultuning:
Erm... my optimizing mind only can say... why is that nobody is not pointing that branching is mostly unnecessary? All that discussion about switch is puzzling, because the whole thing can be reduced to

subType = mainType - 3 ;
if( subType == 8 )
++subType;

There's a special place in Maintenance Hell for coders like you.


That's assuming a mainType always changes the subType. What if the mainType is not 7, 9 or 11?

First reply went wrong.
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Add Comment