Re: Different ways of formatting/writing code
by almut (Canon) on Dec 09, 2009 at 15:46 UTC
|
IMHO, it's more important to not use prototypes (the parentheses between function name and opening curly), unless you have a good reason to do so... ;)
| [reply] |
Re: Different ways of formatting/writing code
by moritz (Cardinal) on Dec 09, 2009 at 15:36 UTC
|
It's of course personal taste. I put the { on the same line as sub keyword, because I don't think it's important enough to warrant a line on its own. It also makes the code more compact, but that's secondary to me.
I also put a space before the opening curly bracket.
See perlstyle for some popular choices within the Perl community.
Update: after thinking a bit more about it, the right answer is probably that I use this style because the books I learned from used it, and it works well for me. | [reply] [d/l] [select] |
|
|
I agree totally agree to your update: it depends on how you "grew up".
For me it was learning C by reading the "bible" (K&R) as well as other books - and I really disliked the K&R-style (no own line for the opening brackets).
And it were the tools I used for coding then (vi (without syntax-highlightning ...)) - I often got errors due to missing brackets ... and found it much easier to find them if the opening brackets are on a line on their own...
Fortunately, my current company has not defined a style-guide on this topic - my previous had, which led to endless discussion (which style is the better one, how to enforce it ...) without making the code any better.
| [reply] |
Re: Different ways of formatting/writing code
by Old_Gray_Bear (Bishop) on Dec 09, 2009 at 19:11 UTC
|
I use the 'cuddled else' style for subroutines, else-constructs, eval-blocks, actually for blocks in general. I indent the content of a block by one 'tab' stop (four spaces) and out-dent the terminating curly-brace on its own separate line foe visual distinction. That is my personal preference.
My text-editor (Vim with a set of macros; thanks Damian, Randal, Uri, Anonymmous, and others) does a fair job of highlighting Perl source code and has an auto-bounce-on-the-%-key feature that shows were the closing character matches up. Add that to a perl-tidy() run every week or so, and I have a reasonably easy tool chain I can use to enforce my coding style.
The important thing to remember is that "Coding Style" is a convention used to make your code easier for the Maintenance Programmer to understand. There is no one 'Holy and Sacred Way'; no Tao.
Like text editor choice, Style is good for discussions over Beer, but there is no one-true-way. (Rudyard Kipling said it better than I can -- "There are nine and sixty ways of constructing Tribal Lays, and every single one of them is right!", from 'In the Neolithic Age')
The worst thing you can do is *not* to have a consistent style. Pick a style of bracing and indentation that works for the way you feel comfortable writing your code a stick with it. Set up your editor of choice to produce that style and build a perl-tidy() configuration to regenerate code in that style. The MP will thank you for it. (And, there is nothing more embarrassing than picking a five-year-old piece of crufty old code, discovering that the coding style is "HidJe-ous! The indenting is inconsistent, and the bracing is **all** wrong!!", and then realizing who wrote it....)
----
I Go Back to Sleep, Now.
OGB
| [reply] |
|
|
| [reply] |
|
|
I think the most important thing is to pick a style and stick to it religiously.
I stick religiously to, and hotly defend, my personal standards—until someone convinces me that they've got better ones, at which point I adopt those with born-again passion and deride those who use the silly old convention.
Just checking: It is true that, once you find the One True Way of indenting, good code writes itself, right?
| [reply] |
|
|
Re: Different ways of formatting/writing code
by BioLion (Curate) on Dec 09, 2009 at 15:38 UTC
|
Perl code doesn't *have* to be written in a particular format or style, at least regarding indentation or (sometimes) your choice of delimiters (c.f my @array = qw/1 2 3/ or my @array = qw(1 2 3) etc...). However there is a certain 'style' that is encouraged, to help your code be readable and organised, and sometimes to encourage good habits. This is much better explained and detailed in perlstyle. For examples of how far you can get from this and still produce working code see Obfuscated code!
Just a something something...
| [reply] [d/l] [select] |
Re: Different ways of formatting/writing code
by FunkyMonk (Bishop) on Dec 09, 2009 at 16:09 UTC
|
When I was a C programmer, I always wrote code indented as in your first example, just because I liked to have the opening and closing braces lined up. I carried on using that style when I moved over to Perl, but I changed to your second example some years ago, purely because I believed it to be more common.
Wikipedia has other examples of indent styles, should you wish to read more. | [reply] |
Re: Different ways of formatting/writing code
by Marshall (Canon) on Dec 09, 2009 at 16:16 UTC
|
I personally like your style of:
sub function_A
{
....
}
because human beings can see patterns better vertically rather than diagonally. There is a history behind this { on the same line stuff. I used to do it that way too, but I have changed my "evil" ways. I think that vertical paren alignment is better, but of course the language allows you do it either way.
The idea of function prototypes in Perl is a bit more dubious, but I don't think that was your question. Perl allows that too, but that doesn't necessarily mean that it is a good idea. | [reply] [d/l] |
|
|
Whereas I think that's confusing: The { is just visual noise, and I'd rather it were out of the way by putting it on the line above. That way I can focus on actual pattern: It starts at the sub/if/loop, and ends at the brace. (To me.)
But it's all personal opinion. The one thing not to do is to mix styles. If you use one in a particular source file/project, use it throughout. That way people know what to expect when they read/skim the code.
| [reply] [d/l] |
Re: Different ways of formatting/writing code
by gmargo (Hermit) on Dec 09, 2009 at 17:05 UTC
|
<religious_war>
Just do it my way, then you will never be wrong.
</religious_war>
| [reply] [d/l] [select] |
Re: Different ways of formatting/writing code
by rowdog (Curate) on Dec 10, 2009 at 01:51 UTC
|
sub foo {
print "do stuff";
}
but I'm old and I grew up on K&R. As others have said, pick a style you like and stick to it.
| [reply] [d/l] |
|
|
Ok, thank you all for your replies, that was some good reading:-) I understand that the program itself doesn't care which style I use (as long as the syntax is correct). I'm more interested in finding the optimal way of writing code that is easily maintainend.
| [reply] |
Re: Different ways of formatting/writing code
by Anonymous Monk on Dec 10, 2009 at 09:14 UTC
|
| [reply] |