Red Flags in Programming
-
Upload
xsawyer -
Category
Technology
-
view
2.755 -
download
0
Transcript of Red Flags in Programming
Red Flags in Programming
Doing things that are probably...not good for us
Some words of caution...
- We Perlers have a saying...TIMTOWTDI- Be nice, no flaming(only I'm allowed)- Not a Perl lecture, more like badprogramming habits- Your mileage may vary, no batteriesincluded, don't drink and drive
Flag #1:
Repeated code
- We overlook repeated patterns without even noticing- N more to read, understand, debug, test, maintain- N more places to have bugs!- Updating multiple places is error prone..- it's boring- distracting- lose focus- make mistakes- OMFG BUGZ!- Really, the worst thing a programmer can do
Flag #1:
Repeated code
- Abstract your code... correctly!- Class?- Abstract Class? (role)- Package?- Collection of Functions?- Loops?- Switches?- Dispatch tables?
Flag #2:
Reinvent the Wheel
- You probably won't do it better... seriously- Development and maintenance grows because you now have another (usually big) chunk of code- It's just repeated code, really...
Flag #2:
Reinvent the Wheel
- Modules- Libraries- Roles- Frameworks- Whatever the hell [Free] Pascal has- Write patches for what doesn't work for you- In extreme cases reinvent, but try to implementas little as required.- Sometimes it's necessary to do it all from scratch:Perlbal, Git, Linux :)
Flag #3:
Switches without default
- Not always wrong- Usually wrong- Unexpected behavior- Not the worst of things...but please think of it
if ( is_alpha($num) ) {}elsif ( is_numeric($num) ) {}
Flag #3:
Switches without default
if ( is_alpha($num) ) {}elsif ( is_numeric($num) ) {}else {}
Flag #4:
Long Switches
- Gross, really- Not fun to read or debug- Not easily maintainable- It's basically a long if() elsif()... even if seems otherwise
given ($foo) { when (/^10$/) {} when (/^20$/) {} when (/^30$/) {} default {}}
Flag #4:
Long Switches
- Dispatch tables(if you can)- Use code references in switches instead of code itself(if you can)
my %dispatch = (10 => CODEREF,20 => CODEREF,30 => CODEREF,);
$dispatch{$num}->();
Flag #5:
Try and Catch (workflow)
- Try and Catch isn't for workflow!!- That's what we have conditions and loops for- Internal functions should have return codes, not throw exceptions- PHP is fscking stupid- Try and Catch is for when external functions might crash the program
try {do_something_simple();} catch { print Dear god, this is a really stupid thing to do!;}
Flag #5:
Try and Catch (workflow)
- Functions, subroutines, return codes!- Try and Catch is for external programs or things that are suppose to crash
do_something_simple()or die you suck!\n;
Flag #6:
String Booleans
- The string false is actually true => confusing!- Unnecessary value check/comparison- Misses the point of booleans.
if ( $bool eq 'true' ) { DO SOMETHING} elsif ( $bool eq 'false' ) { DO SOMETHING}$bool = 'false';if ( $bool ) { BUG }
Flag #6:
String Booleans
- Use real booleans- Use zero (0), empty strings and undefined variables- Sometimes you can't control it (using modules, etc.)
$bool = do_that(@params);
if ($bool) { # wheee... actual booleans}
Flag #7:
External Binaries
- Compatibility problems- Portability problems- Unexpected results (`ps` for example is different on BSD and has different command line switches)- Insecure!
Flag #7:
External Binaries
- Shared libraries- Bindings- APIs- libcurl is an example- Modules for running binaries more safely and controllably (IPC::Open3, IPC::Open3::Simple, IPC::Cmd, Capture::Tiny)- Taint mode (if language supports it Perl does!)- Sometimes you can't control it (external binaries, closed programs, dependencies at $work)
Flag #8:
Intermediate Programs
- Quite possibly insecure- Hard to maintain- No damned syntax highlighting!!!11
Flag #8:
Intermediate Programs
- If same language, use a subroutine/function- Different language && use an Inline module- Else, use templates- External file
Flag #9:
Empty if() Clauses
- Not DWIM/SWYM- Having an empty if() for the sake of the else().. is horrible
if ( $something ) { # do nothing} else { CODE}
Flag #9:
Empty if() Clauses
- SWYM... please?- Use unless() [Perl]- Don't really use unless()
if ( !$something ) { CODE}unless ( $something ) { CODE}
* Shlomi Fish is allowed to do otherwise :)
Flag #10:
Array Counters
- Some older languages have no way to know how many elements are in an array...- Some people are too used to older (more low-level) languages- Some people don't know there's a better way
foreach my $i ( 0 .. $n ) {$array[$i] = 'something';$array_counter++;}
Flag #10:
Array Counters
- Higher level languages have no problem- Using the number of the last element- The number of elements
print $#array + 1;
print scalar @array;
Flag #11:
Variable abuse
- Are you kidding me?- Awful awful awful- The reasons people get their hands chopped off in indigenous countries
sub calc_ages {my ( $age1, $age2, $age3, $age4 ) = @_;}
calc_ages( $age1, $age2, $age3, $age4 );
Flag #11:
Variable Abuse
- That's why we have compound data structures (arrays, hashes)- Even more complex data structures, if you want
my @ages = @_;my %people = (dad => {brothers => [],sisters => [],},},};
Flag #12:
C Style for() Loop
- Not really an issue- Harder to understand- And just not needed
for ( $i = 0; $i < 10; $i++ ) {}
Flag #12:
C Style for() Loop
- Higher level languages have better/cooler things- foreach (where available)
foreach my $var (@vars) {...}
Flag #13:
Goto Hell
- OMGWTF?!1- But seriously folks, goto() strips away all of our ability to profoundly express ourselves using languages that finally let us- And yes, if you use goto(), I also think your mother is promiscuous
if ( something_happened() ) {WHINE: say_it('happened');my $care = your($feelings);if ( !$care ) {goto WHINE;}}
Flag #13:
Goto Hell
- DON'T USE GOTO!- You're not a hardcore assembly programmer, you shouldn't have spaghetti code- Even xkcd agrees with me!
if ( something_happened() ) {my $care;while ( !$care ) {say_it('happened');$care = your($feelings);}}
some good stuff
Dry, Rinse, Repeat
DRY (Don't Repeat Yourself)
Don't duplicate code, abstract it!
KISS (Keep It Simple, Stupid)
Don't write overtly complex stuff. Clean design yields clean code. (yet some things are complex, I know...)
YAGNI (You Aren't Gonna Need It)
Start from what you need. Work your way up.
Thank you.