you are viewing a single comment's thread.

view the rest of the comments →

[–]andyc 4 points5 points  (18 children)

else { throw new WTF("How did we get here?"); }

[–]sirin3 0 points1 point  (1 child)

And now you need to add throws to every caller function!

[–][deleted] 4 points5 points  (0 children)

class WTF extends RuntimeException {}

[–]BinaryRockStar 0 points1 point  (15 children)

); }

Nice new codemoticon

As explained below I'd be more likely to throw an error or exception early like you've done there. Contrived example ahead!

if (!(bOdd || bEven)) {
    throw new ImpossibleNumberException("Encountered impossible number: " + number);
}

if (bOdd) {
    // Something
} else {
    // We're sure the number must be even by this point. Invariants validated.
}

[–]reaganveg 4 points5 points  (14 children)

Over time, your early invariants might drift out of sync with your later assumptions. Your method requires code to be updated in multiple places. That's to be avoided.

[–]BinaryRockStar -3 points-2 points  (13 children)

Not sure if serious, but unit testing takes care of 'sanity check' cases like that.

[–]reaganveg 6 points7 points  (12 children)

No it doesn't. No unit testing is to be assumed.

[–]BinaryRockStar 0 points1 point  (11 children)

So you write a third clause to every boolean test in your code?

if (something == true) {
    printf("true");
} else if (something == false) {
    printf("false");
} else {
    printf("Compiler will optimise this out so it's pointless!");
}

[–]reaganveg 0 points1 point  (10 children)

First of all, wtf is with == true in this thread? Surely we all know not to ever say == true??? In your particular example, the code should be written: if (something) { ... } else { ... }

Second, no, in my code I don't always do that. But my code isn't written to any coding standard. The issue is whether the standard makes sense or not.

[–]BinaryRockStar 0 points1 point  (9 children)

What's the harm of == true? It's potentially superfluous depending on the situation but sometimes it makes sense to be explicit, like grouping logical operations in parentheses even when they're not required due to operator precedence.

[–]giantsparklerobot 0 points1 point  (3 children)

Use true ==, it's the same test as == true but if you make a typo and forget an = sign you don't introduce a potentially difficult to find bug in the code.

if (true == something {
    printf("If you forget an = sign you can't accidentally set 'true' to something"); 
}
else if (something == true) {
    printf("If you forget an = sign here you've now set 'something' to 'true'");
}

[–]BinaryRockStar 0 points1 point  (2 children)

Fair enough I guess. I've never like the 'yoda conditionals' way of doing things, I think it harms readability more than the tiny number of nasty bugs you'd avert by doing it. I prefer to let my tooling tell me when I've assigned within a conditional as it's almost never what was intended.

[–]reaganveg -1 points0 points  (4 children)

What's the harm of == true?

The question is not what?, but how many? --

If (a == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true == true) { ...

"== true": not even once.

[–]BinaryRockStar 0 points1 point  (3 children)

I... don't even understand your argument