Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
KISS - one action per statement please - Perl

"Nice program, Bill - but the output is a bit verbose. Can you take out some print statements". So said one of my delegates' bosses this week, late in the day just before a new application went live. Alas ... the application fell over just because the print had been removed.

In Perl - that was the language in use - you can assign variables within your print statement, and that's what had been done. So taking out the print caused it to fall over. What you can do and what you should do are two different things, and it requires a thoughtful programmer and an excellent manager to the make the best of coding, apply standards and end up with a maintainable program that fits the user's requirement well.

Sample code - what NOT to write:
foreach $value (1..5) {
  print ("... ");
  print ("Value is now ",$sum+=$value,"\n");
  }
print ("Triangle 5 is $sum\n");


Without the print it becomes:
foreach $value (1..5) {
  print ("... ");
  }
print ("Triangle 5 is $sum\n");


When those are run:
grahamellis$ perl avoidthis
... Value is now 1
... Value is now 3
... Value is now 6
... Value is now 10
... Value is now 15
Triangle 5 is 15
grahamellis$ perl liketheplague
... ... ... ... ... Triangle 5 is
grahamellis$


Does this just apply to print statements? No - anywhere you're undertaking two actions in a single statement is liable to come back an bite you late on.

Don't write $r = $p++; as it changes two variables - much clearer to write $r = $p; $p++;.

Don't use printf. PLEASE. As it both formats and prints. Use instead sprintf and then do a separate print. That way, you have the later flexibility to apply further string manipulation - for example if data originally destined for a command window is now to be output to a browser.

You should not write:
printf ("< %.2f",$maxprice);

But rather:
$poundamount = sprintf ("< %.2f",$maxprice);
print $poundamount;


Because you can enhance the latter as follows:
$poundamount = sprintf ("< %.2f",$maxprice);
$poundamount =~ s/</&lt;/g;
print $poundamount;


We (or rather "I" - I'll take the blame) made the mistake on a previous version of the Well House Consultants web site too - in HTML. The image on the left is 132 pixels wide, and I wanted an 8 pixel space to the right of it. What easier than to add the 8 white pixels to the images? So that's what I did and built up quite an image library ... to find my self well and truely in trouble when we wanted to add the ability to change background colour for users. We ended up, temporarily, with a dirty looking white band to the right of every image and it was only fixed by a rather messy procedure to - guess what - replace the single image with two functions with two images each with a single function.

Are you familiar with KISS (post title) ... if not, see next post!
(written 2006-12-05)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P711 - An Introduction to Standards in Perl
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [2875] A long day in Melksham ... - (2010-07-17)
  [2688] Security considerations in programming - what do we teach? - (2010-03-22)
  [2375] Designing your data structures for a robust Perl application - (2009-08-25)
  [1863] About dieing and exiting in Perl - (2008-11-01)
  [1853] Well structured coding in Perl - (2008-10-24)
  [1728] A short Perl example - (2008-07-30)
  [1555] Advanced Python, Perl, PHP and Tcl training courses / classes - (2008-02-25)
  [1395] Dont just convert to Perl - re-engineer! - (2007-10-18)
  [1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
  [1221] Bathtubs and pecking birds - (2007-06-07)
  [1047] Maintainable code - some positive advice - (2007-01-21)
  [945] Code quality counts - (2006-11-26)
  [743] How to debug a Perl program - (2006-06-04)
  [668] Python - block insets help with documentation - (2006-04-04)
  [242] Satisfaction of training - (2005-03-11)


Back to
Practical polymorphism in action
Previous and next
or
Horse's mouth home
Forward to
CSL, KISS and RTFM
Some other Articles
Perl - $_ and @_
Perl - a list or a hash?
Realistic on line shoot'em up
CSL, KISS and RTFM
KISS - one action per statement please - Perl
Practical polymorphism in action
George Hotel and Well House Manor, Melksham
Breaking a loop - Ruby and other languages
Products that our customers want more of
1st, 2nd, 3rd revisited in Ruby
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/965_KISS ... -Perl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat