public void doesSomething(int x){
if (x == 0){
// stuff
}
else{
//other stuff
}
}
public void doesSomething(int x)
{
if (x == 0)
{
// stuff
}
else
{
//other stuff
}
}
public void doesSomething(int x){
if (x == 0){
// stuff
}
else{
//other stuff
}
}
public void doesSomething(int x)
{
if (x == 0)
{
// stuff
}
else
{
//other stuff
}
}
public void doesSomething( int x ) { if ( x ) { // other stuff } else { // stuff } }
public void doesSomething(int x) { if (x) { // stuff } else { // other stuff }}for (int i= 0; i < 10; ++i) if this and that related to i do something with i;public void doesSomething(int x) { if (x) { // stuff } else { // other stuff }}x=0;if (cond) x=1;//skip first element{ for (;x<10;x++) //something done here}Quote:
Original post by richardurich
...
int getSomeElement(){ int i; for (i=0;i<10;i++) { if (i==5) return i; }}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; }}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.
public void DoSomething(){}public void DoSomething(){ ArrayList al = GetMeAFancyList(); foreach (object item in al) { }}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.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}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.
control body//// thus:if (foo) bar;//// or:if (foo){ bar; baz;}//// Also, doingif (foo) { bar; baz;}//// is likely going to cost you more keystrokes to modify.// The only other really defensible (aside from "this is the // way I like it/got used to it") style would be:if (foo) { bar; baz; }This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more