Changes between Version 3 and Version 4 of Development/Code/CodingStyle


Ignore:
Timestamp:
Apr 23, 2012, 9:25:00 PM (12 years ago)
Author:
munoz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Development/Code/CodingStyle

    v3 v4  
    4848So, some good examples:
    4949{{{
     50#!perl
    5051 sub write_user_credentials
    5152 {
     
    5455}}}
    5556
    56 {{{
     57{{{
     58#!perl
    5759 for my $i (0..10) {
    5860     # ...
     
    6365
    6466{{{
     67#!perl
    6568 sub wuc
    6669 {
     
    6972}}}
    7073
    71 {{{
     74{{{
     75#!perl
    7276 for my $counter_from_0_to_10_included (0..10) {
    7377     # Excuse me???
     
    101105Good example:
    102106{{{
     107#!perl
    103108 use constant PI => 3.141592;
    104109}}}
    105 {{{
     110{{{
     111#!perl
    106112 my $circle_area = $radio * PI * PI;
    107113}}}
    108114And the bad example
    109115{{{
     116#!perl
    110117 # Oops! I missed a decimal somewhere!
    111118 my $circle_area = $radio * 3.14159 * 3.141592;
     
    130137Bad examples:
    131138{{{
     139#!perl
    132140 ############################################################
    133141 #
     
    138146}}}
    139147
    140 {{{
     148{{{
     149#!perl
    141150 sub do_something
    142151 {
     
    155164These are best done like this:
    156165{{{
     166#!perl
    157167 $i++;
    158168}}}
    159169
    160 {{{
     170{{{
     171#!perl
    161172 # Performs task foo...
    162173 sub foo {...}
     
    189200Good:
    190201{{{
     202#!perl
    191203 our @ISA = ...;
    192204}}}
     
    194206Bad:
    195207{{{
     208#!perl
    196209 use vars qw (@ISA);
    197210 @ISA = ...;
     
    204217own, excepting when it's an else or a do-while block:
    205218{{{
     219#!perl
    206220 if ($foo) {
    207221     ...
     
    209223}}}
    210224
    211 {{{
     225{{{
     226#!perl
    212227 while ($bar) {
    213228     ...
     
    216231
    217232{{{
     233#!perl
    218234 if ($foo) {
    219235     ...
     
    223239}}}
    224240
    225 {{{
     241{{{
     242#!perl
    226243 do {
    227244     ...
     
    236253the same line:
    237254{{{
     255#!perl
    238256 sub foo
    239257 {
     
    254272The reason is that:
    255273{{{
     274#!perl
    256275 foo();
    257276}}}
    258277Is easier to understand than:
    259278{{{
     279#!perl
    260280 foo;
    261281}}}
     
    281301saves you snippets like this:
    282302{{{
     303#!perl
    283304 $self->debug (5, "Going to run ls -l");
    284305 system ("ls", "-l");
     
    287308This will become inconsistent with different uses. Instead, do:
    288309{{{
     310#!perl
    289311 # Or any reporter object.
    290312 my $proc = CAF::Proces->new (["ls", "-l"], log => $self);
     
    303325command logged. Just don't pass any log argument to CAF::Process::new:
    304326{{{
     327#!perl
    305328 my $proc = CAF::Process->new (["ls", "-l"]);
    306329 $proc->run();
     
    316339Create the contents to be piped:
    317340{{{
     341#!perl
    318342 my $contents = "Hello, world";
    319343}}}
    320344Create the command, specifying $contents as the input, and "execute" it:
    321345{{{
     346#!perl
    322347my $proc = CAF::Process->new (["cat", "-"], stdin => $contents);
    323348$proc->execute();
     
    329354and need to get its output and error:
    330355{{{
     356#!perl
    331357 my ($stdin, $stdout, $stderr) = ("Hello, world", undef, undef);
    332358 my $proc = CAF::Process->new (["cat", "-"], stdin => $stdin,
     
    343369Create the command:
    344370{{{
     371#!perl
    345372 my $proc = CAF::Process->new (["ls", "-lh"]);
    346373}}}
    347374And get the output:
    348375{{{
     376#!perl
    349377 my $output = $proc->output();
    350378}}}
     
    356384example of what shouldn't be done:
    357385{{{
     386#!perl
    358387 open (FH, ">/tmp/foo");
    359388}}}
     
    367396work for you:
    368397{{{
     398#!perl
    369399 my $fh = CAF::FileWriter->open ("/my/file", log => $self);
    370400 print $fh "Hello, world!\n";
     
    378408You can specify the permissions you want at instantiation, too:
    379409{{{
     410#!perl
    380411 my $fh = CAF::FileWriter->open ("/my/path", mode => 0400, log => $self );
    381412 print $fh "Hello, world!\n";
     
    386417the cancel method, and the existing file will be preserved:
    387418{{{
     419#!perl
    388420 my $fh = CAF::FileWriter->open ("/my/path", mode => 0400, log => $self);
    389421 print $fh "Hello, world!\n";
     
    429461first line of a file:
    430462{{{
     463#!perl
    431464 my $fh = CAF::FileEditor->open ("/foo/bar",
    432465                                 log => $self);