Doesn't it just hurt your eyes...

Pages: 12345
some people code like this, and it bugs the sh!t out of me when I want to read it. Especially on this website.

They don't use code tags, so their formatting is lost; it's not that they never had any formatting.

DeXecipher wrote:
1
2
3
   temp = a;
   a = b;
   b = temp; 



I do hope you'll use std::swap() in any real code. This is one of my pet peeves.
DeXecipher wrote:
some people code like this, and it bugs the sh!t out of me when I want to read it. Especially on this website.


It's worth noting that people may actually have used indentation using tabs, but the tabs were not transferred in copy-pasting, because HTML-textarea's don't accept tabs. Most proper text editors will let you configure tabs as spaces, but that's a whole other religious war I won't embark on.

@Fredbill30 There are quite a few indentation styles around: http://en.wikipedia.org/wiki/Indent_style
Preference of one over another is a matter of personal taste and so there is no "one true answer".
People generally associate their personal tastes with their sense of identity, so criticizing their preference might lead to strong reactions in some people (aka flame wars).

That being said, the defence that seems to come up most often in favour of style such as K&R is to avoid using "extra lines". I personally feel that putting curly braces on their own line, whilst indenting their "content", makes it visually very clear that this is a code-block. I've read that the extra lines argument comes from the time where people could only fit so many lines on a screen or buffer, which doesn't hold up any longer.

The "best" style is the one that is used persistently in a project by all its members, that's about as far as I'd go to generalize.

All the best,
NwN
closed account (3qX21hU5)
I agree with NwN, as long as you use the your style consistently you should be fine. Of course there is exceptions, like one just recently was where the programmer put all his semi colons at the start of each line. It was almost impossible to read and know which ones were just blank statements and which ones were errors ect.

Also remember that if you plan to program on any big projects or want to choose programming as a career, you will have to get used to a lot of different styles. So learn to read them all. Because each project will have its own rules on which style they are going to use and everyone will have to use it because it makes the code much more uniform.
NwN wrote:
I personally feel that putting curly braces on their own line, whilst indenting their "content", makes it visually very clear that this is a code-block.
Proper indentation itself is perfectly sufficient to denote code blocks - hence Python/Haskell. With that in mind, curly brackets are pretty much useless and giving a line for each is a waste of space (although stuff; } is a bit ugly and very uncomfortable when editing code). Note that both absence and abuse of whitespace can be considered obfuscation - it only remains to decide where the middle is.

Fredbill30 wrote:
How can anyone stand putting the bracket on the same line?
You're being silly.
Last edited on
I personally prefer having the brace on the same line.
I've got very specific tastes for where brackets should go.
For functions, brackets go on different lines
1
2
3
int main()
{
}


For conditional statements, brackets go on the same line. In the case of an else, the else goes on the same line as the previous if closing bracket.
I also use a bracket on the same line for for and while loops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(something) {
     return true;
} else if(something else) {
     return false;
} else {
     throw exception;
}

for(auto &i: ints) {
    cout << i << endl;
}
while(true) {
    do something here
}


For accessor functions (assuming the function is trivial), the definition and declaration are on the same line.
1
2
int getX() { return xpos; }
int setX(int x) { xpos = x; }


That's my personal preference.
I agree with Thumper minus the conditional statement style.
closed account (zb0S216C)
I have my own style, thought it uses a little more white-space than most styles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Function( int Arg_ )
  {
    if( Something )
      {
        return( Something );
      }
  }

struct Base_Class
  {
    // ...
  };

struct Derived_Class
    :  Base_Class
  {
    public:
      int _M_Member_;
  };

I use a lot of styles, but the above style is the most comfortable for me, which is what matters most.

Wazzak
Last edited on
Framework, that's the exact style I used when I was new to C++.
@ Framework: my first (maybe misguided) thought whenever I see anyone use that style is "poor guy can't set different colors for symbols".

As far as I can tell, setting punctuation, parentheses and curly braces to show in a bright color is an important readability boost, without having to resort to extra white-space.

Example:
http://s21.postimage.org/q3c24mtxj/redbraces.gif

Edit: re-uploaded picture.
Last edited on
But they are all the same color... you improve nothing
Doesn't bother me a bit. I may have missed it, but I was surprised no one pointed out that Java code is like that.

1
2
3
4
5
public class HelloWorldApp{
      public static void main(String[] args){
            System.out.println("Hello World!");
      }
}

The thing I think makes for unreadable code for me is when they cram it into the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
      while(true){ // yes I know it is infinite
            doSomething();
            for(;;){ // yep a 'forever' loop
                  if(/*condition is met*/)
                        break;
                  else
                        continue;
             }
      }
// clean everything up
return 0;
}

After a while of no spacing format it is hard on your eyes to keep track of where you are. I feel indentation formatting and spacing chunks of code properly (whitespace and blank lines around chunks of code) is a major factor to readability and not placement of curly brackets as this is just a preference on the programmer's part.

I must note, while Java uses the above sampled code, I should point out that it does work with this too:
1
2
3
4
5
6
7
public class HelloWorldApp
{
      public static void main(String[] args)
      {
            System.out.println("Hello World!");
      }
}

Again, more a preference for the most part.
Last edited on by closed account z6A9GNh0
closed account (3qX21hU5)
One thing I don't like is when people put single statement if conditions or single statement loops in brackets (Except where they are needed). It just seems like a waste of space to me and personally I find it easier to read without the brackets.


1
2
for (int i = 0; i != something; ++i)
    doSomething();


Just looks so much cleaner then


1
2
3
4
for (int i = 0; i != something; ++i)
{    
    doSomething()
}
Last edited on
The only styles I don't like are when the braces are the same indent level as the block:
1
2
3
4
int main()
  {
   return 0;
  }

or on the same line as the opening line of a block:
1
2
3
4
int main()
{   std::cout << "hello, world" << std::endl;
    return 0;
}


My style is K&R: 4 space indents, all braces on the same line as the opening statement except for functions and methods:
1
2
3
4
5
6
7
8
9
10
11
12
namespace ns {
    class Class {
    public:
        Class(bool b)
        {
            if (b) {
                do_the_things();
                do_the_other_things();
            }
        }
    };
}


@BHXSpecter
Did you put the opening brace in your last example out of line on purpose? Because that really hurts my eyes.

Last edited on
I prefer braces to always be on their own line (except the closing brace of a do-while loop, which has the while clause immediately after it).

I do want to ask this question though: what style is it, and why is it, that you put a space before opening parens? Or you put a space around every single token in existence?
@ chrisname: he did it in the penultimate example as well!
Last edited on
I code using levels of dependance, like a tree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
template<typename t, typename count_type>
class Array
{  
     private: //levels
           
          //subset of the private part of Array<...,...>
          t* g_data; 
  
     public: //levels
          
          //subset of the public part of Array<...,...>
          Array<t, count_type>( count_type size ); 

          count_type getSize();
          
          const t& get( count_type idTag );

          void set( const t& elem, count_type idTag ); 
};

//if empty method don't waste space
void program_methodA(){}
void program_methodB(){}
void program_methodC(){}

int main()
{ 
     while( true )
     {
         program_methodA();
         program_methodB();
         program_methodC();
     }
     
      return 0;
}


I think its the best style for readability, but sometimes when I just want to
code a bit faster i code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int sum( int a, int b );

void do_something();

int sum( int a, int b ){//a bit easier/lazier
    
   return ( a + b );
}

void do_something(){//a bit easier/lazier
    //remember levels
    while(true)
    {
        printf("never stops.\n");
    }
}

int main( int argc, char* argv[] ){ //a bit easier/lazier
//make sure to indent and produce at least one empty line between
//UNRELATED program statements 

//instance data, remember dependance upon main, produce one tab (level)
    int a = argc;
    int b = argc * 2;
    int c = 0;
    int d = 0;

//main program methods
    c = sum( a, b );
    d = sum( c, b );
    
    if( c == d ){//a bit easier/lazier, but remember levels
 
         do_something();
    }

    return d; //if returned then c != d 
}


Yes those were random examples that I just typed to show my style(s). Naughty Dog Studios codes like that as well, I didn't know that until I read a game engine architecture book. It just so happens that my coding style coincides with that style/standard (Aldman (ANSI) ). I didn't always code like that, especially when I first started coding, but it started to evolve a bit naturally as I started to code more and more and READ more and more program statements. Programming languages like python also force a style with levels onto the user as well, hence a reason why it is taught to beginner programmers in Universities.
Last edited on
chrisname wrote:

@BHXSpecter
Did you put the opening brace in your last example out of line on purpose? Because that really hurts my eyes.

In edit mode it isn't misaligned, but spaced it one more so in edit mode it is under the u in public so I wouldn't hurt your eyes. I follow a style, but realize others may not for whatever reason. No code hurts my eyes, just prefer it to be easily readable, but if not I'd be fine trying to read this kind of code even though it would take a while on huge files.
1
2
#include <iostream>
using namespace std; int main(){ cout << "Hello!\n Enter a number:"; double num; cin >>  num; cout << "You entered: " << num <<"\nGoodbye!"; return 0; }
I just hate things being misaligned like that.
Pages: 12345