90

I really hate people who prefer this coding style:

if (condition)
{
// something shitty here
}

Instead of this:

if(condition){
// perfectly clean code
}

Comments
  • 21
    I do understand your point, although I gave Up discussing this with colleagues.

    But still both are way better than
    If(condition)
    //creepy oneliner
  • 0
    @busuu lol
  • 19
    Much worse:
    if (condition == true)
  • 1
    @busuu It seems great minds think alike ;)
  • 10
    I personally prefer having spaces around the condition in parentheses:

    if (condition) {
    ...
    }

    And I love oneliners without curved brackets for cases like this:

    if (first check)
    throw new Exception(...);

    if (second check)
    throw new Exception (...);

    ...
  • 1
    @runfrodorun In this situation, the ambiguity is caused by an ambiguous variable name. If for example the value of "state" refers to something being active or inactive, "active" or "isActive" would be a much better choice.

    But I agree, there might be situations where this could be appropriate.
    I was originally referring to people who use this style for every single expression, simply because they don't actually understand what a boolean expression is.
  • 3
    I'm the opposite here. I used to write the second style in PHP, and still do

    However, in my C# projects the first style just sticks
  • 1
    I bet you're the kinda developer that would comment on a PR that it should be done the other way.

    So long as it works. To each their own.
  • 5
    @itsdaniel0 Yeah, whenever you try to change it Visual Studio tells you to fuck off and reverts back.
  • 1
    @Salkir you should start a protest 😂😂
  • 1
    @JTBringe I think that's actually changeable under environment settings
  • 4
    @Salkir Kill him and become the new team leader. Then you'll be the one to make all the rules.
    If it works for the Sith, it should work for you.
  • 0
  • 0
    @itsdaniel0 Thanks!
    I'll look for it the next time I open VS.
  • 0
    @busuu I was taught the second way, and I like the second way.
  • 0
    @runfrodorun I agree with everything you said. From a C perspective, your issues with my statements certainly seem reasonable.
    At the moment, I mostly have to work with (and think in) Java, because of university and work.
  • 1
    Well, fuck you, too.
  • 0
    @nordlicht

    Is there a reason why you wouldn't use a switch case for the second thing?
  • 0
    @runfrodorun the java compiler can do a lot of amazing optimisations, as long as the data you provide has a "pattern" and there ain't to much multithreading going on.

    My favorite example of a java compiler going nuts is still
    ```
    boolean check = true;
    while (check) {
    ...
    }
    ```

    Will compile to something like
    ```
    while (true) {
    ...
    }
    ```

    If it doesn't find a `check = false;` assignment within the loop.
  • 0
    @busuu how is that easier to read clearly the second way is ? 🤔
  • 0
    Hate is a strong word.
  • 1
    Pftt, new lines are for amateurs

    If(x==5){x++;}
  • 4
    @t04glovern pfft get on my level

    if
    (
    x
    ==
    5
    )
    x++
    ;
  • 3
    I like spaces.

    if (condition) {

    }

    You can lint your team's code so it is consistent all throughout.
  • 1
    That's why so many way to write, right? Let them code in the way they like. Developing is creating something, i believe. It's okay to have some view exact opposite of yourself. Hate is such a strong word.
  • 0
    Ok but why are the people who write the first way perfectly ok with wasting a line every condition and function? Do you want to see less of your code at a time?
  • 2
    First one is PSR-2 Standard. Better follow that standard when you are work as team. :-)
  • 0
    @theorion you mean better follow that standard if your team work with that one. And I don't think first one is psr-2.
    Most of the time (most common) :

    function bar()
    {
    //code
    }

    And (if case)

    if (/*condition*/) {
    //code
    }

    (Check php-fig.org or just use plugins on your favorite ide/text editor to help you)
  • 1
    I think most people would prefer perfectly clean code to something shitty but for the style just use the standard.

    Look online at any official c# documentation and you will see the first being used. Look at javascript documentation you will see the 2nd.

    if
    (
    notFollowingStandardConvention
    ){ console.log
    (
    'why?'
    )}
  • 2
    @3141
    This evil is common in my class:
    If (condition) return true;
    Else return false;
  • 1
    I'm like a compiler and doesn't really care which one.
  • 0
    @juchiast I see that one way too often, sometimes even as:

    if (condition == true) {
    return true;
    } else {
    return false;
    }

    IntelliJ IDEA is actually able to convert that into

    return condition;

    with a single keyboard shortcut. That's the only thing keeping me sane when I have to work with certain people's code.
  • 0
    I actually learned the second one first and thought the same but then when a professor said a reason why he does it, it simple made more sense. It's easier to see if one is missing and I couldn't find a reasoning similar for the second you mentioned so from then on I started doing like the first one
  • 0
    Hate me
  • 0
    Because with big if statements i need to look to the whole condition before i see the { .

    Sometimes you just want to see where the condition ends, and the first one is the best for this.
  • 1
    This seems to come up a lot.

    I'm totally with OP and @loserboi and @3141
  • 0
    @3141 but what happens when you check a variable in JavaScript with the value 0. Null, 0,undefined all convert to false, this can easily cause bugs, thats why you should use if(x === false) in JavaScript.

    var x = 5 - 5;
    If(x) {} would not be executed for example.
  • 0
  • 0
    @3141 if(condition)
  • 1
    While I prefer that way too with minor variation(extra space), the syntax is perfectly valid to the compiler and whatever works works. If any little thing bothers you that says something about you more than it does about the thing. Let all write how they want and let yourself just format their code if you need to review it as most tools can do this for you.

    Not doing that is like complaining about how your feet hurt because of all the sharp things on the ground. Why don't they remove all the sharp things off the ground? Instead, why don't you just put on some decent shoes? Do your own damn formatting however you like it.
  • 1
    I totally agree with you m8! :)
Add Comment