Changes between Version 3 and Version 4 of Development/Code/CodingStyle
- Timestamp:
- Apr 23, 2012, 9:25:00 PM (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Development/Code/CodingStyle
v3 v4 48 48 So, some good examples: 49 49 {{{ 50 #!perl 50 51 sub write_user_credentials 51 52 { … … 54 55 }}} 55 56 56 {{{ 57 {{{ 58 #!perl 57 59 for my $i (0..10) { 58 60 # ... … … 63 65 64 66 {{{ 67 #!perl 65 68 sub wuc 66 69 { … … 69 72 }}} 70 73 71 {{{ 74 {{{ 75 #!perl 72 76 for my $counter_from_0_to_10_included (0..10) { 73 77 # Excuse me??? … … 101 105 Good example: 102 106 {{{ 107 #!perl 103 108 use constant PI => 3.141592; 104 109 }}} 105 {{{ 110 {{{ 111 #!perl 106 112 my $circle_area = $radio * PI * PI; 107 113 }}} 108 114 And the bad example 109 115 {{{ 116 #!perl 110 117 # Oops! I missed a decimal somewhere! 111 118 my $circle_area = $radio * 3.14159 * 3.141592; … … 130 137 Bad examples: 131 138 {{{ 139 #!perl 132 140 ############################################################ 133 141 # … … 138 146 }}} 139 147 140 {{{ 148 {{{ 149 #!perl 141 150 sub do_something 142 151 { … … 155 164 These are best done like this: 156 165 {{{ 166 #!perl 157 167 $i++; 158 168 }}} 159 169 160 {{{ 170 {{{ 171 #!perl 161 172 # Performs task foo... 162 173 sub foo {...} … … 189 200 Good: 190 201 {{{ 202 #!perl 191 203 our @ISA = ...; 192 204 }}} … … 194 206 Bad: 195 207 {{{ 208 #!perl 196 209 use vars qw (@ISA); 197 210 @ISA = ...; … … 204 217 own, excepting when it's an else or a do-while block: 205 218 {{{ 219 #!perl 206 220 if ($foo) { 207 221 ... … … 209 223 }}} 210 224 211 {{{ 225 {{{ 226 #!perl 212 227 while ($bar) { 213 228 ... … … 216 231 217 232 {{{ 233 #!perl 218 234 if ($foo) { 219 235 ... … … 223 239 }}} 224 240 225 {{{ 241 {{{ 242 #!perl 226 243 do { 227 244 ... … … 236 253 the same line: 237 254 {{{ 255 #!perl 238 256 sub foo 239 257 { … … 254 272 The reason is that: 255 273 {{{ 274 #!perl 256 275 foo(); 257 276 }}} 258 277 Is easier to understand than: 259 278 {{{ 279 #!perl 260 280 foo; 261 281 }}} … … 281 301 saves you snippets like this: 282 302 {{{ 303 #!perl 283 304 $self->debug (5, "Going to run ls -l"); 284 305 system ("ls", "-l"); … … 287 308 This will become inconsistent with different uses. Instead, do: 288 309 {{{ 310 #!perl 289 311 # Or any reporter object. 290 312 my $proc = CAF::Proces->new (["ls", "-l"], log => $self); … … 303 325 command logged. Just don't pass any log argument to CAF::Process::new: 304 326 {{{ 327 #!perl 305 328 my $proc = CAF::Process->new (["ls", "-l"]); 306 329 $proc->run(); … … 316 339 Create the contents to be piped: 317 340 {{{ 341 #!perl 318 342 my $contents = "Hello, world"; 319 343 }}} 320 344 Create the command, specifying $contents as the input, and "execute" it: 321 345 {{{ 346 #!perl 322 347 my $proc = CAF::Process->new (["cat", "-"], stdin => $contents); 323 348 $proc->execute(); … … 329 354 and need to get its output and error: 330 355 {{{ 356 #!perl 331 357 my ($stdin, $stdout, $stderr) = ("Hello, world", undef, undef); 332 358 my $proc = CAF::Process->new (["cat", "-"], stdin => $stdin, … … 343 369 Create the command: 344 370 {{{ 371 #!perl 345 372 my $proc = CAF::Process->new (["ls", "-lh"]); 346 373 }}} 347 374 And get the output: 348 375 {{{ 376 #!perl 349 377 my $output = $proc->output(); 350 378 }}} … … 356 384 example of what shouldn't be done: 357 385 {{{ 386 #!perl 358 387 open (FH, ">/tmp/foo"); 359 388 }}} … … 367 396 work for you: 368 397 {{{ 398 #!perl 369 399 my $fh = CAF::FileWriter->open ("/my/file", log => $self); 370 400 print $fh "Hello, world!\n"; … … 378 408 You can specify the permissions you want at instantiation, too: 379 409 {{{ 410 #!perl 380 411 my $fh = CAF::FileWriter->open ("/my/path", mode => 0400, log => $self ); 381 412 print $fh "Hello, world!\n"; … … 386 417 the cancel method, and the existing file will be preserved: 387 418 {{{ 419 #!perl 388 420 my $fh = CAF::FileWriter->open ("/my/path", mode => 0400, log => $self); 389 421 print $fh "Hello, world!\n"; … … 429 461 first line of a file: 430 462 {{{ 463 #!perl 431 464 my $fh = CAF::FileEditor->open ("/foo/bar", 432 465 log => $self);