Jump to content

  • Log In with Google      Sign In   
  • Create Account


My OLD Syntax

  • You cannot reply to this topic
83 replies to this topic

#1 TrentonK   Members   -  Reputation: 108

Like
0Likes
Like

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.



Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2380

Like
3Likes
Like

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.


Waterlimon (imagine this is handwritten please)


#3 Servant of the Lord   Crossbones+   -  Reputation: 17272

Like
12Likes
Like

Posted 01 August 2013 - 10:39 AM

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' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

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.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#4 TheChubu   Crossbones+   -  Reputation: 3760

Like
6Likes
Like

Posted 01 August 2013 - 11:46 AM

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


#5 Net Gnome   Members   -  Reputation: 769

Like
7Likes
Like

Posted 01 August 2013 - 12:33 PM

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;

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


#6 Brother Bob   Moderators   -  Reputation: 7787

Like
6Likes
Like

Posted 01 August 2013 - 12:51 PM

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


#7 Sik_the_hedgehog   Crossbones+   -  Reputation: 1495

Like
1Likes
Like

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.

#8 samoth   Crossbones+   -  Reputation: 4523

Like
5Likes
Like

Posted 01 August 2013 - 12:57 PM

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



#9 Cornstalks   Crossbones+   -  Reputation: 6966

Like
-1Likes
Like

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

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 ]

#10 stupid_programmer   Members   -  Reputation: 1023

Like
0Likes
Like

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.



#11 samoth   Crossbones+   -  Reputation: 4523

Like
0Likes
Like

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.


#12 TheChubu   Crossbones+   -  Reputation: 3760

Like
3Likes
Like

Posted 01 August 2013 - 01:40 PM

40231999.jpg


"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


#13 Servant of the Lord   Crossbones+   -  Reputation: 17272

Like
0Likes
Like

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' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

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.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#14 Sik_the_hedgehog   Crossbones+   -  Reputation: 1495

Like
3Likes
Like

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.

#15 ultramailman   Prime Members   -  Reputation: 1558

Like
13Likes
Like

Posted 01 August 2013 - 02:37 PM

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


#16 MarkS   Prime Members   -  Reputation: 875

Like
0Likes
Like

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



#17 Cornstalks   Crossbones+   -  Reputation: 6966

Like
1Likes
Like

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

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 ]

#18 MarkS   Prime Members   -  Reputation: 875

Like
1Likes
Like

Posted 01 August 2013 - 04:15 PM

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



#19 Bacterius   Crossbones+   -  Reputation: 8188

Like
2Likes
Like

Posted 01 August 2013 - 04:22 PM

Curly brace arguments. Keeping programmers busy since 1969. tongue.png

 

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


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

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#20 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

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 ]





PARTNERS