Three maxims of coding and system development; I'm reminded of these this week as I run a
PHP course which is heavily biased towards good coding standards / writing robust and maintainable web sites, but they apply to other languages / coding systems too!
"If you find yourself repeating something, then there must be a better way of doing it".
Have you even copied and pasted a block of code? I know I have, and then I've probably modified the duplicate. If you're copying and pasting, think again. Perhaps you should be incorporating the block of code into a loop, or perhaps it should become a function? If you're copying and pasting, but then altering the copy, you can still use a loop or a function - the bits that you change will become the loop control variables, or the parameters to the function.
"If you think to your self 'surely someone else has done this before', they probably have"
Don't re-invent the wheel! You'll very rarely be the first person ever to want to do "x" or "y", so have a look for a function / procedure / command that does what you want. In the Open Source world especially, where code develops with a multitude of enthusiastic contributors, you'll find rich picking by browsing the manuals in the area around what you're looking for. On our
Python course, I tell a story against myself. In a live application, I wanted to re-arrange a list into a random order. Great - wrote the code, tested it, and it worked well. Then I discovered that there's already a shuffle method in the random module that's a part of the standard distribution ...
And in PHP we have a saying -
"There's a function to do that".
"Each component should perform a single task"
Keep each logical task in its own component. Why? Because, later on, you may wish to use that task again and if it comes bundled with a second task with which it's not logically connected, it may be non-trivial to separate out. Again, an example; the images on the left hand side of our web site are 132 x 300 pixels, and they're all bundled with a white band 8 pixels wide on the right to separate off the text from the margin. Great ... until I want to change the colour of the band. Because I've bundled the band and the picture in the same image file, there's no easy way.
(written 2005-06-08, updated 2006-06-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y116 - Python - Applying OO design techniques and best practise [656] Think about your design even if you don't use full UML - (2006-03-24)
[668] Python - block insets help with documentation - (2006-04-04)
[836] Build on what you already have with OO - (2006-08-17)
[945] Code quality counts - (2006-11-26)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[2363] Alpaca Case or Camel Case - (2009-08-16)
[2407] Testing code in Python - doctest, unittest and others - (2009-09-16)
[2485] How do I set up a constant in Python? - (2009-10-31)
[2523] Plan your application before you start - (2009-12-02)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[3887] Inheritance, Composition and Associated objects - when to use which - Python example - (2012-10-10)
[4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
[4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
[4359] How to avoid too many recalculations within an object - (2014-12-21)
[4718] Defining an object that is a modified standard type in Python - (2016-11-02)
Y105 - Python - Functions, Modules and Packages [96] Variable Scope - (2004-10-22)
[105] Distance Learning - (2004-10-31)
[294] Python generator functions, lambdas, and iterators - (2005-04-28)
[303] Lambdas in Python - (2005-05-06)
[308] Call by name v call by value - (2005-05-11)
[386] What is a callback? - (2005-07-22)
[418] Difference between import and from in Python - (2005-08-18)
[561] Python's Generator functions - (2006-01-11)
[745] Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus. - (2006-06-05)
[749] Cottage industry or production line data handling methods - (2006-06-07)
[775] Do not duplicate your code - (2006-06-23)
[821] Dynamic functions and names - Python - (2006-08-03)
[900] Python - function v method - (2006-10-20)
[912] Recursion in Python - (2006-11-02)
[913] Python - A list of methods - (2006-11-03)
[949] Sludge off the mountain, and Python and PHP - (2006-11-27)
[959] It's the 1st, not the 1nd 1rd or 1th. - (2006-12-01)
[1134] Function / method parameters with * and ** in Python - (2007-04-04)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1464] Python Script - easy examples of lots of basics - (2007-12-08)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1790] Sharing variables with functions, but keeping them local too - Python - (2008-09-09)
[1869] Anonymous functions (lambdas) and map in Python - (2008-11-04)
[1870] What to do with a huge crop of apples - (2008-11-04)
[1871] Optional and named parameters in Python - (2008-11-05)
[1879] Dynamic code - Python - (2008-11-11)
[2011] Conversion of OSI grid references to Eastings and Northings - (2009-01-28)
[2439] Multiple returns from a function in Python - (2009-10-06)
[2440] Optional parameters to Python functions - (2009-10-07)
[2481] Sample code with errors in it on our web site - (2009-10-29)
[2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
[2520] Global and Enable - two misused words! - (2009-11-30)
[2718] Python - access to variables in the outer scope - (2010-04-12)
[2766] Optional and named parameters to Python functions/methods - (2010-05-15)
[2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[2994] Python - some common questions answered in code examples - (2010-10-10)
[2998] Using an exception to initialise a static variable in a Python function / method - (2010-10-13)
[3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
[3280] Passing parameters to Python functions - the options you have - (2011-05-07)
[3459] Catching the fishes first? - (2011-09-27)
[3464] Passing optional and named parameters to python methods - (2011-10-04)
[3472] Static variables in functions - and better ways using objects - (2011-10-10)
[3474] Python Packages - groupings of modules. An introduction - (2011-10-11)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[3695] Functions are first class variables in Lua and Python - (2012-04-13)
[3766] Python timing - when to use a list, and when to use a generator - (2012-06-16)
[3852] Static variables in Python? - (2012-08-29)
[3885] Default local - a good choice by the author of Python - (2012-10-08)
[3931] Optional positional and named parameters in Python - (2012-11-23)
[3945] vargs in Python - how to call a method with unknown number of parameters - (2012-12-06)
[4029] Exception, Lambda, Generator, Slice, Dict - examples in one Python program - (2013-03-04)
[4161] Python varables - checking existance, and call by name or by value? - (2013-08-27)
[4212] Python functions - an introduction to how they work - (2013-11-16)
[4361] Multiple yields and no loops in a Python generator? - (2014-12-22)
[4407] Python - even named code blocks are objects - (2015-01-28)
[4410] A good example of recursion - a real use in Python - (2015-02-01)
[4441] Reading command line parameters in Python - (2015-02-23)
[4448] What is the difference between a function and a method? - (2015-03-04)
[4645] What are callbacks? Why use them? An example in Python - (2016-02-11)
[4662] Recursion in Python - the classic example - (2016-03-07)
[4719] Nesting decorators - (2016-11-02)
[4722] Embedding more complex code into a named block - (2016-11-04)
[4724] From and Import in Python - where is the module loaded from? - (2016-11-06)
H115 - Designing PHP-Based Solutions: Best Practice [123] Short underground journeys and a PHP book - (2004-11-19)
[237] Crossfertilisation, PHP to Python - (2005-03-06)
[261] Putting a form online - (2005-03-29)
[394] A year on - should we offer certified PHP courses - (2005-07-28)
[426] Robust checking of data entered by users - (2005-08-27)
[563] Merging pictures using PHP and GD - (2006-01-13)
[572] Giving the researcher power over database analysis - (2006-01-22)
[839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
[896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
[936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
[1047] Maintainable code - some positive advice - (2007-01-21)
[1052] Learning to write secure, maintainable PHP - (2007-01-25)
[1166] Back button - ensuring order are not submitted twice (PHP) - (2007-04-28)
[1182] Painting a masterpiece in PHP - (2007-05-10)
[1194] Drawing hands on a clock face - PHP - (2007-05-19)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1323] Easy handling of errors in PHP - (2007-08-27)
[1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
[1389] Controlling and labelling Google maps via PHP - (2007-10-13)
[1390] Converting from postal address to latitude / longitude - (2007-10-13)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1482] A story about benchmarking PHP - (2007-12-23)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1490] Software to record day to day events and keep an action list - (2007-12-31)
[1533] Short and sweet and sticky - PHP form input - (2008-02-06)
[1623] PHP Techniques - a workshop - (2008-04-26)
[1694] Defensive coding techniques in PHP? - (2008-07-02)
[1794] Refactoring - a PHP demo becomes a production page - (2008-09-12)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
[2430] Not just a PHP program - a good web application - (2009-09-29)
[2679] How to build a test harness into your PHP - (2010-03-16)
[3539] Separating program and artwork in PHP - easier maintainance, and better for the user - (2011-12-05)
[3813] Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them - (2012-07-22)
[3820] PHP sessions - a best practice teaching example - (2012-07-27)
[3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
[4069] Even early on, separate out your program from your HTML! - (2013-04-25)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4641] Using an MVC structure - even without a formal framework - (2016-02-07)
[4691] Real life PHP application using our course training MVC example - (2016-06-05)
H105 - PHP - Functions [223] There is a function in PHP to do that - (2005-02-21)
[339] Passing information into and out of PHP functions - (2005-06-07)
[409] Functions and commands with dangerous names - (2005-08-11)
[421] Don't repeat code - use loops or functions - (2005-08-21)
[866] A lazy programmer is a good programmer - (2006-09-15)
[1021] PHP - static declaration - (2007-01-04)
[1267] is there a lookup function in php? - (2007-07-15)
[1357] Clean my plate, but keep my wine bottle. (PHP; Static) - (2007-09-18)
[1380] Static variables in PHP - (2007-10-05)
[2488] A variable number of arguments in a PHP function - (2009-11-02)
[2630] Static variables and ampersands in PHP - (2010-02-10)
[2682] Adding extensions to PHP Open Source applications - callbacks - (2010-03-17)
[2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
[3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
H104 - PHP - Control Statements [353] Wimbledon Neck - (2005-06-20)
[406] Assignment, equality and identity in PHP - (2005-08-08)
[657] The ternary operator in Python - (2006-03-25)
[863] Double and Triple equals operator in PHP - (2006-09-12)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[1199] Testing for one of a list of values. - (2007-05-22)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
[2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25)
[2912] Predictions for the seagull population - (2010-08-09)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[3914] While, for, foreach or something else to loop. - (2012-11-06)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
Some other Articles
Walk in BathShould I use structured or object oriented?The evening after the courseHappy Birthday, PHPCode and code maintainance efficiencyOO techniques are hard to teachthe array returned by preg_match_allTargetted AdvertisingSad priorities