# My OLD Syntax

This topic is 1531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I still remember how I used to program. My syntax was horrible:

if keyboard_check(vk_left)
{
x-=5;
}
if keyboard_check(vk_right)
{
x+=5;
}
if keyboard_check(vk_up)
{
y-=5;
}
if keyboard_check(vk_down)
{
y+=5;
}


This is how I do it now...So much better:

if (keyboard_check(vk_left)){
x -= 5;
}

if (keyboard_check(vk_right)){
x += 5;
}

if (keyboard_check(vk_up)){
y -= 5;
}

if (keyboard_check(vk_down)){
y += 5;
}


Just imagine hundreds or sometimes thousands of lines of code similar to the first one....what a nightmare lol.

#### Share this post

##### Share on other sites

And this is how you do it in the future:

if (keyboard_check(vk_left))
x -= 5;
if (keyboard_check(vk_right))
x += 5;
if (keyboard_check(vk_up))
y -= 5;
if (keyboard_check(vk_down))
y += 5;

Pay attention to the more compact form at the cost of flexibility.

#### Share this post

##### Share on other sites

Seriously though, incase the original poster isn't joking, both those are commonly used syntaxes, and some professionals advocate for one, and some professionals advocate for another, and neither is necessarily superior than the other.

Seriously? I don't think most people know that you can omit the parenthesis, they probably think it'd be a syntax error.

EDIT: it becomes handy with type casting though. Instead of (int)(x) you could write int(x). Looks nifty, and syntax highlight will still make sure it's obvious it's a casting. Probably still better avoided, though.

Edited by Sik_the_hedgehog

#### Share this post

##### Share on other sites

And once you've mastered C++, and have the official and secretive gold C++ guru medal around your neck, you code like this:

if(keyboard_check(vk_left))  x -= 5;
if(keyboard_check(vk_right)) x += 5;
if(keyboard_check(vk_up))    y -= 5;
if(keyboard_check(vk_down))  y += 5;

Now if there was a space between the if and the (, it'd be perfect ;)

@OP: it took me a good 5 seconds to even spot a difference between your two styles. The only difference between the two is a set of parentheses and Allman vs K&R bracing. The difference is minor. If you think your old style is horrible, then you must think your new style is at least slightly horrible too, seeing as the difference really is very minor.

Some people get religious about the bracing style. Meh. Same with the parentheses. Edited by Cornstalks

#### Share this post

##### Share on other sites

Personally, I greatly dislike the opening bracket on the same line of code.  Instead of just being able to scan horizontally for blocks of code now you must go horizontally until you see a closing bracket and then backtrack to find the open one instead of both being at the same tab stop.  I prefer Waterlimon's way of doing it.  But Servant's doesn't give me too much of a twitch.  I'm very anal about my code looking 'pretty' and well aligned.

#### Share this post

##### Share on other sites
Some people get religious about the bracing style. Meh. Same with the parentheses.

Though, admittedly not always without a reason, if you look at my snippet above (which is a real coding horror).

When I was first told "we always use braces, Allman style, even for single liners" long long long ago, my thought was "Bah...". Of course I didn't do such a stupid thing outside work, because it was just too silly. Guess what made me change my mind one day. Exactly... wasting 2 hours of time on trying to figure out why the program logic wasn't correct although it just had to be good -- everything was right. Except, well, you know.

Edited by samoth

#### Share this post

##### Share on other sites

Seriously though, incase the original poster isn't joking, both those are commonly used syntaxes, and some professionals advocate for one, and some professionals advocate for another, and neither is necessarily superior than the other.

Seriously? I don't think most people know that you can omit the parenthesis, they probably think it'd be a syntax error.

I meant the two @TrentonK in the original post - neither is worse than the other, and there's proponents for both styles.

Personally, I prefer is "old horrible nightmarish" style, for two super minor irrelevant reasons.
1) It's easier for me to spot if the first brace is missing.

2) It makes the code less cluttered-feeling by spacing it out more, making it easier for me to skim and comprehend code.

Whitespace is free. Monitors are large. Legibility is valuable.

#### Share this post

##### Share on other sites

Personally, I greatly dislike the opening bracket on the same line of code.  Instead of just being able to scan horizontally for blocks of code now you must go horizontally until you see a closing bracket and then backtrack to find the open one instead of both being at the same tab stop.

Doesn't indenting take care of this already anyway?

I meant the two @TrentonK in the original post - neither is worse than the other, and there's proponents for both styles.

I have yet to see somebody argue for not using parenthesis in an if clause (and I was talking about the original post).

2) It makes the code less cluttered-feeling by spacing it out more, making it easier for me to skim and comprehend code.

Curiously, it's the other way for me. Probably I see the condition as being part of the block of code, so having it look separate from the rest of the block feels off to me.

#### Share this post

##### Share on other sites

I meant the two @TrentonK in the original post - neither is worse than the other, and there's proponents for both styles.

I have yet to see somebody argue for not using parenthesis in an if clause (and I was talking about the original post).

I was waiting for that as well...

#### Share this post

##### Share on other sites

I meant the two @TrentonK in the original post - neither is worse than the other, and there's proponents for both styles.

I have yet to see somebody argue for not using parenthesis in an if clause (and I was talking about the original post).

I was waiting for that as well...

4 less keystrokes on a typical keyboard.

Whether you think that's a valid argument or not is another story, but I know people who value minimizing key strokes.

#### Share this post

##### Share on other sites

Is that legal syntax? I tried that in VS 2012 and it yelled at me.

#### Share this post

##### Share on other sites

Curly brace arguments. Keeping programmers busy since 1969.

Besides, the only correct brace style is the one I use.

Edited by Bacterius

#### Share this post

##### Share on other sites

Is that legal syntax? I tried that in VS 2012 and it yelled at me.

It looks like it's not, at least according to section 6.4 of the C++ standard. It defines selection-statements as:

selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
switch ( condition ) statement

The parentheses here are part of the definition of if and switch, so I would conclude that no, they are not optional.

#### Share this post

##### Share on other sites

The real WTF is that on my keyboard only 2 of the conditions can possibly be true at any one time.

Keyboard ghosting, FTW.

#### Share this post

##### Share on other sites

Syntax aside, what's actually concerning about the OP's before and after code, is that they're tying input processing directly to logic. Its well-enough for prototypes or small things, but it's probably gonna break-down right around the time you want to make the character jump, or do any kind of animation.

Be ye character? Or cursor?

#### Share this post

##### Share on other sites

If I'm going to use curly braces, then I want the open and closed characters nicely lined up vertically. Makes things nice and neat by grouping things in blocks.

But personally I just use Python these days and ignore these debates.

#### Share this post

##### Share on other sites

This topic is 1531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now