Re: Style, *again*
by Abigail-II (Bishop) on Apr 08, 2003 at 21:25 UTC
|
It would be nice if you also indicated why you use those
rules. Some of your rules are fairly obvious, but from
others, I'd be interesting to hear why you picked them.
In general, I don't find the list of rules themselves
very interesting. What is interesting is knowing why someone
picked the rules.
Abigail | [reply] |
|
|
In general, I don't find the list of rules themselves very interesting. What is interesting is knowing why someone picked the rules.
Short answer: because anything else is evil and not the one true style. :)
But here goes: (I'm lazy, so I'm formatting this as code.)
| [reply] [d/l] |
|
|
| [reply] |
|
|
* No here-documents, but multi-line q/qq. Even repeated prints are better :)
The here-doc end marker cannot be indented, and I don't want something to be outdented in the middle of something that should be indented. Adding the indent spaces to the end marker means code stops working if it changes, which is VERY bad.
That's actually not true about the here-doc end marker not being able to be indented. pgs. 23-26 (ch. 1.11) of the Perl Cookbook give a few examples of how to do that. Below is an example with 4 space indents in front of each of the lines.
(my $text = <<' EOT') =~ s/^\s+//gm;
This is a test of the multi-line, indented HERE
document.
EOT
print qq($text);
This code strips out the leading spaces in the text so
that you don't have extra spaces there. It still has the drawback that if you change the spacing it will break
unless you adjust the <<' EOT' declaration as well, but at least it lines up better in the code.
On another note, great list and thanks for sharing it.
«Rich36» | [reply] [d/l] [select] |
|
|
|
|
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
Re: Style, *again*
by dws (Chancellor) on Apr 08, 2003 at 19:27 UTC
|
Taint mode *only* for setuid programs
Alas, many exploits start with a wiley h4x0r getting their foot in the door.
Logical order in comparisons: $foo == 4, but never 4 == $foo
The latter form is occassionaly useful to quickly signal to the reader that we're interested in array size, not array contents. I.e.,
foo(@stuff) unless 0 == @stuff;
It is, however, a matter of style.
| [reply] [d/l] |
|
|
The latter form is occassionaly useful to quickly signal to the reader that we're interested in array size, not array contents
That doesnt only apply to arrays. When comparing against literals I find using the literal on the left can make for more readable code, as it can emphasise the important part of the conditional. Just like the way it can be more readable to use $_ when doing a lot of regex work on one string. I also find an added advantage of putting the literal first in numeric context is that it avoids the problem of occasionally using "=" instead of "==", which gets caught as an attempt to assign to a read only value instead of (sometimes silently!) overwriting the variable.
---
demerphq
<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
| [reply] [d/l] |
Re: Style, *again*
by thelenm (Vicar) on Apr 08, 2003 at 19:47 UTC
|
Wow, I think your coding style is about the closest to mine that I've ever seen anyone's. We do things differently on a couple of points (I uncuddle my elses, use warnings instead of -w, I interleave my POD, occasionally use here-docs, and prefer pre-increment to post-increment), but 95% agreement is not bad. And you even use Vim. Good style list... it gives some things to think about.
Oh... and I think you probably have one too many negations in No map or grep not in void context, unless you really mean that you always use map in void context. :-)
-- Mike
--
just,my${.02} | [reply] |
|
|
| [reply] |
Re: Style, *again*
by gmpassos (Priest) on Apr 08, 2003 at 20:48 UTC
|
Well, I wan't to share my code style too, but with less rules, since I think that simple is better, and I will explain some:
- 2 spaces for ident.
Because you win horizontal space, is easier, and 2 is just the sufficient to identify the difference
of levels.
- No table (\t) in the code, including index.
Table doesn't work well in all the editors and OS!
- Open block in the same line: if (...) {
- Do not declare else/elsif in the same line of previous close block!:
if (...) {
....
}
else { }
- Blocks, with 1 group of statement, or small blocks, will be in one line:
if (...) { $x++ ;}
- Always put the ";", even when is the last in the block: (use like the previous code).
Because if you put the ";" always, you can add codes after it without care to add the ; in the
prevoius. If you cut the ";" and need to put new codes after it, the probability to forget to check the previous line is
big, and this make bugs!
- In foreach use for the scalar the same name of the array plus "_i":
foreach my $array_i ( @array ) {}
## Note that I have an editor that make this for me! ;-P
- Separate the code in subs, and if make sence build it in OO, or better a Perl Module.
- Spaces between strings and ",": (a , b, c). But not between string and "("! But for variables use the space:
( %hash , a , b)
- Spaces when the code open and close alot "(","{": join("x", length( $hash{ $hash2{k}{k2} } )) ;
- Put ";" separated of the last code: length($foo) ;
Because if you want to add or change the code ";" is already the position, and avoid the wrong cut
of chars:
length($foo) if $foo ;
- Use $i,$j,$k... for index, $c for count, and $s for "tmp" string.
- 1 for true, undef for false, not 0!
- Use $var shift only for objects, and variables that will be used a lot, or when directly access $_[0] doesn't bring speed!
- Always Class->method, never method Class (this includes "new"!). (I agree with this too)
- For q or qq, use "`": $var = q`fooo` ;
Since "`" is not much used for strings! Better than ~,/, or anoy other.
- Always use parens: tie( %hans , 'pack' , arg ) ;
- For returned data always use: return() ;
- Before each sub make a comment box: (My editor make this for me, useful to mark the point and open a area for comments to:)
#######
# FOO # Some comment
#######
sub foo {
...
}
Well, I think that for now is just this!
Graciliano M. P.
"The creativity is the expression of the liberty". | [reply] |
Re: Style, *again*
by zentara (Cardinal) on Apr 09, 2003 at 15:28 UTC
|
I'm just a lowly beginner compared to you guys, here is what I use for "style". :-) Just write whatever you want, or cut'n'paste
code, then run it thru the following script. Presto! Automatic style.
#!/usr/bin/perl
#usage: $0 script
use warnings;
$newfile = $ARGV[0];
$outfile = $newfile . '.tmp';
open( FH, $newfile ) or die $!;
open( OUT, ">> $outfile" );
while (<FH>) {
# $_ =~ s/^\s+//g; #only gets leading whitespace
$_ =~ s/^\s+|\s+$//g; #gets trailing also
#next if $_ =~ /^\s*$/; #removes blank lines
# $_ =~ s/[\t]+//g; #gets tabs and ws inside strings
+
#and leaves \n
print OUT $_ . "\n";
}
close FH;
close OUT;
system( "perltidy", $outfile );
rename( "$outfile.tdy", $newfile );
unlink $outfile;
chmod 0755, $newfile;
exit;
| [reply] [d/l] |
|
|
#!/usr/bin/perl -w
use strict;
my $infile = shift;
my $outfile = "$newfile.tmp";
open my $infh, '<', $infile or die "Cannot open $infile: $!\n";
open my $outfh, '>>', $outfile or die "Cannot open $outfile: $!\n";
while (<$infh>) {
s/^\s+//g;
s/\s+$//g;
print $outfh $_, "\n" or die "Cannot write to $outfile: $!\n";
}
close my $outfh or die "Cannot close $outfile: $!\n";
close my $infh or die "Cannot close $infile: $!\n";
system('perltidy', $outfile) == 0 or die "Perltidy failed\n";
rename "$outfile.tdy", $infile or die "Cannot rename $outfile.tdy: $!\
+n";
unlink $outfile or die "Cannot unlink $outfile: $!\n";
chmod 0755, $infile or die "Cannot chmod $infile: $!\n";
Changes include:
- use strict
- Got rid of all global variables, including file handles
- Modern three argument open to avoid bugs
- Sane indenting instead of an awful mess
- Parens only where needed instead of ... I guess at random
- Error checking on all system stuff. In the real world, many things can go wrong
- Passing a list of $_ and \n to print instead of a concatenated string, for efficiency
- Renaming of 'newfile'. Why 'new' anyway?
I don't understand:
- why you remove whitespace. perltidy does that for you.
- why you chmod the file using a hardcoded value (use the original permissions!)
- why there is no error message that indicates usage if @ARGV != 1
And you should probably not just filter whitespace like that. The whitespace might be in a string literal!
Juerd
- http://juerd.nl/
- spamcollector_perlmonks@juerd.nl (do not use).
| [reply] [d/l] |
|
|
Thanks for "purifying" that script Juerd. :-)
| [reply] |
Re: Style, *again*
by awkmonk (Monk) on Apr 09, 2003 at 15:32 UTC
|
It's good to see you put so much thought into it. ++
Far more important though is not any given style of coding but the conformity
to that style. There's no more heinous crime than to change the way code is
presented half way through a program. Grrrr --.
What would be ideal then, would be to be able to reconfigure how the source
looks when you want to work on it. Hmm something along the lines of a
turbo-charged Perl::Tidy springs to min, driven by a users config file. (The
include/remove unwanted constructors & POD interleaved/at end would be fun
parts to write!).
P.S 3 space indent lines up nicely with if statements :o)
'I think the problem lies in the fact that your data
doesn't fit my program'.
| [reply] |
|
|
In vim you can fold lines. I found it really usefull and I have not tried it - but it should not be difficult to fold POD.
| [reply] |
|
|
P.S 3 space indent lines up nicely with if statements :o)
But, it doesn't align with else, for, while, until and unless.
/prakash
| [reply] [d/l] [select] |
Re: Style, *again*
by halley (Prior) on Apr 10, 2003 at 20:18 UTC
|
My style is easier seen than described, but I'd say my general rules are as follows.
- reads like natural language where appropriate: few abbreviations or multiple-crammed-words for names, postfix if when it reads better, method before Class when it reads better,
- interleave pod: name, synopsis, optional abstract, {globals}, {helpers}, methods/functions {pod, code, ...}, description, see also, license/author,
- help the smart editor within reason: curlies aligned vertically, not afraid to use vertical space, escape properly for weaker color-coders,
- hash+dash*76 visual cut-here lines to delineate logical sections,
- standard #TODO: #REVIEW: #HACK, #BUG: comment keywords for future planning and scanning,
My perltidy does the token-level style: http://www.halley.cc/ed/linux/configs/
See a reasonable example of my style on CPAN in Data::Binder or Data::Favorites.
e d @ h a l l e y . c c | [reply] |
Re: Style, *again*
by Mr. Muskrat (Canon) on Apr 22, 2003 at 17:15 UTC
|
"Fashion is a variable, but style is a constant." -- Programming Perl 3rd Ed., page 628. | [reply] |
Re: Style, *again*
by g00n (Hermit) on Apr 11, 2003 at 06:17 UTC
|
why ???
- Extra spaces in arrayref constructor: [ foo, bar ]
- Extra spaces in hashref constructor: { foo => bar }
It just takes up space and not necessarily easier to read. why not ...
- Extra spaces in arrayref constructor: [foo, bar]
- Extra spaces in hashref constructor: {foo => bar}
| [reply] [d/l] [select] |
|
|
It just takes up space and not necessarily easier to read.
I disagree. In my opinion, they are easier to read. Besides that, I want them to look very different since they construct new data structures, while parentheses are only for grouping.
Juerd
- http://juerd.nl/
- spamcollector_perlmonks@juerd.nl (do not use).
| [reply] |
|
|
I want them to look very different since they construct new data structures, while parentheses are only for grouping
I'll buy into that.
| [reply] |