My OLD Syntax

Started by
79 comments, last by 21st Century Moose 10 years, 5 months ago

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.

Advertisement

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

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

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

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;

destroys all readability though ;)

[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;

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

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.

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.

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

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

Some people get religious about the bracing style. Meh. Same with the parentheses.
[size=2][ 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 ]

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.

This topic is closed to new replies.

Advertisement