12
Fexell
6y

The way this if statement is written (across multiple lines) is really weird to me. Anyone here that writes if statements like this?

Comments
  • 9
    Yes. Breaking the line instead of scrolling.
  • 3
    Nope, I'd put both statements in variables with names describing what the value means...

    Edit: ... Or skip the first linebreak:

    if(prettyLong(thing) ||
    thisOther[thing]) {
    //...
    }

    Or however you'd like your curlies aligned.
  • 4
    Never seen that before.
    I always write longer if statements like this:

    if(longCondition ||
    otherCondition) {

    }
  • 2
    I'd attach the parentheses and vertically align the conditions, but yes. It's significantly easier to read two similar lines than one very long line.

    Or if it's more complex, I'd break up the logic into a builder-like approach:

    condition = blah
    condition = condition && blah
    condition = condition || blah
    ...

    if (condition) { ... }
  • 6
    Doesn’t matter what language, just pure logic, these are always true:
    Not a or not b = not (a and b)
    Not a and not b = not (a or b)
  • 3
    It seems fine to me. New lines for better readability 😀
  • 2
    Only thing I do for readability is place the comparison operator on the next line. Like this:

    if (
    doSomeCoolStuff(user)
    || doSomeBoringStuff(user)
    ) {
    //
    }
  • 3
    I'd write an (and) statement instead of (not or not) and put it all on one or two lines max
  • 2
    @frickerg I guess it depends on the situation. In some cases an "or" statement can have quite the performance impact, since the second statement isn't executed if the first statement is true
  • 1
    Yeah, do that stuff daily, though I newline before the operators, not after. I know its culprits, eg. assigning a variable can only be done in the very last statement etc.

    I'm fine with it as long as I can interpret what's going on.. and this isn't even close to what I sometimes have to do to implement certain logic altogether.
  • 1
    Hmm. I have never written if statements this way before. I understand the argument for readability, but it feels like a pattern-breaker, unless you write every if statement like that. If I would come across that it would take me a second to realize that that is an if statement.
  • 1
    @dennie170 it's been a while since my algorithmics course, but isn't it the same for AND too? Despite the efficiency, I don't want to use chained NOT expressions in one statement for better readability.
  • 0
    Also, that console log.. "propertydoesnt exist" at least chain the property variable to it.. so that the debug makes sense..

    console.log('property doe...', collection, property);
  • 1
    @frickerg you're totally right. Sorry, wasn't really paying attention...
Add Comment