Openwall GNU/*/Linux - a small security-enhanced Linux distro for servers
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 15 Jul 2011 17:57:22 +0400
From: Solar Designer <solar@...nwall.com>
To: john-dev@...ts.openwall.com
Subject: Re: coding styles for c/c++? indenter? emacs settings?

Aleksey, all -

On Fri, Jul 15, 2011 at 02:44:36AM +0400, Aleksey Cherepanov wrote:
> I want to know how to indent the code right.
> 
> Owl/doc/CONVENTIONS says that good (not perfect) way to indent is
>  'indent -kr -i8 -nlp -nbbo -l79 -lc79'
>  (http://cvsweb.openwall.com/cgi/cvsweb.cgi/~checkout~/Owl/doc/CONVENTIONS?rev=1.33;content-type=text%2Fplain
>  ).
> 
> But documentation for indent says that it is not suitable for c++. For
> instance template
> 'func<T>(arg);'
> will be formatted as
> 'func < T > (arg);'
> that is not good (i saw similar code in Johnny).
> 
> Are there any known or already used alternative of indent for c++?
> 
> May it be easier to use indent and hands? Because as i understand it is
> needed to use hands even with indent as of it is not perfect.

I am not aware of anything better than "indent and hands", but I did not
specifically look for indenters for C++.  I'm sure some exist.

> By the way why indent is not perfect? What does indent miss? Or what is the
> difference between indent and the preferred coding style? Or what is the
> preferred coding style?

Here are some differences between what indent does (with the settings
given above) vs. what I do:

For occasional labels (goto targets), I put them at the very beginning
of a line.  You can also see this in the Linux kernel sources.  However,
indent somehow indents them by 6 spaces - really weird.

For the struct fmt_* initializers in JtR, we use a certain meaningful
style, but indent does something weird with them.

With nested loops fully sharing their body, I often only indent the body
by one tab, like:

for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
for (k = 0; k < 10; k++) {
	... body ...
	... body ...
}

but indent would indent the loops as well, resulting in:

for (i = 0; i < 10; i++)
	for (j = 0; j < 10; j++)
		for (k = 0; k < 10; k++) {
			... body ...
			... body ...
		}

which I don't think is any more readable.  I admit that the latter
appears to be more common in code written by others, so I don't strictly
object to this change.  It becomes problematic when the indent levels
otherwise-unnecessarily get very deep, though.

The same applies to other constructs fully sharing a block, like:

for (i = 0; i < 10; i++)
if (ok(i))
while (j))
	... body ...
	... body ...
}

Here's a specific example, from cracker.c: crk_password_loop():

	if (salt->hash_size < 0) {
		pw = salt->list;
		do {
			if (crk_methods.cmp_all(pw->binary, crk_key_index))
			for (index = 0; index < crk_key_index; index++)
			if (crk_methods.cmp_one(pw->binary, index))
			if (crk_methods.cmp_exact(pw->source, index)) {
				if (crk_process_guess(salt, pw, index))
					return 1;
				else
					break;
			}
		} while ((pw = pw->next));
	} else
	for (index = 0; index < crk_key_index; index++) {
		if ((pw = salt->hash[salt->index(index)]))
		do {
			if (crk_methods.cmp_one(pw->binary, index))
			if (crk_methods.cmp_exact(pw->source, index))
			if (crk_process_guess(salt, pw, index))
				return 1;
		} while ((pw = pw->next_hash));
	}

I think it's perfectly readable as written, but indent would use much
deeper indentation levels, so we'd have either lines exceeding 79 chars
or we'd need to use narrower than 8-char tabs.  Or lines would wrap,
which would hamper readability.

> Also i want to set my editor up to support such style. Does anyone have
> such settings for emacs? Probably set up editor could replace indenter.

I don't.  I indent my code manually.  The only editor feature I use is
(un)indenting entire blocks by one or a few steps when I move them
around.  I select the block in VIM, then press < or >, optionally
preceded by a number.

Thanks,

Alexander

Powered by blists - more mailing lists

Your e-mail address:

Powered by Openwall GNU/*/Linux - Powered by OpenVZ