Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 13 Apr 2009 10:46:26 +0200
From:      deeptech71@gmail.com
To:        freebsd-chat@freebsd.org
Subject:   My whitespace style (was: Why?? (prog question))
Message-ID:  <49E2FBE2.8020305@gmail.com>

next in thread | raw e-mail | index | archive | help
Tabs are better, because they allow the programmer to specify the 
desired width, and is dynamically changable at any time.

Width could be geared towards a purpose, for example: I've read 
somewhere that the linux kernel guide recommends 8 characters per tab, 
because the distance clearly separates levels, and warns the coder if 
statements are nested too deep. So a linux kernel programmer sets the 
tab with to 8, while another programmer with a small monitor prefers 4 
to see code like "if(..) { if(..) { if(..) { if(..) { if(..) { if(..) { 
...". This is all possible without text processing programs that convert 
indentation sizes required if the code were hard-indented with spaces.

Tabs are to be considered non-constant in width. Specifically they 
should be used for and only for indentation.

So here's my style. First, a display with a tab-size of 8. The arrows 
("------->") denote tabs.

struct datatype {
------->int field_one;             // this is the ...
------->int field_two;             // this contains ...
------->
------->long long int field_three; // and this one is used for ...
------->long long int field_four;  // this one is useless
};

void function( int x )
{
------->if( x > 1234567891011121314 ) {
------->------->do_something();
------->------->do_something_else();
------->}
------->else {
------->------->do_something_else();
------->------->do_something();
------->}
------->
------->assert( c89_is_outdated );
------->
------->struct datatype data;
------->
------->while( check_me_first() && then_check_me() ||
------->       read(&data) && data.field_three > x )
------->{
------->------->do_something();
------->------->x = x + 2*x + 4*x*x + 5*x*x*x + 5*x*x*x*x
------->------->  + x*x*x*x*x*x - 9*x*x*x*x*x*x*x;
------->}
}

The same code with 4 spaces per tab:

struct datatype {
--->int field_one;             // this is the ...
--->int field_two;             // this contains ...
--->
--->long long int field_three; // and this one is used for ...
--->long long int field_four;  // this one is useless
};

void function( int x )
{
--->if( x > 1234567891011121314 ) {
--->--->do_something();
--->--->do_something_else();
--->}
--->else {
--->--->do_something_else();
--->--->do_something();
--->}
--->
--->assert( c89_is_outdated );
--->
--->struct datatype data;
--->
--->while( check_me_first() && then_check_me() ||
--->       read(&data) && data.field_three > x )
--->{
--->--->do_something();
--->--->x = x + 2*x + 4*x*x + 5*x*x*x + 5*x*x*x*x
--->--->  + x*x*x*x*x*x - 9*x*x*x*x*x*x*x;
--->}
}

For every left curly brace, the indentation increases, as the number of 
tabs on a line. But take a look at the loop: It has level 1 nesting, but 
the condition is as long as 2 lines. The second line of the condition is 
indented properly by 1 tab, and is polished a bit by spaces. Finally, I 
find it more readable to put the left curly brace on a new line for such 
long conditions.

I like the idea of writing appropriate amount of tabs for empty lines 
too, it could possibly make a diff output readable (note even the 
additional spaces in the empty line of the struct definition). Although 
I haven't used it in practice.

On a side note, I use spaces inside the outermost parentheses, but not 
in the deeper ones.
I rarely write "} else {" on one line.

Everything else I can think of is based on opinion and good looks.

Finally, another possible definition of the struct:
struct datatype {
------->int field_one;            ------->// this is the ...
------->int field_two;            ------->// this contains ...
------->                          ------->
------->long long int field_three;------->// and this one is used for ...
------->long long int field_four; ------->// this one is useless
};

Elastic tabstops look cool, they are just what I need.

I'm open to critics.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?49E2FBE2.8020305>