PDA

View Full Version : Coding standard? Indentation?


Bandobras
December 20, 2007, 19:23
I keep making a mess of indentation whenever I touch a C file from Angband, so I wonder what I should set up my Emacs to (or Eclipse, or 'indent' utility). I see a year ago I had the following conversation on the NPP forum, but now that V is again active I wonder if there are any opinions...

> > > Merry Christmas!
> > >
> > > wonder what coding standards do you adhere to.
> > > E.g. GNU (http://www.gnu.org/prep/standards/standards.html)? Any other?
> > >
> > > Also, I wonder what indentation you use: GNU, K&R? Spaces or tabs for indents?
> > > 4-wide or 8-wide tabs? What was the original standard of Moria?
> > > Of Ben Harrison's Vanillia? Do you ever use
> > > the indent utility to maintain a single format?
> > >
> > > BTW, is NPP os a CVS/SVN somewhere?
> > >
> > > Thanks a lot!
> >
> > CJN: AFAIK, most *bands, including NPP, use K&R style with 8-wide tabs and tab for indent.
> >
>
> Bad, bad, bad. But at least the tab width is standard ---
> however the text must go off 80 characters wide terminal very often...
> Hmm, from the look of the code it's not pure K&R. Obviously not GNU, too.
> I guess everybody indents as he likes...

takkaria
December 21, 2007, 00:51
I keep making a mess of indentation whenever I touch a C file from Angband, so I wonder what I should set up my Emacs to (or Eclipse, or 'indent' utility). I see a year ago I had the following conversation on the NPP forum, but now that V is again active I wonder if there are any opinions...

http://rephial.org/wiki/CodingGuidelines

Bandobras
December 21, 2007, 22:45
A very nice article. It says

Use the BSD/Allman style, and indent using tabs. Avoid lines over 80 character long (though this is not strict).

Is it 80 characters with 8 characters wide tab? That's a very wide tab...

Supposedly (Wikipedia)

indent somefile.c -st -bad --blank-lines-after-procedures -bli0 -i4 -l79 \
-ncs -npcs -nut -npsl -fca -lc79 -fc1

indents code by BSD/Allman. I wonder how much of V would fit in 80 lines if run through with that utility and with 8-characters tab.

Emacs can be set up for BSD/Allman probably just with

'(c-default-style (quote ((c-mode . "bsd") (c++-mode . "bsd"))))
'(c-basic-offset 3)
'(standard-indent 3)
'(tab-width 3)


I'll try and see if I'm less destructive with that setting. :)

Edit: more emacs code

takkaria
December 21, 2007, 23:02
Is it 80 characters with 8 characters wide tab? That's a very wide tab...

8 characters would be crazy. I generally use 3, just because everyone else uses even numbers -- in this way, I can see spaces-instead-of-tabs really easily.

Supposedly

indent somefile.c -st -bad --blank-lines-after-procedures -bli0 -i4 -l79 \
-ncs -npcs -nut -npsl -fca -lc79 -fc1

indents code by BSD/Allman. I wonder how much of V would fit in 80 lines if run through with that utility and with 8-characters tab.

Indent always manages to screw up the source. Steven Fuerst (previous Z developer) wrote a patch for indent that makes it indent code Angband-style.

pav
December 25, 2007, 20:56
Tabs are ment to be eight spaces wide :) That's the end of it.
That said, it does not matter much on a 300+ columns terminal. :)

Bandobras
December 25, 2007, 21:04
Those wide-screen monitors?

pav
December 25, 2007, 21:13
Those wide-screen monitors?
Pretty wide. Good for coding, bad for scrollback.

Bandobras
March 21, 2008, 23:48
8 characters would be crazy. I generally use 3, just because everyone else uses even numbers -- in this way, I can see spaces-instead-of-tabs really easily.

So, I now use 3 spaces wide tab, too. But I recently encountered a case where tab width matters. (This is how my emacs formats it by itself.)

int v = MY_CONST * (wieeerd_struct->loooooooooooong_field
+ wieeerd_struct->longeeeeeeeeeeeeeeeeeer_field
- wieeerd_struct->medium_field)


It's obviously tab width-depenent, so what am I doing wrong? Does it agree with BSD/Allman?

P.S. A simpler example:

int v = (wieeerd_struct->loooooooooooong_field
+ wieeerd_struct->longeeeeeeeeeeeeeeeeeer_field
- wieeerd_struct->medium_field)

takkaria
March 21, 2008, 23:59
So, I now use 3 spaces wide tab, too. But I recently encountered a case where tab width matters. (This is how my emacs formats it by itself.)

int v = MY_CONST * (wieeerd_struct->loooooooooooong_field
+ wieeerd_struct->longeeeeeeeeeeeeeeeeeer_field
- wieeerd_struct->medium_field)


It's obviously tab width-depenent, so what am I doing wrong? Does it agree with BSD/Allman?

P.S. A simpler example:

int v = (wieeerd_struct->loooooooooooong_field
+ wieeerd_struct->longeeeeeeeeeeeeeeeeeer_field
- wieeerd_struct->medium_field)


You use tabs to indent to the same as the line you're continuing on from, and then spaces after that. This solves indentation issues with all setups.

Bandobras
March 22, 2008, 00:13
You use tabs to indent to the same as the line you're continuing on from, and then spaces after that. This solves indentation issues with all setups.

Ha, so this is emacs fault --- it does not let me mix tabs and spaces with default settings. Instead it fits as many tabs as it can and then a few spaces. Of course it's configurable, for sure...

Bandobras
May 24, 2008, 19:46
Ha, so this is emacs fault --- it does not let me mix tabs and spaces with default settings. Instead it fits as many tabs as it can and then a few spaces. Of course it's configurable, for sure...

After some experimentation and some googling I'm afraid it's not configurable. Unless I want to script my own c-mode, that is, which I don't. I don't want to part with automatic indentation that Emacs gives me, so I have no choice but to break indentation whenever I contribute, either by inserting too many tabs in some cases (as shown above) or by turning to spaces-only mode.

I'm wondering which way is better and what tab width to assume (4 reads best for me, 3 would be fine, but statements align with conditions in ifs, 2 is not enough visual feedback with such long functions as C has).

No, I won't switch to vi. :p