|
Code and code maintainance efficiency
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 05:36:35)
Associated topics are indexed under H104 - PHP - Control StatementsH105 - PHP - FunctionsH115 - Designing PHP-Based Solutions: Best PracticeY105 - Python - Functions, Modules and PackagesY116 - Python - Best Programming Practice
Some other Articles
Walk in BathShould I use structured or object oriented?The evening after the courseHappy Birthday, PHPCode and code maintainance efficiencyPassing information into and out of PHP functionsOO techniques are hard to teachthe array returned by preg_match_allTargetted AdvertisingSad priorities
|
2259 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 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).
|
|