Popular coding style?

Started by
53 comments, last by GameDev.net 17 years, 8 months ago
In my code you may find things like:
//C
int getSomeElement(){  int i; for (i=0;i<10;i++)  {    if (i==5) return i;  }}


i use scopes in my cases for switch also

//C#
switch (i){  case 0:  {    ArrayList al = Database.Query("select * from mytable1");    foreach (object[] obj in al) {}    break;  }  case 1:  {    ArrayList al = Database.Query("select * from mytable2");    foreach (object[] obj in al) {}    break;  }}

To show i can now use the same variable in multiple cases...
Advertisement
Quote:Original post by richardurich
Most people I have encountered had an easier time learning with the second style, but everyone seems to eventually move to the first. I doubt it's always for the same reason I did, so maybe it's more information fitting on a single screen/page or just people adapting to the norm.


I actually started out with the second style, then moved to the first style for the same reason as you. But i recently went back to the second again. I trained myself to type the closing bracket of the scope first before writing the internals. I start like:
public void DoSomething(){}

and then i move on:
public void DoSomething(){  ArrayList al = GetMeAFancyList();  foreach (object item in al)  {  }}

now fill in the code of the foreach scope.
It helps me when i go many levels deep while writing lots of code (i had to do some calculations over a 9 dimension array once). Because before i did this i found myself ending my function with a load of closing brackets hoping for the best :)

Greetings.
I use the first style when coding in Java or C++, and the second when coding in C. It's just the way I was taught each and I don't really have trouble reading either, so I didn't feel the need to deviate from the style I was originally shown. I do like Python's no-brace syntax though.

As for changing your own coding style, I wouldn't recommend it unless you're doing so to meet a team or company standard. Just use whichever you feel comfortable with and make sure you're consistent. Each method is quite popular and will generally be fairly easy for most programmers to understand.

- Jason Astle-Adams

style #1 is good for its tendency to properly bind things together. Notice how the else statement tightly binds the two potential blocks together.
void doesSomething(int x) {    if (x) {        // stuff    } else {        // stuff    }}
With style #1, do/while loops are tightly integrated with the block structure.
void doesSomething(int x) {    do {        // stuff    } while (--x > 0);}
Under style #2, it becomes difficult to differentiate a do/while loop from an empty while loop; the integration is much looser.
void doesSomething(int x) {    do    {        // stuff    }    while (--x > 0);}
The same problem comes up with for loops, as richardurich mentioned. Under style #2, if you remove the for line, the code will remain meaningful, but with a different meaning. The code is written as if the for line were a sibling of the brace line while, as far as the meaning is concerned, they basically the same. Actually, the brace takes the place of the single expression after the conditional to create the multi-expression block, so it should either be indented or placed on the same line.
void doesSomething(int x){    for (int i; i < x; ++i) x--;    {        int x;        // stuff    }}
Orphaned scopes can be useful for avoiding pollution. With style #1, they are easy to recognize. Under style #2, they could easily be mistaken for a loop, as in the previous example.

However, single expressions after conditionals (is there a correct word for these?) are harder to recognize under style #1, especially if the conditional is really long.
void doesSomething(int x) {    if (x != 1 && ...) // pretend this conditional extends out of view        x = 0;    if (x != 1 && ...) { // pretend this conditional extends out of view        x = 0;    } // your only clue is the closing brace}


[Edited by - dcosborn on August 20, 2006 1:03:27 PM]
in my opinion, the first style just looks cluttered and a little less easily understood. i really dont know where that style came around from, i had never seen it until i started programming in Java, and even then it wasn't the way it was taught in my school.

it makes me think that it was possibly an adaptation of style from another language like visual basic, where code always starts on the next line, except for END statements (analgous to a closing bracket).
I use the second style at home, as I often use the bracelets to check scope. I always put an if statement code block on the next line to prevent the problem richardurich posted.
I use the first style at work, as it's the guideline I have to follow. It does result in shorter code and some may find that easier to read. It's not so bad now that I work with it, and although I still prefer the second style, I can see some reasons to use this one.

Yeah, it's just personal preference, or corporate guidelines. :)
Create-ivity - a game development blog Mouseover for more information.
Am I the only one using this hybrid?
public void doesSomething(int x){    if (x == 0) {	// stuff    } else {	//other stuff    }}

This way it is a layout-wise difference between structure (functions) and logic (if while do for etc) which I think makes the code easier to read.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Quote:Original post by Enselic
Am I the only one using this hybrid?
*** Source Snippet Removed ***
This way it is a layout-wise difference between structure (functions) and logic (if while do for etc) which I think makes the code easier to read.


I usually use that one.

____________________________________Spazuh- Because I've had too much coffee
Quote:Original post by Enselic
Am I the only one using this hybrid?
*** Source Snippet Removed ***
This way it is a layout-wise difference between structure (functions) and logic (if while do for etc) which I think makes the code easier to read.


I used to, but then I switched to something akin to the OPs #2.
You've gotta be flexable, many companies will have coding guidelines which will specify this sorta thing.

I used to use OP's #1, but where I've worked for the last 2 years uses OP's #2, so that's what I use now.

This topic is closed to new replies.

Advertisement