14
Fexell
18d

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

Comments
  • 8
    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) {

    }
  • 1
    That is a guilty pleasure of mine.. I love it, but it feels out of place at work.

    I also find it very helpful helpful for function calls with a lot of parameters and long, meaningful variable names.
  • 4
    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) { ... }
  • 7
    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 😀
  • 4
    Only thing I do for readability is place the comparison operator on the next line. Like this:

    if (
    doSomeCoolStuff(user)
    || doSomeBoringStuff(user)
    ) {
    //
    }
  • 4
    @dennie170
    I second this. Beginning of line for operators because it's sooooo much easier to read the flow of
  • 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
  • 2
    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...
Your Job Suck?
Get a Better Job
Add Comment