BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 ·...
Transcript of BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 ·...
![Page 1: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/1.jpg)
Best Programming Prac/ces
Ashish Mahabal, Caltech Ay/Bi 199b 31 Mar 2011
![Page 2: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/2.jpg)
The zen of bug-‐free programming • If debugging is the process of removing bugs, programming must be the process of introducing them. -‐ Edsger W. Dijkstra (1930-‐2002)
Don’t program!
![Page 3: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/3.jpg)
Obfuscated programming contests
• touch selfreproducingprogram.c • makefile:
• cp selfreproducingprogram.c a.out • chmod 755 a.out
• ./a.out
![Page 4: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/4.jpg)
• Programming style • Programming tools
My own experience/mistakes
The Pragma/c Programmer By Andrew Hunt and David Thomas
Perl Best Prac/ces By Damian Conway
![Page 5: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/5.jpg)
The scene keeps changing • Drupal hXp://drupal.org/node/287350 • Django hXp://www.djangoproject.com/ • iphone apps hXp://mashable.com/2009/06/10/build-‐iphone-‐app/ • Android apps: hXp://developer.android.com/guide/webapps/best-‐prac/ces.html
• Chrome extensions: hXp://blog.chromium.org/2010/06/making-‐chrome-‐more-‐accessible-‐with.html
… and yet the basics stay the same
![Page 6: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/6.jpg)
Coding by ins/nct
• Variable names (caps, underscores, …) • Types of loops (for, while, …) • Forma^ng – Indents, brackets, braces, semicolons
• Procedural versus object oriented approach
Conscious and consistent programming style
![Page 7: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/7.jpg)
Necessary ingredients
• Robustness • Efficiency • Maintainability
![Page 8: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/8.jpg)
Robustness
• Introducing errors – checking for existence (uniform style)
• Edge cases – 0? 1? last?
• Error handling – excep/ons? Verifying terminal input
• Repor/ng failure – Traces? Errors don’t get quietly ignored
![Page 9: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/9.jpg)
Efficiency
• Working with strength • Proper data structures • Avoiding weaknesses • Dealing with version changes (backward compa/bility)
![Page 10: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/10.jpg)
Maintainability
• More /me than wri/ng • You don’t understand your own code • You yourself will maintain it • Consistent prac/ces – Braces, brackets, spaces – Semicolon (ager last statement) – Trailing , in lists – Linelengths, tabs, blank lines
• cb, bcpp, perl/dy, jacobe, Jxbeauty
![Page 11: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/11.jpg)
• my @countries = ( USA, UK, UAE, Ukraine );
• my @countries = ( USA, UK, UAE, Ukraine, );
![Page 12: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/12.jpg)
• every piece of code splits the universe in two possibili/es.
• which future are you coding for? • if your code is most consistent, you have least to fear.
![Page 13: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/13.jpg)
Some simple recommenda/ons • Use underscores
– $tax_form rather than $taxForm • Don’t use abbrvs
– don’t drop all vowels if you do • Don’t use single leXer variable names
– Except perhaps in trivial small loops • Don’t use too common words as variable names
– e.g. no, yes, count, leg, okay • Empty strings: name and use them
– my $empty_string = “ “; • Constants: use Readonly
– my READONLY $PI = 3;
![Page 14: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/14.jpg)
• easy development versus easy maintenance – projects live much longer than intended – adopt more complex and readable language
• check requirements • design, implement, integrate • validate
![Page 15: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/15.jpg)
• Don’t trust the work of others – Validate data (numbers, chars etc.) – Put constraints (-‐90 <= dec <= 90) – Check consistency
![Page 16: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/16.jpg)
• Don’t trust the work of others – Validate data – Put constraints – Check consistency
• Don’t trust yourself – Do all the above to your code too
![Page 17: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/17.jpg)
Design by contract (Eiffel, Meyer ’97)
• Precondi/ons • Postcondi/ons • Class invariants Be strict in what you accept Promise as liXle as possible Be lazy
Inheritance and polymorphism result
![Page 18: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/18.jpg)
• Crash early – Sqrt of nega/ve numbers (require, ensure, NaN)
• Crash, don’t trash – Die – Croak (blaming the caller) – Confess (more details) – Try/catch (own error handlers e.g. HTML 404)
• Excep/ons – when to raise them – should it have existed? – Don’t know?
![Page 19: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/19.jpg)
sub locate_and_open { open my $w,’<‘,”filename”; return $w; }
sub load_header_from { TRY TO READ HEADER HERE }
my $w = locate_and_open($filename); my $head = load_header_from($w);
![Page 20: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/20.jpg)
sub locate_and_open { open my $w,’<‘,”filename” or croak “cant”; return $w; }
my $w = locate_and_open($filename); my $head = load_header_from($w);
![Page 21: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/21.jpg)
If(my $w = eval { locate_and_open($filename)}){ my $head = load_header_from($w); }
else{ carp “Couldn’t access $filename.\n”; }
![Page 22: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/22.jpg)
• Tests • Comments • Arguments • Debugging
![Page 23: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/23.jpg)
Tests • Test against contract – Sqrt: nega/ve, zero, string – Testvalue(0,0) – Testvalue(4,2) – Testvalue(-‐4,0) – Testvalue(1.e12,1000000)
• Test harness – Standardize logs and errors
• Test templates • Write tests that fail
hXp://ib.ptb.de/8/85/851/sps/swq/graphix
![Page 24: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/24.jpg)
All sogware will be tested
• If not by you, by other users! – perl Makefile.pl – make – make test – make install
• Don’t use code you do not understand
![Page 25: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/25.jpg)
Source Code Control
• SVN – Checkin – Checkout – Comment – Merge
hXp://img.idealwebtools.com/blog/svn.gif
Git, google docs, wiki, trac
![Page 26: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/26.jpg)
Modifica/on cycle
• write test • run and make sure it fails • Checkout • change, comment, edit readme etc. • Compile • run: make sure test passes • checkin
![Page 27: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/27.jpg)
Comments
• If it was difficult to write, it must be difficult to understand
• bad code requires more comments • tying documenta/on and code • use Euclid;
![Page 28: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/28.jpg)
Documenta/on/comments in code
• List of func/ons exported • Revision history • List of other files used • Name of the file
![Page 29: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/29.jpg)
Documenta/on
• Algorithmic: # full line comments to explain the algorithm • Elucida/ng: # end of line comments • Defensive: # Has puzzled me before. Do this. • Indica/ve: # This should rather be rewriXen • Discursive: # Details in POD
![Page 30: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/30.jpg)
Arguments
• Don’t let your subrou/nes have too many arguments – universe(G,e,h,c,phi,nu)
• Look for missing arguments • Set default argument values
• Use explicit return values
![Page 31: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/31.jpg)
Needing/demanding arguments
• unless(@ARGV==4){exit;} • my ($a,$b,$c,$d) = @ARGV;
use Getopt::Euclid; # not just demands arguments
# but provides constraints
![Page 32: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/32.jpg)
PROMPT> pq_images.pl Missing required arguments: -‐r[a] [=] <RA> -‐d[ec] [=] <Dec> (Try: pq_images.pl -‐-‐help) PROMPT>
![Page 33: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/33.jpg)
PROMPT> pq_images.pl -‐-‐help Usage: pq_images.pl -‐r <RA> -‐d <Dec> [op/ons]
Required arguments: -‐r[a] [=] <RA> Specify RA in degrees [0 <= RA <= 360]
-‐d[ec] [=] <Dec> Specify Dec in degrees [PQ: -‐25 <= Dec <= 25]
Op/ons: -‐i[d] [=] <id> [string] ID of the object
![Page 34: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/34.jpg)
-‐c[leanup] [=] <cleanup> Level of cleanup ager the program is done [default: 2] 0: Do not remove anything 1: Remove everything except individual mosiacs (and final product) 2: Leave only final coadded image
-‐v -‐-‐verbose Print all warnings
-‐-‐version -‐-‐usage -‐-‐help -‐-‐man Print the usual program informa/on PROMPT>
![Page 35: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/35.jpg)
PROMPT>pq_images.pl -‐-‐man AUTHOR Ashish Mahabal <[email protected]>
BUGS There are undoubtedly serious bugs lurking somewhere in this code. Bug reports and other feedback are most welcome.
COPYRIGHT Copyright (c) 2007, Ashish Mahabal. All Rights Reserved. This module is free sogware. It may be used, redistributed and/or modified under the terms of the Perl Ar/s/c License (see hXp://www.perl.com/perl/misc/Ar/s/c.html)
![Page 36: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/36.jpg)
use Getopt::Euclid; … =head1 REQUIRED ARGUMENTS =over =item -‐r[a] [=] <RA> Specify RA in degrees [0 <= RA <= 360] =for Euclid: RA.type: number >= 0 RA.type: number <= 360 =item -‐d[ec] [=] <Dec> Specify Dec in degrees [PQ: -‐25 <= Dec <= 25] =for Euclid: Dec.type: number >= -‐25 Dec.type: number <= 25 =back
![Page 37: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/37.jpg)
Debugging
• there will be bugs! • the only bugfree program is one that does not do anything
• tests: write unit tests first • make sure the program compiles without warnings (perl -‐c)
![Page 38: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/38.jpg)
• make bugs reproducible (with a single command)
• visualize the data • ddd or perl -‐d • Breakpoints • use Smart::Comments;
hXp://www.gnu.org/sogware/ddd/plots.png
![Page 39: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/39.jpg)
use Smart::Comments;
### seeing: $seeing ### calcmag: $cmag ### calcmag2: $cmag2;
![Page 40: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/40.jpg)
When you find a bug …
• check boundary condi/ons – first and last elements of lists
• describe the problem to someone else • why wasn't it caught before • could it be lurking elsewhere (orthogonality!) • if tests ran fine, are the tests bad?
![Page 41: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/41.jpg)
• (non)Duplica/on • Orthogonality • Refactoring
![Page 42: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/42.jpg)
Duplica/on
• Don't repeat yourself • Impa/ence • Reinven/ng wheels
![Page 43: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/43.jpg)
Orthogonality
• Decouple rou/nes • Make them independent • Change in one should not affect the other • Changes are localized • Unit tes/ng is easy • Reuse is easy • If requirements change for one func/on, how many modules should be affected? 1
• Configurable
![Page 44: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/44.jpg)
sub line{ my ($startpoint, $endpoint, $length); … }
![Page 45: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/45.jpg)
![Page 46: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/46.jpg)
• if while entertaining libraries you need to write/handle special code, it is not good.
• avoid global data • avoid similar func/ons • even if you are coding for a par/cular flavor of a par/cular OS, be flexible
![Page 47: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/47.jpg)
Refactoring
• Early and ogen – Duplica/on – Non-‐orthogonal design – Outdated knowledge – Performance
• Don’t add func/onality at the same /me • Good tests • Short deliberate steps
![Page 48: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/48.jpg)
Por�olio building • learn general tools, invest in different ones
– plain text • easier to test (config files, for instance)
– Shells • find, sed, awk, grep, locate • .tcshrc, .Xdefaults
– learn different (types of) languages – Editor
• if you know emacs, learn just a liXle bit of vi • Configurable, extensible, programmable (cheat sheet)
– syntax highligh/ng – auto comple/on – auto indenta/on – Boilerplates – built-‐in help
![Page 49: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/49.jpg)
• Text manipula/on – perl and ruby are very powerful
![Page 50: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/50.jpg)
Metaprogramming
• Configure • Abstrac/on in code, details in metadata – Decode design – Pod files (plain old documenta/on)
![Page 51: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/51.jpg)
• Code generators – make files, config files, shell scripts., …
• Ac/ve code generator: – Skyalert (streams)
• new transient • obtain new data • incorporate it • if certain condi/ons met,
– run other programs – or raise alerts – drive other telescopes – and obtain feedback
![Page 52: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/52.jpg)
Workflow
• Improving concurrency • Unified Modeling Language (UML) diagrams • Architecture – Ac/on – Synchroniza/on – Connect ac/ons
![Page 53: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/53.jpg)
![Page 54: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/54.jpg)
Publish-‐subscribe rather than push
• Allow people to subscribe • Let them subselect • Allows separate view of model
Skyalert hXp://www.skyalert.org
![Page 55: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/55.jpg)
![Page 56: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/56.jpg)
Before the project
• Dig for requirements • Document requirements • Make use case diagrams • Maintain a glossary • document
![Page 57: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/57.jpg)
• Don’t op/mize code – benchmark it • Don’t op/mize data structures – measure them
• Cache data when you can – use Memoize • Benchmark caching strategies • Don’t op/mize applica/ons – profile them (find where they spend most /me)
![Page 58: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/58.jpg)
use Benchmark qw( cmpthese );
my @sqrt_of = map {sqrt $_} 0..255;
cmpthese -‐30, { recompute => q{ for my $n (0..255) { my $res = sqrt $n } },
look_up_array => q{ for my $n (0..255) { my $res = $sqrt_of[$n] } },
};
![Page 59: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/59.jpg)
Summarizing …
• Sogware entropy – Fix broken windows
• Know when to stop – Don’t overperfect
• Widen knowledge por�olio – Hotjava – Postscript – vi/emacs
![Page 60: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/60.jpg)
• Languages/tools/OSes/editors – 99 boXles of beer – Programming shootout – Project Euler • Python • Perl • J • Haskell
![Page 61: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/61.jpg)
Whats the lesson?
• Chain as weak as its weakest link • Comment! For others and for yourself • Tests! • Orthogonality • Don’t duplicate • Designing by contract • Know the features
![Page 62: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/62.jpg)
• Review/balance – Public forums • Ask specific things • Check FAQs, webresults etc.
– Maintain your own bookmarks
• Use wikis • Use SVN, trac • CHECK REPOSITORIES (like CPAN)
![Page 63: BestProgramming%Prac/ces%george/aybi199/Mahabal... · 2011-04-05 · Obfuscated%programming%contests% • touch%selfreproducingprogram.c% • makefile: • cp%selfreproducingprogram.c%a.out%](https://reader035.fdocuments.us/reader035/viewer/2022070823/5f2badab80ccbe5e323e2875/html5/thumbnails/63.jpg)
• Law 1: Every program can be op/mized to be smaller.
• Law 2: There's always one more bug. • Corollary: Every program can be reduced to a one-‐line bug.
From a Bug’s life