77

Found this at work today.

Comments
  • 13
    Can you tell me where's the problem ? I mean I can't see anything wrong in this code ?
  • 10
    I neither don't see the bad you're talking about. This is actually exactly the code I would write....
  • 3
    @moagggi ohh I thought that I am the only one 😂
  • 56
    return count == 0;

    I'm outta here.
  • 2
    Does it work? Good. Then we don't have a problem.
  • 0
    @Letmecode @kamen yes there are many ways to write this code. The thing is that they are all correct in my opinion the difference is in execution time (probably milliseconds) and style
  • 13
    I thought return count == 0; would be the obvious simplification...
  • 1
    Although it is correct, I would prefer return count == 0;
  • 8
    These little things easily add up. If you dont try to keep stuff in one line you easily start having classes over 600 lines of code which end up being even bigger as time passes... in short... don't do this :)
  • 2
    Maybe planning to put something before "return false;"? I'm being naïve.
  • 0
    @Gauthier You should not do that. Only implement stuff when you actually need them
  • 2
    @olpe Sure, I wouldn't. It was not meant as an excuse, only a possible explanation.
  • 2
    return count == 0;
    or
    return !count == 0;

    I'm just 15 years old and already cringe at the code in the image...
  • 1
    @Letmecode It seems you've been dealing with way too much shitty JS.
  • 1
    I mean yeah return code==0 is sophisticated and all, but this one is just so much easier to read
  • 2
    @filthyranter !count == 0
    ??
    Not
    count != 0
    ?
  • 1
    Aside from rewriting the statement to one line, the '==' can lead to problems down the line.

    What if count is null? A string?
  • 1
    @Letmecode using double negative logic...how beautiful! This way the next developer can spend a minute to realize you should have written return count == 0;
  • 0
    @FuzzyMyztiq take it easy
  • 0
    @FuzzyMyztiq it's worse than that; this is JavaScript for the Mozilla Rhino engine. Variables could working with Java classes and therefore return Java objects. For example a Java string is not equal a JavaScript string, and so name.getValue() === "bob" will return false because they are not the same String object.
  • 1
    @kamen Better (if JS):

    return !count;
  • 1
    @RazorSh4rk You're resorting to different data types and rely on JavaScript's casting, which is a dirty hack in my book. If it was up to me, I'd initialise the variable with whatever variable I need (e.g. 0).
  • 0
    Coupe de gras
  • 0
    @Gauthier Ah, yes. Even though you could alternatively use return !(count == 0)
  • 0
    @aamitkov
    Boolean(0)

    Can you run this code in you console?
  • 0
    Mhm, I believe it would still return false.

    0 is the only number which is interpreted as false, which would return true in this case, every other number would be interpreted as true, which would be returned as false.

    Or am I wrong?

    count == 0
    And
    !count

    Should have the same effect in this specific case.

    @Letmecode
  • 0
    @RazorSh4rk Adding redundancy to code doesn't make it easier to read. Unless (in this case) the reader doesn't know that "count == 0" is a boolean expression, but then he should learn programming before attempting to read code.
  • 1
    return !count;
  • 1
    @uuppi Depends on whether you intend to add more checks later. If it's just a helper method (meant to be used more than once), then it's fine with "return count == 0" or rather "return count === 0" so that it fails if you pass it the wrong type.
  • 1
    @uuppi There is nothing wrong with return count == 0 && !getSomething()

    As long as it can fit in one line its readable. Once you start having more conditions or you start feeling the need to split them into more lines thats when helper variable is good. ☺
Add Comment