Page 5 of 5

Re: Blank lines height

Posted: Tue Apr 19, 2011 7:28 pm UTC
by 31eee384
I do this:

Code: Select all

for(;;)
{   
   while()
   {      
      if()
      {      
         doIt();   
      }
   }
}

Adds extra space to the beginning of each block (without just being an empty line). Occasionally I'll add a newline to one of the closing braces (to show the difference between the last if in a loop and the end of a loop, for example) but normally I find it looks just fine like above.

[EDIT: Heh, didn't realize that this was my first post. I swear I had posted before.... This thread didn't have any replies when I first posted, so my post was made without any of the information in the replies. I'm of the camp "it doesn't matter, just be consistent (and don't use GNU style)".]

[EDIT 2: OH! The thread I posted in was moved into the religious wars whitespace/braces thread. That makes a lot more sense.]

Re: The 1 true style

Posted: Thu Apr 21, 2011 1:42 am UTC
by ochuckles
I like vertical alignment because it makes it so you can see the stack. Also what about when you just put a { without a control structure before it? Like if you're in straight-C and want to make a temporary variable. WHAT THEN?

Re: The 1 true style

Posted: Thu Apr 21, 2011 1:48 am UTC
by masher
ochuckles wrote: Also what about when you just put a { without a control structure before it? Like if you're in straight-C and want to make a temporary variable. WHAT THEN?


The { goes at the current indentation level, and then you indent inside those brackets.

Code: Select all

class Class
{
   some code
   
   if(x)
   {
      do this
   }
   
   {
      int temp = 1;      
      do some things with temp
   }
   
   now we're back to normal
}

Re: The 1 true style

Posted: Thu Dec 29, 2011 9:44 pm UTC
by Inglonias
Just want to say that I've used Whitesmiths simply because that was the style I was taught in. I went to a summer camp where the programming was taught in MicroWorlds, and the instructor did all of his stuff in that style, so that's what I use.

Re: The 1 true style

Posted: Wed Jan 11, 2012 3:08 pm UTC
by prolog
ANSI.

Code: Select all

void do_magic()
{
  bool check = condition();
  if (condition)
  {
    global_var = getRandomNumber(); // Chosen by a fair die roll; guaranteed to be random
  }
}


I use spaces, indentation is 2. This has been my coding style ever since high school. My company uses the same coding style, which is a very happy coincidence.

Re: The 1 true style

Posted: Mon May 21, 2012 2:55 am UTC
by Anubis
I use what the wiki article (http://en.wikipedia.org/wiki/Indent_style) refers to as "Compact Control Readability style", though I had never heard it called that until today. Basically it's just a slightly modified Java style where else statements do not go on the same line as the closing brace of the preceding if. Wikipedia says:

This style makes it easy to skim the left edge of the code for control statements (whereas styles like 1TBS make statements such as "else" harder to see because they are after an end bracket on the same line). However it keeps the code more compact than styles like the Allman style, by putting opening brackets at the end of lines (as opposed to on their own lines).


and that's exactly what I like about it.

Code: Select all

private void determineBestStyle(int styleChoice) {
     if (styleChoice == CCR) {
           println("Obviously!");
     }
     else {
           throw new ErrorException("This could never be the case");
     }
}

Re: The 1 true style

Posted: Mon May 21, 2012 4:21 am UTC
by EvanED
Anubis wrote:I use what the wiki article (http://en.wikipedia.org/wiki/Indent_style) refers to as "Compact Control Readability style"

I used to be an Allman guy, but I've basically switched to something like that compact style. There are a couple exceptions: I don't usually put the opening brace of a function on the same line as the signature, and if the control statement itself is multi-line, then the brace gets its own line (personally I think you're insane if you don't do this). Thus:

Code: Select all

for (std::vector<int>::const_iterator whatever = container.begin();
     whatever != container.end(); ++whatever)
{
   blah(*whatever);
}

is common.

Re: The 1 true style

Posted: Wed May 23, 2012 3:40 am UTC
by Anubis
EvanED wrote:
Anubis wrote:I use what the wiki article (http://en.wikipedia.org/wiki/Indent_style) refers to as "Compact Control Readability style"

I used to be an Allman guy, but I've basically switched to something like that compact style. There are a couple exceptions: I don't usually put the opening brace of a function on the same line as the signature, and if the control statement itself is multi-line, then the brace gets its own line (personally I think you're insane if you don't do this).


You think I'm insane then... I usually just leave a blank line after the control statement in that case, like so:

Code: Select all

for (std::vector<int>::const_iterator whatever = container.begin();
     whatever != container.end(); ++whatever) {

   blah(*whatever);
   otherFunction();
   thisDoesSomething();
}


For some reason an opening brace on a line by itself just looks really wrong to me.

Re: The 1 true style

Posted: Wed May 23, 2012 5:07 am UTC
by EvanED
That's pretty reasonable... the point is just to clearly delineate the loop header or condition from the block that it surrounds. Especially with 4-space indents (the one true amount :mrgreen:) they can really blend together:

Code: Select all

for (std::vector<int>::const_iterator whatever = container.begin();
     whatever != container.end(); ++whatever) {
     blah(*whatever);
     otherFunction();
     thisDoesSomething();
}

That said, the blank line in your code looks "wrong" to me. :-)

Oh, and in the rare case of opening a block of code that isn't the body of an if or loop, then putting the opening brace on a line by itself is about the only reasonable choice. (You might do this if you want to limit the scope of a variable -- particularly in C++, to get the destructor to run at a given time.)

Re: The 1 true style

Posted: Wed May 23, 2012 6:54 pm UTC
by Thesh
EvanED wrote:That's pretty reasonable... the point is just to clearly delineate the loop header or condition from the block that it surrounds. Especially with 4-space indents (the one true amount :mrgreen:) they can really blend together:

Code: Select all

for (std::vector<int>::const_iterator whatever = container.begin();
     whatever != container.end(); ++whatever) {
     blah(*whatever);
     otherFunction();
     thisDoesSomething();
}

That said, the blank line in your code looks "wrong" to me. :-)

Oh, and in the rare case of opening a block of code that isn't the body of an if or loop, then putting the opening brace on a line by itself is about the only reasonable choice. (You might do this if you want to limit the scope of a variable -- particularly in C++, to get the destructor to run at a given time.)


You can do a one tab indent for the body and two for the continued part of the control statement in that case, but it still looks better with one tab each in allman.

Re: The 1 true style

Posted: Thu Jul 19, 2012 1:04 am UTC
by sam_i_am

Code: Select all

         public class MyClass
         {
      public static int foo()
      {
   int sum;
   for(int i=0; i<10; i++)
   {
sum += i;
   }
   return sum;
      }
         }

Re: The 1 true style

Posted: Thu Jul 19, 2012 7:39 am UTC
by EvanED
sam_i_am wrote:

Code: Select all

         public class MyClass
         {
      public static int foo()
      {
   int sum;
   for(int i=0; i<10; i++)
   {
sum += i;
   }
   return sum;
      }
         }

I feel like someone should submit a patch on April 1st to the Linux kernel list or something changing the indentation style to that.

Re: The 1 true style

Posted: Thu Jul 26, 2012 8:39 pm UTC
by Quizatzhaderac
Shaolin is the one true style. Context be damned.

I vote for K&R with sam_i_am's reversed indenting.

Re: The 1 true style

Posted: Wed Aug 15, 2012 12:43 pm UTC
by sourmìlk
I don't actually care. I haven't really come across any commonly used style that I find unreadable. For years I used a K&R-ish style, like so:

Code: Select all

for(int i = 0; i < max; i++) {
    if (condition == true) {
        doSomething();
        break;
    } else {
        doSomethingElse();
    }
}


And then I got a job that required that I put braces on new lines, and so I started doing that.

Don't tell my dad that I'm cool with any style though. He'll kill me if he finds out I'm okay putting braces on new lines.

Re: The 1 true style

Posted: Wed Aug 15, 2012 2:49 pm UTC
by Роберт
sam_i_am wrote:

Code: Select all

         public class MyClass
         {
      public static int foo()
      {
   int sum;
   for(int i=0; i<10; i++)
   {
sum += i;
   }
   return sum;
      }
         }

That's perfect, because you really notice it if you code badly and end up with too many levels of indentation.

Re: The 1 true style

Posted: Wed Aug 15, 2012 3:03 pm UTC
by EvanED
sourmìlk wrote:I don't actually care. I haven't really come across any commonly used style that I find unreadable.

Bah! What kind of religious wars post is this? "Yeah, everything's cool man! You're cool, I'm cool, we're all cool!" :wink:

Nah, I'm actually about the same, with a couple specific exceptions like the "loop header continuing to a second or subsequent line" than I mentioned above (where putting the { on a separate line really does help).

Re: The 1 true style

Posted: Wed Aug 15, 2012 3:34 pm UTC
by sourmìlk
EvanED wrote:
sourmìlk wrote:I don't actually care. I haven't really come across any commonly used style that I find unreadable.

Bah! What kind of religious wars post is this? "Yeah, everything's cool man! You're cool, I'm cool, we're all cool!" :wink:

I don't know. Apparently I'm argumentative everywhere except the one forum specifically for arguments.

Nah, I'm actually about the same, with a couple specific exceptions like the "loop header continuing to a second or subsequent line" than I mentioned above (where putting the { on a separate line really does help).

There are certainly things that make code unreadable that I'm not really okay with, but in general I haven't really come across those things.

Re: Blank lines height

Posted: Sat Sep 15, 2012 1:45 am UTC
by Brickmack
31eee384 wrote:I do this:

Code: Select all

for(;;)
{   
   while()
   {      
      if()
      {      
         doIt();   
      }
   }
}



This is basically what I normally use. But I dont much care what others use, its all cool. Except for this, this just looks... annoying... to me. Its all backwards.

sam_i_am wrote:

Code: Select all

         public class MyClass
         {
      public static int foo()
      {
   int sum;
   for(int i=0; i<10; i++)
   {
sum += i;
   }
   return sum;
      }
         }

Re: The 1 true style

Posted: Sun Sep 16, 2012 8:47 am UTC
by troyp
sourmìlk wrote:I don't actually care. I haven't really come across any commonly used style that I find unreadable.


I don't really find any unreadable either, but there is a convenience factor in having code spaced out how you like it.

I use K&R style and while I find others readable, I find "line for the open braces" styles wasteful of screen space and "open brace left of code" styles a bit inconvenient for editing. Also, I do find K&R-like styles the most readable, although it's not a huge thing (it's also nice that K&R-style code looks a lot like Python layout).

Another thing is that I think differences between styles are ameliorated by syntax highlighting. If I had to read monochrome code a lot, I'd be a lot more fussy about style.

EvanED wrote:Nah, I'm actually about the same, with a couple specific exceptions like the "loop header continuing to a second or subsequent line" than I mentioned above (where putting the { on a separate line really does help).

Maybe I should start doing this. I hate those multiline loop conditions (sometimes I even define booleans just to get rid of them).

Re: The 1 true style

Posted: Sat Feb 08, 2014 9:51 pm UTC
by ucim
There's only one proper way to format code, and it's more or less secret. This is because the illuminati don't want the hoi polloi to gain the awesomeness that such formatting entails. But now, you can dominate your machine with this one weird trick!

Code: Select all

if (something)
{  do this thing;
   while (not done yet)
   {  drum your fingers;
      sit by a lake;
   }
   do one last thing;
}
else
{  if (this happened)
   {  do something odd;
      do something even;
   }
   else
   {  go for a walk;
   }
}
This has all the advantages of the aligned braces, and collapses useless spaces so that you can see more on the screen at once. Clearly there's no better way.

Jose

Re: Blank lines height

Posted: Fri Feb 14, 2014 8:36 pm UTC
by untitled
Brickmack wrote:
31eee384 wrote:I do this:

Code: Select all

for(;;)
{   
   while()
   {      
      if()
      {      
         doIt();   
      }
   }
}



This is basically what I normally use. But I dont much care what others use, its all cool. <SNIP>


Me too. I didn't even know it had a name till I saw this topic (Whitesmith? Sounds cool!). It's very clear, concise and logical, even though it tends to take up a lot of vertical space (all those lines with just one symbol... if I had to print it, I would probably sink into a depression about dead trees and stuff).

I must also mention that I generally write short and relatively complicated programs so this style helps my dyslexia A LOT.

Re: The 1 true style

Posted: Fri Feb 14, 2014 11:07 pm UTC
by EvanED
That's not Whitesmiths. Whitesmiths would be

Code: Select all

for(;;)
    {   
    while()
        {     
        if()
            {     
            doIt();   
            }
        }
    }

Re: The 1 true style

Posted: Sat Feb 15, 2014 2:23 am UTC
by untitled
thank you, I see the difference... although one could be converted to the other very easily...

Re: The 1 true style

Posted: Sat Feb 15, 2014 7:08 pm UTC
by Flumble
ucim wrote:This is because the illuminati don't want the hoi polloi

The the people?
Perhaps you should learn (Ancient) Greek instead of a programming language. :P

I dislike every style that incorporates a closing brace followed by any keyword* on the same line.

In my world every block starts with an identifier, followed by a space, followed by an opening brace, followed by indented statements on the next lines, followed by a closing brace on the next line:

Code: Select all

function foo(bar) {
  return bar;
}

var foo2 = (function(bar) {
  if (bar)
    return bar;
  else {
    bar = "string";
    //TODO: perform another very useful statement here
    return bar;
  }
}); //I'm used to the (function(<args>){<body>}) construct due to jquery and mootools and the likes



*I were to say "anything", but then I remembered javascript with stuff like /}\)?[,;])$/.

Re: The 1 true style

Posted: Wed Jul 30, 2014 10:33 pm UTC
by Genuinely, a prune
Unfortunately, my style is better than all of yours:

Code: Select all

void main(){
   if ("K_R"=="acceptable"){
     print("erm.");
   } else{
     print("That's better.");
   }
}

Re: The 1 true style

Posted: Thu Jul 31, 2014 12:51 am UTC
by Nyktos
Genuinely, a prune wrote:

Code: Select all

   } else{
That's the worst thing I've ever seen.

Re: The 1 true style

Posted: Thu Jul 31, 2014 10:31 am UTC
by Genuinely, a prune
Oh? I thought it was the best.

Re: The 1 true style

Posted: Thu Jul 31, 2014 2:06 pm UTC
by Yakk
Nyktos wrote:
Genuinely, a prune wrote:

Code: Select all

   } else{
That's the worst thing I've ever seen.

Code: Select all

}else{

or

Code: Select all

} else {

never

Code: Select all

}else {

or

Code: Select all

} else{

Re: The 1 true style

Posted: Thu Jul 31, 2014 5:32 pm UTC
by cphite
I only opened the thread because I thought it might be about martial arts...

But regarding the bracket thing, it's this:

Code: Select all

if something
    {
         do this
    }
    else
    {
         do this other thing
    }


Anything else is wrong.

Re: The 1 true style

Posted: Fri Aug 01, 2014 4:00 am UTC
by Nyktos
Yakk wrote:

Code: Select all

}else{

or

Code: Select all

} else {
As far as I'm concerned, anything besides

Code: Select all

}
else {
is wrong, but either of those is definitely leagues ahead of the mismatched one.

Re: The 1 true style

Posted: Fri Aug 01, 2014 3:08 pm UTC
by Dason
Nyktos wrote:As far as I'm concerned, anything besides

Code: Select all

}
else {
is wrong, but either of those is definitely leagues ahead of the mismatched one.

I don't like this format. In R this syntax isn't even always valid.

Re: The 1 true style

Posted: Fri Aug 01, 2014 3:35 pm UTC
by Yakk
Sometimes, you just don't want to burn vertical space on else.

Re: The 1 true style

Posted: Fri Aug 01, 2014 6:13 pm UTC
by Quizatzhaderac
Nyktos wrote:As far as I'm concerned, anything besides

Code: Select all

}
else {
I agree.

Re: The 1 true style

Posted: Fri Aug 01, 2014 7:13 pm UTC
by Nyktos
Yakk wrote:Sometimes, you just don't want to burn vertical space on else.

As far as I'm concerned, it's really the closing brace that's wasting space. The word "else" is actually meaningful and deserves to begin a line.

Re: The 1 true style

Posted: Fri Aug 01, 2014 7:51 pm UTC
by Flumble
Nyktos wrote:
Yakk wrote:Sometimes, you just don't want to burn vertical space on else.

As far as I'm concerned, it's really the closing brace that's wasting space. The word "else" is actually meaningful and deserves to begin a line.

Partly. The closing brace matches the prefix of the block by its indentation (assuming that the OTS indents the statements inside a block and not the block itself), so you can quickly see where the block ends.
That said, I do like Python's indent-instead-of-brace style. (not that I ever used Python. Haskell instead)

Re: The 1 true style

Posted: Fri Aug 01, 2014 9:13 pm UTC
by Nyktos
Flumble wrote:The closing brace matches the prefix of the block by its indentation (assuming that the OTS indents the statements inside a block and not the block itself), so you can quickly see where the block ends.
Sure, but the word "else" also tells you that the if-block is over. I like Python a lot, including its syntax, but you don't actually need anything that radical for the case of an if/else. There are languges that have a syntax along the lines of:

Code: Select all

if x == y
    do_whatever()
else
    do_other_thing()
end if

Re: The 1 true style

Posted: Sat Aug 16, 2014 12:49 am UTC
by chasesan
I use 1TBS or "the one true brace style", but with attached function braces. It is a variant of K&R. But you always add curly braces even around single statements. Basically allowing safe line insertion anywhere in the file.

Re: The 1 true style

Posted: Thu Jan 05, 2017 5:21 pm UTC
by rciafardone
This is a no debate. Each one does the one that they like the most.
Allman is unambiguously clearer.
K&R has less strokes and is vertically cheaper.

I use Allman. but if I have to print the code on dead trees is a good idea to switch to K&R. Those extra lines do add up a lot.

Everything else are just visual variations without much consequence that may or may not appeal to someone.

Automatic tools will solve all conflicts.

Re: The 1 true style

Posted: Thu Jan 05, 2017 5:26 pm UTC
by rciafardone
Hammer wrote:For myself, I like Allman. However, my main requirement for style is that, whatever you decide to do, do it consistently! I regularly get handed code from beginners whose reaction to suggestions to maintain a consistent style is "Do I haaaaave to?" Then, after listening to them whine, I get to plow through code that uses three different indenting styles selected at random when they bother to indent at all. Grrrrrrrrr...


That smells like someone is copy-pasting for many different places instead of doing their own stuff... not that there is anything wrong with that, code posted on the wild is meant to be copied, and why should I reinvent the wheel?, BUT not even caring about reformatting it means usually they didn't even care to read and understand the code. When I copy code I always read it to try to understand how it works and always reformat it on my own style while i read it, and usually change the variable names if I don't like them.