Probably a veery long time ago, but being able to use what you use in if(...){} for e.g. booleans like "bool(ean) isX = a == x;".
And than reusing that value in if again and so on.

Even if it sounds trivial, there was a time where it was not and "==" was only associated with "you use that in if/while only" rather than "a == b" returns a bool(ean)/int.

Same goes for other arithmetic operators and && / || ofc.

  • 1
    It's a code smell.
  • 2
    @electrineer Not it ain't wtf
  • 1
    @electrineer It's a great feature, but has to be used with caution to avoid that.

    It can be used however, to make conditions more expressive:
    bool areTouching(... subject, ... touchable) {
    // Are rectangles subject and touchable touching each other?
    bool outOfY = subjectBottomY < touchableTopY || subjectTopY > touchableBottomY; bool outOfX = subjectRightX < touchableLeftX || subjectLeftX > touchableRightX;
    if(!outOfY && !outOfX)
    return true;
    return false;
    // or: return !outOfY && !outOfX;

    Is a lot more easy to understand than to put everything into one giant condition.
  • 2
    You can use it for loads of stuff. How about `return array.length > 0;` It's super cringy when people only use operators in if conditions like they haven't understood what an expression is
  • 2
  • 1
    @12bitfloat you're right, even Google's c++ style guide tells to use that as the variable will be in narrower scope.
Add Comment