Re: A Question of style.
by talexb (Chancellor) on Jun 06, 2005 at 21:32 UTC
|
While no doubt well meant, the subject of coding style has enough opinions that it is usually referred to as a 'religious war', comparable to one's choice of editor or operating system. Thus, in polite society (hopefully The Monastery is included in this definition), one usually soft-pedals any discussion of said 'delicate' subjects -- or even doesn't mention them at all.
To put it another way, there's no 'nice' way to discuss bracing style -- it's just a matter that we're going to have to agree to disagree on.
And that's really all I can say.
Alex / talexb / Toronto
"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds
| [reply] |
Re: A Question of style.
by kirbyk (Friar) on Jun 06, 2005 at 21:40 UTC
|
Yes, as people say, this is one of those things were TMTOWTDI really is true.
I will say, the one concrete thing (as opposed to just preference) I like about having the { on the same line of a sub or an if:
If it's a longer sub or if block, and I'm at the closing }, I can hit % in vi, and it takes me to the matching one. If it's on the same line, I see the sub name or conditional on the screen, which is _exactly_ what I want. If it's on the next line, I have to then scroll up a line to see, and the next thing I'm usually going to want to do after seeing that is % back to the end brace (to go back to where I was.) So, it annoys me to no end when people put them on the next line.
| [reply] |
|
|
If it's a longer sub or if block, and I'm at the closing }, I can hit % in vi, and it takes me to the matching one. If it's on the same line, I see the sub name or conditional on the screen, which is _exactly_ what I want. If it's on the next line, I have to then scroll up a line to see, and the next thing I'm usually going to want to do after seeing that is % back to the end brace (to go back to where I was.) So, it annoys me to no end when people put them on the next line.
Can't you just rebind the % key so that (in addition
to what it usually does) it checks that the line it
is on is not the first or last line in the buffer,
scrolling one line if necessary to accomplish that?
vim fans claim it's pretty customizable, so one
would think that would be easy (though, not being a
vi user myself, I'm speculating here).
(Of course, I prefer the behavior wherein the editor
shows the line containing the opening brace in the
status area when your cursor rests after the closing
brace, so you don't have to hit a key in the first
place, which brings us back to your argument, since
there's not room in the status area for two lines
without expanding it, which is a bit jarring.)
My own reason for preferring nestled braces in most
cases is that it makes the code significantly more
vertically compact, which allows a lot more context
to fit on the screen at once, making the code easier
to follow, since you can see more with less scrolling
about.
"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."
— Pratico & Van Pelt, BBHG, p68
| [reply] |
|
|
| [reply] |
Re: A Question of style.
by trammell (Priest) on Jun 06, 2005 at 21:44 UTC
|
I think that differences in indentation, etc. are largely superficial. Some real style differences that matter at the end of the day are:
- Is your code documented and commented?
- Does your code have a reasonable interface? Have you separated concerns?
- Does your code have a test suite? Do you use it?
If you do these things these cosmetic issues like your indentation style don't matter.
| [reply] |
Re: A Question of style.
by TheStudent (Scribe) on Jun 06, 2005 at 21:45 UTC
|
I think that:
Food for thought, not fuel for flames.
is very wishful thinking. Coding styles always generate differing points of view. It is as taboo a subject as religion or politics.
A very quick SuperSearch shows:
| [reply] |
Re: A Question of style.
by hardburn (Abbot) on Jun 07, 2005 at 13:30 UTC
|
While working on the B language, K&R realized that cyrillic character sets have no curly-brace. Further, there is no tab character Therefore, a language that was to catch on in the West should rely heavily on this syntax in order to stop Godless Communists from using it.
Not only did K&R use this syntax, but they also suggested a bracing style that was difficult to use on commie terminals:
sub y
{
$h = shift;
if( $b ) {
# code
}
elsif( $t ) {
# code
}
}
As you can see, the key block, sub, places the brace on the next line. This is because Russian terminals were only 7 lines high, which was forced by Stalin's 1957 decree. With this style in hand, only 5 other lines were visible. The effectiveness can be increased through the use of plentiful vertical whitespace.
However, modern corruption of the programming field has led to the development of languages that forsake this important, commie-fighting syntax.
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: A Question of style.
by dbwiz (Curate) on Jun 06, 2005 at 21:29 UTC
|
Just a few days ago we had a discussion about the
same subject.
Have a look at it. There are plenty of internal and
external links to satisfy your needs.
| [reply] |
Re: A Question of style.
by adrianh (Chancellor) on Jun 07, 2005 at 10:18 UTC
|
Oh lord. Not the "one true brace style" argument again :-)
Learning to use perltidy and learning to compromise on something that the whole team can agree on would be my advice.
| [reply] |
Re: A Question of style.
by tilly (Archbishop) on Jun 08, 2005 at 01:37 UTC
|
Code Complete has (as usual) a lot of good things to say about indentation style. The best IMO is that programming expertise is fragile. Changing any of many apparently insignificant details can ruin an expert programmer's efficiency. It is possible to learn not to be so hampered by details, but they matter.
If you ever have to use another form of indentation for long enough to become used to it, I predict that you'll discover that your present preference is entirely due to this fragility. You mentally block code based on the indentation style, and facing an unfamiliar indentation style you need to start thinking about that, which gets in the way of everything else that you do.
However there are other people who would have exactly the same issue with how you indent code.
BTW about commenting, if you really put comments on every block, you're probably overdoing it. Remember, comments start with as many bugs as code does, but bugs in code get fixed. And code tends to be better maintained than comments. Therefore if your comments reiterate what your code does, then experienced programmers are likely to ignore the comments.
The mantra is to make the code, as much as possible, its own comment. You do this with well-chosen variable names, function names, and so on.
Does that mean that you can remove comments? Of course not. But in the long run I've found, as I0 put it once, I find the most useful comments state what remains invariant, while the code states what gets transformed. In that way comments document what you need to know when editing the code, but changes to the logic are less likely to require re-writing comments.
A relevant thread is Re (tilly) 2 (disagree): Another commenting question,. As I always point out when I bring up this thread, the thread was accompanied by a private chat between me and DeusVult. The tone of the thread was far more confrontational than the chat, so this thread doesn't really capture the actual tone of the full conversation. But still it is worthwhile reading. | [reply] |
|
|
Excellent reply. Thank you for the links. I guess you recommend Code Complete? :-)
I agree fully with your comments about comments and names, and indentation actually too. Our experience is indeed that programming expertise is fragile (legacy K&R formated code in our software really slows us down).
No, I don't comment each block as illustrated. On reflection that was a rather illustration. Hey-ho.
Food for thought, not fuel for flames.
| [reply] |
Re: A Question of style.
by jonadab (Parson) on Jun 07, 2005 at 10:22 UTC
|
Dude, just use cperl-mode (or the equivalent) and let it do your indentation for you. Then you can get on to thinking about your actual code. As far as your third point, automatic paren matching is forty-year-old technology, and any text editor that doesn't do it is lame in the extreme. Get a better text editor. | [reply] |
|
|
but cperl-mode supports several styles, do you know about M-x cperl-set-style???
| [reply] [d/l] |
Re: A Question of style.
by Nkuvu (Priest) on Jun 06, 2005 at 22:34 UTC
|
It doesn't matter what style of indentation you use. As long as you use it consistently. | [reply] |
|
|
By that argument, would lack of indentation qualify as consistent use?
Personally, I use what makes sense to me. I never focused on a particular style until I learned to write LPC, and I still write my Perl that way. (trust me -- it's better than how I learned to format Perl, as I came from a BASIC, Logo, Pascal and FORTRAN before that).
As things go, I think that screen size and editor choice play a rather significant role in what someone's preferred style. Someone who learned to program on a 12 line high screen, or through a dumb terminal is going to think differently than someone who started programming with a 21" monitor on their desk. Likewise, someone who started out in ex/ed, or some other line editor is going to work much different from someone who worked in DOS EDIT, or BBEdit, or whatever else the popular editors are these days.
I'm personally okay with just about anything, because I'll just reflow it. (which works up until I'm trying to submit patches, in which case I have to save my initial reformatted version, so I can diff that and the final, then re-apply the changes with the author's style of formatting back to the original. For the most part, I'm happy with whatever I'm given -- it's normally enough for me that people have gone out of their way to give their code at all, I'm not going to be overly picky about how many spaces are in it.
| [reply] |
|
|
It doesn't matter what style of indentation you use. As long as you use it consistently.
If by "consistently" you mean "always the same way",
then I actually disagree with this, at least to some
extent. It is my
considered opinion that good indentation style takes
into consideration the semantics of the code being
indented. Sometimes I put up to three "statements"
(things between semi-colons) on the same line,
if what they are doing is sufficiently related that
it enhances, rather than impedes clarity, and indeed,
that can make a block rather compact...
if (condition) { $foo=0; $bar++; }
But I certainly wouldn't recommend doing *all*
conditional blocks that way. Also, sometimes I
put something after a closing brace, on the same
line, but only if what it's doing is related to
the closing of the block, as in...
open FOO, '<', $filename;
while (<FOO>) { chomp;
my ($bar, $baz, $quux) = $_ =~ /someregex/;
push @quux, [$foo, $baz, $quux] if $quux;
} close FOO;
But again, I certainly wouldn't recommend always
putting code on the same line after the closing
brace. If it weren't logically related to the closing
of the block, that could get very confusing.
If I had to distill this to a general principle, it
would be, take the code's semantics into consideration
when deciding how much whitespace to use and where
to use it.
"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."
— Pratico & Van Pelt, BBHG, p68
| [reply] [d/l] [select] |
|
|
No, I don't mean "always the same way." I'm talking about avoiding inconsistent indentation style like:
if (condition)
{
code;
other_code;
}
else {
bork; }
if (condition_2) {
foo;
}
else
{
i_think_you_get_what_i_mean();
}
I don't peronsally mind if a block is on one line, as long as it's short. if (condition) { $foo=0; $bar++; } is perfectly fine. I mean consistency within a file, given the context. If you always put short blocks on one line, great. If you do something like
if (condition) { $foo=0; $bar++ }
# more code...
if (other_condition) {
$foo++;
}
then I consider it to be inconsistent. And when I find inconsistent indentation it implies to me that your thinking isn't clear. This may or may not be the case -- it's more of an impression I get than an absolute rule. | [reply] [d/l] [select] |
Re: A Question of style.
by itub (Priest) on Jun 07, 2005 at 22:35 UTC
|
What is lamentable is calling someone else's favorite indentation style lamentable. :-) I personally prefer K&R, and find your style extremely ugly; the first thing I do when I'm confronted with code like that is pass it through perltidy.
I won't get into the "reasons" for my preference because they are all subjective, and decades of history have proven that this discussion is futile.
Coming soon: why cherry ice cream sucks and chocolate ice cream rules! | [reply] |
|
|
| [reply] |
Re: A Question of style.
by sfink (Deacon) on Jun 09, 2005 at 06:11 UTC
|
My answer to your #4 would be "no". To me, curly brackets are part of the conditional or looping construct.
And I personally don't find it helpful to have open and close curlies line up vertically. I find it very annoying to scroll to some point in a file and see something like {
...do something...
.
.
}
Huh? Is that part of an if, while, for, or ? Oh, right. The important part is just above the top of the screen. I'd much rather have the close curly line up with the keyword.
But, as many others have pointed out, that's all a matter of religion1. So to actually add something useful to this topic, I'll assert that everyone should try out a couple of different styles. And not just for a file or two. Personally, I have to stick to a style for about a month to get through the full set of phases:
- disgust and revulsion
- grudging acceptance and understanding of how the lunatics who use that style are able to make sense and extract value out of it
- enthusiastic and wholesale adoption of the style for yourself, together with disbelief at how mired you were in your ridiculous old style
- (this one usually comes after going back to editing one of your old projects) complete reversal -- wonderment at how you could possibly have found that new style any good. Sure, it has its advantages, but your original style is just so much better.
- and finally: integration, when you merge parts of the old and new together to make something that is more like one than the other, but takes parts from both.
Or perhaps I'm just a slow learner. But the main thing to come out of this is tolerance. I had a big hang-up for a long time where I just couldn't work on a project written in a style I found abhorrent. Well, I could, but I would first reformat everything, and the original maintainer would ignore all of my patches because they looked like crap to him. Or, if we were forced to work together (eg on a school project), we would be constantly annoyed at each other, and prone to needlessly rewriting swathes of each other's code just to make them "readable"2.
After I (unwillingly) went through a couple of those series of phases I listed above, I actually found it very liberating to be able to shift between styles fluidly to match whatever existing code I encountered. There are too many barriers to working with other people's code already: programming language, NIH syndrome, portability, licensing, .... You do not want style to be yet another one.
Open source projects are great for this, since there are undoubtably several out there that interest you for personal reasons, and aren't written in your preferred style.
1 And what's the one thing that all religions have in common? That's right, they all hold that they are right and all the rest are wrong.
2 On the other hand, this helped my typing speed immeasurably. Whoever could type faster would be able to gradually convert the codebase to the One True Style.
| [reply] [d/l] |
|
|
Good points well made.
I realise that some people would have the {} belong to the enclosing statement. There is a compelling argument for them being part of the block, but I won't go into that here :-). That is about the point that religion enters.
The first three people who worked on our code base had pretty compatible styles. Later additions to the team have had divergent styles. In general that's ok so long as they use their style in their new code, but it becomes a real pain when they start editing old code and convert parts of it to a radically different style. K&R and Whitesmiths just don't go together (see Indentation styles).
Perl is Huffman encoded by design.
| [reply] |
Re: A Question of style.
by TedPride (Priest) on Jun 07, 2005 at 07:22 UTC
|
I generally indent as follows:
if ($x) {
if ($y) {
code;
}
code;
}
else {
code;
}
Though some smaller bits of code can be formatted like this:
if ($x) { code; }
Or even this (since this is Perl):
code if $x;
long section of code
if long test on $x;
The style of indenting depends primarily on the size of the blocks, imho, though there are some indenting methods I hate:
if ($x)
{
code;
}
if ($x)
{ code; }
Etc. The former produces far too much white space; the latter makes for some real problems lining things up.
| [reply] [d/l] [select] |
Re: A Question of style.
by bageler (Hermit) on Jun 07, 2005 at 16:48 UTC
|
what'sthisaboutwhitespaceandindentation?Ifiteverythingononelineandcanreaditfine. | [reply] |
|
|
| [reply] |
Re: A Question of style.
by Juerd (Abbot) on Jun 06, 2005 at 21:18 UTC
|
| [reply] |