11

While I know I can save a few characters, I think that

if(username.length > 0 && password.length > 0)

looks a lot nicer than just

if(username.length && password.length)

However, I am so lazy that I will probably always use the uglier version just to save like 0.00001 seconds typing.

Comments
  • 4
    Think about code portability and readability.

    Someone from a strongly typed language like C# and Java won't immidiately understand the second one and can't just reuse that code without changing it.
  • 4
    @Highlight

    // Best of both worlds(???)
    if(username.length*password.length>0)
  • 4
  • 0
    How about !!username && !!password

    For js
  • 0
    @MrCSharp if I'm not mistaken... That will work in everything but Java and Python whom both have no implicit cast from integer to Boolean. My C# is not fine-tuned, however. I know for a fact it works in c and c++, which are both strongly typed and static languages.
  • 0
    @AlgoRythm nope, got error CS0019 operator & & cannot be applied to operands of type imt and int
  • 0
    @AlgoRythm and the same for Java.
  • 0
    @MrCSharp Like I said, my c# is out of practice and the little things escape me. But I know it works with the rest of the c family!
  • 0
    @MrCSharp I knew Java is a bitch about it. Unfortunate amount of experience I have with Java.

    here it is in c
  • 0
    @AlgoRythm if you do & & in C with any value other than 0 or 1, it will error out during compilation
  • 4
    @MrCSharp If your compiler does that, it doesn't follow the c standard. True is defined as any number other than 0, and false is defined as 0.

    Proof:
  • 1
    @AlgoRythm I see. I was using an online repl tool running clang 7 on ubuntu

    So maybe it is that but what you're saying about the c standard makes perfect sense 👌
  • 1
    @MrCSharp The internet is so fucking great for exchanging little blips of knowledge like this. Sometimes I almost even like the internet again.
  • 1
    @MrCSharp I believe I found the tool you were using. Perhaps a syntax error?
  • 2
    @MrCSharp Oh, you probably wrote constants! There are special rules for those. You can't literally write 69 && 420.

    ((THIS DEPENDS ON COMPILER. It works with the tool I am using - common theme with c))

    Programming is weird!
  • 1
    @AlgoRythm yes, that's the one. Though I was using constants inside the if expression : if(1&&5)

    Which is why it was failing.

    Cheers brah 😅
  • 0
    I always compare with 0, '\0' or NULL, depending on the type, and use the "direct" form only for logical values. It's much easier to read because it gives an immediate hint what type of check is being done.
Add Comment