View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# My OLD Syntax

83 replies to this topic

### #1TrentonK  Members

Posted 01 August 2013 - 10:21 AM

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.

### #2Waterlimon  Members

Posted 01 August 2013 - 10:26 AM

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.

o3o

### #3Servant of the Lord  Members

Posted 01 August 2013 - 10:39 AM

POPULAR

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;

.

.

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. I use the former "bad" example, or else the syntax I posted here. There are a dozen threads on GameDev.net debating the merits of each one if you want to learn more. Also, this would be called "coding style", not "syntax". Both of those share the same "syntax", but use different "styles".

Edited by Servant of the Lord, 01 August 2013 - 10:43 AM.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #4TheChubu  Members

Posted 01 August 2013 - 11:46 AM

POPULAR

Pff, this is my syntax:

if kbchk(vkl) x-=5; if kbchk(vkr) x+=5; if kbchk(vku) y-=5; if kbchk(vkd) y+=5;

79 characters, right under the limit.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

### #5Net Gnome  Members

Posted 01 August 2013 - 12:33 PM

POPULAR

i guess you could get crafty and make this work:

x+=vk==vkl?5:vk==vkr?-5:0;y+=vk==vku?-5:vk==vkd?5:0;


[realized i could compress it a bit more ;)]

if your compiler allows logical and-ing ints: you can shorten it to this:

x+=vk&vkl?-5:vk&vkr?5:0;y+=vk&vku?-5:vk&vkd?5:0;


Edited by Net Gnome, 01 August 2013 - 01:29 PM.

### #6Brother Bob  Moderators

Posted 01 August 2013 - 12:51 PM

POPULAR

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


### #7Sik_the_hedgehog  Members

Posted 01 August 2013 - 12:53 PM

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, 01 August 2013 - 12:54 PM.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

### #8samoth  Members

Posted 01 August 2013 - 12:57 PM

POPULAR

That's my syntax:

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;

Oh wait...

### #9Cornstalks  Members

Posted 01 August 2013 - 01:01 PM

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, 01 August 2013 - 01:03 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #10stupid_programmer  Members

Posted 01 August 2013 - 01:07 PM

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.

### #11samoth  Members

Posted 01 August 2013 - 01:09 PM

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, 01 August 2013 - 01:10 PM.

### #12TheChubu  Members

Posted 01 August 2013 - 01:40 PM

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

### #13Servant of the Lord  Members

Posted 01 August 2013 - 01:53 PM

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.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #14Sik_the_hedgehog  Members

Posted 01 August 2013 - 02:01 PM

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.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

### #15ultramailman  Prime Members

Posted 01 August 2013 - 02:37 PM

POPULAR

and mine:

#define IF if(
#define THEN )
#define DO {
#define ENDIF }
#define ELSE }else

IF keyboard_check(vk_left) THEN
DO
x -= 5;
ELSE IF keyboard_check(vk_right) THEN
DO
x += 5;
ENDIF


### #16MarkS  Members

Posted 01 August 2013 - 03:36 PM

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...

### #17Cornstalks  Members

Posted 01 August 2013 - 03:46 PM

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.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #18MarkS  Members

Posted 01 August 2013 - 04:15 PM

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

### #19Bacterius  Members

Posted 01 August 2013 - 04:22 PM

Curly brace arguments. Keeping programmers busy since 1969.

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

Edited by Bacterius, 01 August 2013 - 04:23 PM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #20Cornstalks  Members

Posted 01 August 2013 - 04:38 PM

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.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]