Indentation 
Author Message
 Indentation

I'm relatively new to C.  I have been coding for a time with no
direction on style.  I have read a few style guides but have seen such
variation.  I know that adopting a standard and sticking with it is a
good idea.  I have bounced about with my own style and feel like a ship
without a good rudder.  Plus I am debugging and adding onto code that is
already there... (and has a multitude of personal styles... none of
which are the same).

Here are two excerpts from Linux' style-guide that I have /not/ followed
that well... (or not at all)

"Now, some people will claim that having 8-character indentations makes
the code move too far to the right, and makes it hard to read on a
80-character terminal screen.  The answer to that is that if you need
more than 3 levels of indentation, you're screwed anyway, and should fix

your program."

"Functions should be short and sweet, and do just one thing.  They
should
fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,

as we all know), and do one thing and do that well."

I am obsessed  at keeping my code within 80 characters.  My indentation
has been around 3 chars.  If I were to go back and recode what I've done
and go 8 chars on the indentation /and/ keep within 80 chars, I'd be
doing a total rewrite.  But if the advice above is correct... I do need
to rewrite.

Any advice?
Keith



Fri, 04 Feb 2005 11:15:55 GMT  
 Indentation

Quote:

> Here are two excerpts from Linux' style-guide that I have /not/ followed
> that well... (or not at all)

> "Now, some people will claim that having 8-character indentations makes
> the code move too far to the right, and makes it hard to read on a
> 80-character terminal screen.  The answer to that is that if you need
> more than 3 levels of indentation, you're screwed anyway, and should fix
> your program."
> I am obsessed  at keeping my code within 80 characters.  My indentation
> has been around 3 chars.  If I were to go back and recode what I've done
> and go 8 chars on the indentation /and/ keep within 80 chars, I'd be
> doing a total rewrite.  But if the advice above is correct... I do need
> to rewrite.

Linus is exaggerating in his style guide.  It's good to write
code that fits it, but overriding circumstances do come up.  If
you're consistently writing deeply indented code, it might be
something to worry about.  But Linus, and Linux, don't conform
strictly to the above admonishments, and you can't be expected to
do so either.


Fri, 04 Feb 2005 12:04:42 GMT  
 Indentation

Quote:
> I'm relatively new to C.  I have been coding for a time with no
> direction on style.  I have read a few style guides but have seen such
> variation.  I know that adopting a standard and sticking with it is a
> good idea.  I have bounced about with my own style and feel like a ship
> without a good rudder.  Plus I am debugging and adding onto code that is
> already there... (and has a multitude of personal styles... none of
> which are the same).

> Here are two excerpts from Linux' style-guide that I have /not/ followed
> that well... (or not at all)

> "Now, some people will claim that having 8-character indentations makes
> the code move too far to the right, and makes it hard to read on a
> 80-character terminal screen.  The answer to that is that if you need
> more than 3 levels of indentation, you're screwed anyway, and should fix

> your program."

> "Functions should be short and sweet, and do just one thing.  They
> should
> fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,

> as we all know), and do one thing and do that well."

> I am obsessed  at keeping my code within 80 characters.  My indentation
> has been around 3 chars.  If I were to go back and recode what I've done
> and go 8 chars on the indentation /and/ keep within 80 chars, I'd be
> doing a total rewrite.  But if the advice above is correct... I do need
> to rewrite.

> Any advice?

Just be consistent. If you change style and go back to an earlier
project then use the style on that project while debugging/changing.

I think 8 characters are too much. 3 levels of indentation... Well, if
you have double loop, and an if inside the inner loop you suddenly
have four levels. So take this with a pinch of salt, but it might
be good to have it in the back of your head.

--
Thomas.

Approaching singularity.



Fri, 04 Feb 2005 17:40:32 GMT  
 Indentation

Quote:

>   My indentation
> has been around 3 chars.  

I like 4

--
 pete



Fri, 04 Feb 2005 18:21:41 GMT  
 Indentation

Quote:

> I'm relatively new to C.  I have been coding for a time with no
> direction on style.  I have read a few style guides but have seen such
> variation.  I know that adopting a standard and sticking with it is a
> good idea.  I have bounced about with my own style and feel like a ship
> without a good rudder.  Plus I am debugging and adding onto code that is
> already there... (and has a multitude of personal styles... none of
> which are the same).

> Here are two excerpts from Linux' style-guide that I have /not/ followed
> that well... (or not at all)

> "Now, some people will claim that having 8-character indentations makes
> the code move too far to the right, and makes it hard to read on a
> 80-character terminal screen.  The answer to that is that if you need
> more than 3 levels of indentation, you're screwed anyway, and should fix

> your program."

> "Functions should be short and sweet, and do just one thing.  They
> should
> fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,

> as we all know), and do one thing and do that well."

> I am obsessed  at keeping my code within 80 characters.  My indentation
> has been around 3 chars.  If I were to go back and recode what I've done
> and go 8 chars on the indentation /and/ keep within 80 chars, I'd be
> doing a total rewrite.  But if the advice above is correct... I do need
> to rewrite.

> Any advice?

Develop your own style, modify it as and when it makes sense so to do,
and don't pay too much attention to style guides.

It seems your biggest concern is that of whitespace. Note that there are
other aspects of style which are rather more important, but it is true
that good use of whitespace *is* important. It is also true that nobody
has yet come up with a definitive whitespace style that satisfies
everybody. I use the following whitespace style:

Allman indent (braces align vertically at the same level as the
controlling statement), two spaces, no tabs, whitespace around binary
and conditional operators, but not around unary operators. Vertical
whitespace is used liberally where it appears to add clarity.

I find that style to lead to clear, readable code. Some people say it
wastes vertical whitespace. I accept their criticism; what they don't
realise is that I have a magic keyboard which doesn't charge me extra
for pressing ENTER. :-)

Here is an example of that style in actual code (the program itself is
sheer nonsense, so don't waste time trying to see meaning in it):

#include <stdio.h>

int print(int w, int x, int y, int z)
{
  return printf("%d%d%d%d\n", w, x, y, z);

Quote:
}

int main(void)
{
  int a = -1;

  while(++a < 10)
  {
    int b = -1;

    while(++b < 10)
    {
      int c = -1;

      while(++c < 10)
      {
        int d = -1;

        while(++d < 10)
        {
          print(a, b, c, d);
        }
      }
    }
  }

  return 0;

Quote:
}

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Fri, 04 Feb 2005 19:20:34 GMT  
 Indentation

Quote:

> Allman indent (braces align vertically at the same level
> as the controlling statement), two spaces, no tabs,
> whitespace around binary and conditional operators, but
> not around unary operators. Vertical whitespace is used
> liberally where it appears to add clarity.

> I find that style to lead to clear, readable code. Some
> people say it wastes vertical whitespace. I accept their
> criticism; what they don't realise is that I have a magic
> keyboard which doesn't charge me extra for pressing ENTER.
> :-)

I have a very similar style (Allman, 2 spaces, no tabs). However I don't
always put whitespace around binary operators. If there is more than one
type of binary operator in an expression I often use spaces or the lack of
them to act as a visual reminder of precedence.

Example:

struct foo
{
  int a;
  int b;

Quote:
};

int main(void)
{
  struct foo f = {2, 3};

  /* I would write: */
  f.a = f.b;

  /* Rather than: */
  f . a = f . b;

  return 0;

Quote:
}

And it's not just around the '.' operator, I have also left out the spaces
around multiplications:

n.x = (v.y - u.y)*(w.z - u.z) - (v.z - u.z)*(w.y - u.y);
n.y = (v.z - u.z)*(w.x - u.x) - (v.x - u.x)*(w.z - u.z);
n.z = (v.x - u.x)*(w.y - u.y) - (v.y - u.y)*(w.x - u.x);

--
Simon.



Fri, 04 Feb 2005 20:37:01 GMT  
 Indentation

Quote:


> > Allman indent (braces align vertically at the same level
> > as the controlling statement), two spaces, no tabs,
> > whitespace around binary and conditional operators, but
> > not around unary operators. Vertical whitespace is used
> > liberally where it appears to add clarity.

<snip>

> I have a very similar style (Allman, 2 spaces, no tabs).

Yay! Go Simon! :-)

Quote:
> However I don't
> always put whitespace around binary operators. If there is more than one
> type of binary operator in an expression I often use spaces or the lack of
> them to act as a visual reminder of precedence.

> Example:

> struct foo
> {
>   int a;
>   int b;
> };

> int main(void)
> {
>   struct foo f = {2, 3};

>   /* I would write: */
>   f.a = f.b;

>   /* Rather than: */
>   f . a = f . b;

Er, so would I. I don't count that one. :-) Same goes for -> where I
would use a->b, not a -> b.

Quote:

>   return 0;
> }

> And it's not just around the '.' operator, I have also left out the spaces
> around multiplications:

> n.x = (v.y - u.y)*(w.z - u.z) - (v.z - u.z)*(w.y - u.y);
> n.y = (v.z - u.z)*(w.x - u.x) - (v.x - u.x)*(w.z - u.z);
> n.z = (v.x - u.x)*(w.y - u.y) - (v.y - u.y)*(w.x - u.x);

Ah, I don't think I'd like to do that.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Fri, 04 Feb 2005 23:28:15 GMT  
 Indentation


Quote:


> > > Allman indent (braces align vertically at the same level
> > > as the controlling statement), two spaces, no tabs,
> > > whitespace around binary and conditional operators, but
> > > not around unary operators. Vertical whitespace is used
> > > liberally where it appears to add clarity.

> <snip>

> > I have a very similar style (Allman, 2 spaces, no tabs).

> Yay! Go Simon! :-)

I do not understand what is the matter with you people! It should
be 3, _three_ spaces! Do you get me? How you can even consider
using only two spaces is beyond me!

FLAMEWAR!!

Oh, and btw, ;)

--
Thomas.

Approaching singularity.



Sat, 05 Feb 2005 03:03:22 GMT  
 Indentation
  For myself, I just hit "indent region" while in the Epsilon text
editor and accept the indentation I get from that. (four spaces).  Saves
a lot of time when cleaning up someone else's less-than-neat code...

David Starr

Quote:

> I'm relatively new to C.  I have been coding for a time with no
> direction on style.  I have read a few style guides but have seen such
> variation.  I know that adopting a standard and sticking with it is a
> good idea.  I have bounced about with my own style and feel like a ship
> without a good rudder.  Plus I am debugging and adding onto code that is
> already there... (and has a multitude of personal styles... none of
> which are the same).

> Here are two excerpts from Linux' style-guide that I have /not/ followed
> that well... (or not at all)

> "Now, some people will claim that having 8-character indentations makes
> the code move too far to the right, and makes it hard to read on a
> 80-character terminal screen.  The answer to that is that if you need
> more than 3 levels of indentation, you're screwed anyway, and should fix

> your program."

> "Functions should be short and sweet, and do just one thing.  They
> should
> fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,

> as we all know), and do one thing and do that well."

> I am obsessed  at keeping my code within 80 characters.  My indentation
> has been around 3 chars.  If I were to go back and recode what I've done
> and go 8 chars on the indentation /and/ keep within 80 chars, I'd be
> doing a total rewrite.  But if the advice above is correct... I do need
> to rewrite.

> Any advice?
> Keith



Mon, 07 Feb 2005 03:13:35 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Smart indentation

2. Indentation/formatting style of C programs: solution found.

3. Indentation

4. code indentation tools

5. Indentation style question

6. Indentation style of if loops

7. INDENTSR: modifiable to different form of indentation?

8. indentation: enough already!

9. Standard Indentation etc.

10. The great indentation debate continues!! (long)

11. Standard indentation?

12. Imposed indentation standards don't work

 

 
Powered by phpBB® Forum Software