The 1 true style

Please compose all posts in Emacs.

Moderators: phlip, Moderators General, Prelates

31eee384
Posts: 1
Joined: Tue Aug 24, 2010 2:17 pm UTC

Re: Blank lines height

Postby 31eee384 » Tue Apr 19, 2011 7:28 pm UTC

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

ochuckles
Posts: 8
Joined: Sun Nov 14, 2010 9:24 pm UTC

Re: The 1 true style

Postby ochuckles » Thu Apr 21, 2011 1:42 am UTC

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?

masher
Posts: 821
Joined: Tue Oct 23, 2007 11:07 pm UTC
Location: Melbourne, Australia

Re: The 1 true style

Postby masher » Thu Apr 21, 2011 1:48 am UTC

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
}

User avatar
Inglonias
Posts: 126
Joined: Mon Jun 28, 2010 5:54 pm UTC

Re: The 1 true style

Postby Inglonias » Thu Dec 29, 2011 9:44 pm UTC

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.

prolog
Posts: 3
Joined: Fri May 20, 2011 12:59 pm UTC

Re: The 1 true style

Postby prolog » Wed Jan 11, 2012 3:08 pm UTC

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.

User avatar
Anubis
Posts: 222
Joined: Sun Mar 01, 2009 7:59 am UTC

Re: The 1 true style

Postby Anubis » Mon May 21, 2012 2:55 am UTC

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");
     }
}

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The 1 true style

Postby EvanED » Mon May 21, 2012 4:21 am UTC

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.

User avatar
Anubis
Posts: 222
Joined: Sun Mar 01, 2009 7:59 am UTC

Re: The 1 true style

Postby Anubis » Wed May 23, 2012 3:40 am UTC

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.

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The 1 true style

Postby EvanED » Wed May 23, 2012 5:07 am UTC

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

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 5246
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: The 1 true style

Postby Thesh » Wed May 23, 2012 6:54 pm UTC

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.
Honesty replaced by greed, they gave us the reason to fight and bleed
They try to torch our faith and hope, spit at our presence and detest our goals

User avatar
sam_i_am
Posts: 623
Joined: Mon Jun 18, 2012 3:38 pm UTC
Location: Urbana, Illinois, USA

Re: The 1 true style

Postby sam_i_am » Thu Jul 19, 2012 1:04 am UTC

Code: Select all

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

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The 1 true style

Postby EvanED » Thu Jul 19, 2012 7:39 am UTC

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.

User avatar
Quizatzhaderac
Posts: 1484
Joined: Sun Oct 19, 2008 5:28 pm UTC
Location: Space Florida

Re: The 1 true style

Postby Quizatzhaderac » Thu Jul 26, 2012 8:39 pm UTC

Shaolin is the one true style. Context be damned.

I vote for K&R with sam_i_am's reversed indenting.
The thing about recursion problems is that they tend to contain other recursion problems.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: The 1 true style

Postby sourmìlk » Wed Aug 15, 2012 12:43 pm UTC

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.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Роберт
Posts: 4285
Joined: Wed May 14, 2008 1:56 am UTC

Re: The 1 true style

Postby Роберт » Wed Aug 15, 2012 2:49 pm UTC

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.
The Great Hippo wrote:[T]he way we treat suspected terrorists genuinely terrifies me.

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The 1 true style

Postby EvanED » Wed Aug 15, 2012 3:03 pm UTC

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

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: The 1 true style

Postby sourmìlk » Wed Aug 15, 2012 3:34 pm UTC

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.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Brickmack
Posts: 58
Joined: Wed Sep 01, 2010 1:48 am UTC

Re: Blank lines height

Postby Brickmack » Sat Sep 15, 2012 1:45 am UTC

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

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: The 1 true style

Postby troyp » Sun Sep 16, 2012 8:47 am UTC

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

User avatar
ucim
Posts: 5091
Joined: Fri Sep 28, 2012 3:23 pm UTC
Location: The One True Thread

Re: The 1 true style

Postby ucim » Sat Feb 08, 2014 9:51 pm UTC

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
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

User avatar
untitled
Posts: 39
Joined: Wed Jan 29, 2014 9:23 pm UTC

Re: Blank lines height

Postby untitled » Fri Feb 14, 2014 8:36 pm UTC

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.

EvanED
Posts: 4314
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: The 1 true style

Postby EvanED » Fri Feb 14, 2014 11:07 pm UTC

That's not Whitesmiths. Whitesmiths would be

Code: Select all

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

User avatar
untitled
Posts: 39
Joined: Wed Jan 29, 2014 9:23 pm UTC

Re: The 1 true style

Postby untitled » Sat Feb 15, 2014 2:23 am UTC

thank you, I see the difference... although one could be converted to the other very easily...

User avatar
Flumble
Yes Man
Posts: 1777
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The 1 true style

Postby Flumble » Sat Feb 15, 2014 7:08 pm UTC

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 /}\)?[,;])$/.

User avatar
Genuinely, a prune
Posts: 0
Joined: Wed Jul 30, 2014 5:44 pm UTC
Location: Near a council housing estate

Re: The 1 true style

Postby Genuinely, a prune » Wed Jul 30, 2014 10:33 pm UTC

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.");
   }
}

Nyktos
Posts: 135
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: The 1 true style

Postby Nyktos » Thu Jul 31, 2014 12:51 am UTC

Genuinely, a prune wrote:

Code: Select all

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

User avatar
Genuinely, a prune
Posts: 0
Joined: Wed Jul 30, 2014 5:44 pm UTC
Location: Near a council housing estate

Re: The 1 true style

Postby Genuinely, a prune » Thu Jul 31, 2014 10:31 am UTC

Oh? I thought it was the best.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11034
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The 1 true style

Postby Yakk » Thu Jul 31, 2014 2:06 pm UTC

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{
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

cphite
Posts: 1083
Joined: Wed Mar 30, 2011 5:27 pm UTC

Re: The 1 true style

Postby cphite » Thu Jul 31, 2014 5:32 pm UTC

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.

Nyktos
Posts: 135
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: The 1 true style

Postby Nyktos » Fri Aug 01, 2014 4:00 am UTC

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.

User avatar
Dason
Posts: 1308
Joined: Wed Dec 02, 2009 7:06 am UTC
Location: ~/

Re: The 1 true style

Postby Dason » Fri Aug 01, 2014 3:08 pm UTC

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.
double epsilon = -.0000001;

User avatar
Yakk
Poster with most posts but no title.
Posts: 11034
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: The 1 true style

Postby Yakk » Fri Aug 01, 2014 3:35 pm UTC

Sometimes, you just don't want to burn vertical space on else.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Quizatzhaderac
Posts: 1484
Joined: Sun Oct 19, 2008 5:28 pm UTC
Location: Space Florida

Re: The 1 true style

Postby Quizatzhaderac » Fri Aug 01, 2014 6:13 pm UTC

Nyktos wrote:As far as I'm concerned, anything besides

Code: Select all

}
else {
I agree.
The thing about recursion problems is that they tend to contain other recursion problems.

Nyktos
Posts: 135
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: The 1 true style

Postby Nyktos » Fri Aug 01, 2014 7:13 pm UTC

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.

User avatar
Flumble
Yes Man
Posts: 1777
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: The 1 true style

Postby Flumble » Fri Aug 01, 2014 7:51 pm UTC

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)

Nyktos
Posts: 135
Joined: Mon Mar 02, 2009 4:02 pm UTC

Re: The 1 true style

Postby Nyktos » Fri Aug 01, 2014 9:13 pm UTC

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

User avatar
chasesan
Posts: 6
Joined: Thu Oct 13, 2011 3:53 pm UTC
Location: Walking the orbits of electrons.

Re: The 1 true style

Postby chasesan » Sat Aug 16, 2014 12:49 am UTC

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.

rciafardone
Posts: 0
Joined: Thu Jan 05, 2017 5:08 pm UTC

Re: The 1 true style

Postby rciafardone » Thu Jan 05, 2017 5:21 pm UTC

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.

rciafardone
Posts: 0
Joined: Thu Jan 05, 2017 5:08 pm UTC

Re: The 1 true style

Postby rciafardone » Thu Jan 05, 2017 5:26 pm UTC

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.


Return to “Religious Wars”

Who is online

Users browsing this forum: No registered users and 5 guests