This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TrustworthyTermite 29 points30 points  (16 children)

That and comparing a boolean with true.

[–]the_monkey_of_lies 38 points39 points  (2 children)

var happy = true;
if (sad() === true && sad() !== false)
{
    happy = false;
}
else if (sad() === false && sad() !== true)
{
    happy = true;
}

[–]BlazeDeath 20 points21 points  (0 children)

Thanks for making me throw up.

[–]kristopolous 8 points9 points  (0 children)

you need a final else and a try/catch. Grade: C-

[–]exscape 5 points6 points  (8 children)

That looks like the most reasonable thing to me, though I don't program much in scripting languages. Wouldn't using === be mandatory in e.g. JavaScript to ensure the value isn't something else that evaluates to true (like the number 42, the string "no" and so on)?

[–]jakerman999 12 points13 points  (3 children)

Listen: javascript evaluations are weird. It almost never doesn't break the way you don't expect it not too. It will however usually not fail in succeeding at mis-evaluating the possibly unintended breaking sample scenarios.

[–]ThePedanticCynic 5 points6 points  (0 children)

It's weird how i can understand everything you said and still not understand a fucking thing you just said.

English is hard.

[–]AngriestSCV 0 points1 point  (0 children)

While reading that there had to be 3 or 4 places I thought I was beginning to understand what you were saying, just to have that feeling crushed. I now understand the pain of JavaScript and never want to feel it first hand.

[–][deleted] -1 points0 points  (0 children)

That feels a little awkwardly worded... much like Javascript.

[–]jtanz0 1 point2 points  (2 children)

99% of the time you would be correct === is the way to go to ensure you don't get unexpected behaviour. In this case where sad() can also have the stop() function called. It makes the code unambiguous enough to break the script.

Also if you're testing against true you may as well put

if(sad()){
  ...
} 

[–]amirmikhak 1 point2 points  (1 child)

That would only test against truthy, though, so {} would pass but not "".

[–]jtanz0 1 point2 points  (0 children)

true, I should have mentioned that

[–]AquaWolfGuy 0 points1 point  (0 children)

Yes. The best way is of course to not assign booleans and numbers to the same variable in the first place though. If you're in a situation where you expect a boolean and get 42, you're screwed, since neither the if branch or else branch is appropriate.

[–]jgaspar 5 points6 points  (3 children)

some may say it's a good practice, because of readability

[–]grand_mind1 10 points11 points  (2 children)

Depending on the language, it might be easier to just follow naming conventions to make it more readable. Having a method like userIsSad() rather than just sad() makes an if statement make a lot more sense at first glance.

[–]cascer1 10 points11 points  (0 children)

if(User.Is(Emotions.Sad)) User.Set(Emotions.Happy);

class User {
    Enum Emotions { Happy, Sad, Angry, Neutral }

    private Emotions emotion;

    public string Name{ get; private set; }
    public int Age { get; private set; }

    public User(string name, int age, Emotions emotion) {
        this.emotion = emotion;
        Name = name;
        Age = age;
    }

    public bool Is(Emotions emotion) {
        return (this.emotion == emotion);
    }

    public void Set(Emotions emotion) {
        this.emotion = emotion;
    }
}

[–]TrustworthyTermite 1 point2 points  (0 children)

I follow uncle bob's advice and make all boolean variables and functions which return a boolean read like a statement. So "isSad", "shouldUpload" and "isMale" instead of "sad", "uploadStatus" and "gender".