|
A better alternative to cutting and pasting code
If you're new to coding, you'll be so concerned to be writing code that works that you may not take a look at coding technique. Your nose will be so close to the grindstone as you work that you won't take the time to look and ask "Do I need to keep grinding anyway?"
If you find yourself writing a piece of code and thinking "surely someone has done this before", they probably have, and the code is probably available to you - either as a standard PHP function of in resource libraries such as the PEAR and PECL. ((See footnote for other languages))
If you find yourself cutting and pasting part of your code, then STOP! - you should be turning that code into a function! "But I need to change a few things in the copy" I hear you saying. Maybe you do - and you have just identified those parts of the function code that will need to be passed in to it as parameters.
I wrote the text above in relation to PHP, but the same thing applies in almost every language.
In Perl, you write your shared code in a sub, and look on the CPAN resource library.
In Python, you write your shared code using a def, and look in the cheeseshop resource library.
In Java and C++ you write a method, in C a function, and in Tcl you write a proc. As I recall, in Fortran it's a subroutine or a function, and you may also come across words like macros and procedures ... (written 2007-04-26 18:38:23)
Associated topics are indexed under C204 - C and C based languages - Functions, Macros and programs in multiple filesH105 - PHP - FunctionsJ706 - Java - Objects and ClassesP209 - Subroutines in PerlR104 - Ruby - Control StructuresT207 - Tcl/Tk - Procedures and Variable ScopeY105 - Python - Functions, Modules and Packages
Some other Articles
Leaning tower of CheeserBack button - ensuring order are not submitted twice (PHP)Extended Credit request - train in June and be paid in SeptemberGrand Central StationA better alternative to cutting and pasting codeCheddleton, StaffordshireA contrast in room rates and facilitiesHTML - example of a simple web pageIt can take more that one plus one to get two.Private Java Course - A customer's pictures
|
2258 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).
|
|