Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Improving your function calls (APIs) - General and PHP

Some of the code at the Civil Service Department (CSD) where I worked on manpower planning models some 35 years ago would not have stood up to analysis against modern programming practice (but then ... that WAS 35 years ago). Indeed, some of the code "behind the curtains" didn't even stack up properly in those days. Perhaps we shouldn't have been surprised - for it was written by a very mixed crew of sandwich course students in the middle of their courses, and brilliant statisticians who were educated in an age before computers and had found their own coding techniques for writing functional code, but code that was impenetrable to students like myself who then had to maintain it.

The languages of the day were a bit short on facilities in some areas too, and that didn't really help; a prevalence of GO TO statements (and everything in capital letter) and a need to subscript check all arrays, without a list / vector / array list primitive for starters. Then, FORTRAN was a compiled language in an application area where, today, we would use scripting with (ideally, I think) Python.

So ... the CSD invited Colin Day to have a look at some code and one day he came in and talked us through some of the aspects that we should be looking at updating / amending to make the code more robust, more maintainable and more extendable as the project continued. That was a superb lecture; a real eye opener for me that marked a distinct change in direction for my coding, taking not only what Dr Day had to tell us to heart on the immediate project, but also looking at the principles behind his talk, and applying them thereafter and elsewhere.

I was reminded (yet again) of Colin Day's day with us yesterday - technical comment to follow - and wondered if I could find him via a search. One page says "We know 200 Colin Days in the UK" but then, amazingly, I think I have found the guy: [here]. It looks like a true case of "if you want a job doing, ask a busy man" for he had so much else happening when he came to the CSD - and I get the impression of someone who really has made a difference. He did to me - thank you, Colin.




The Heinz 57 varieties Subroutine

Subroutines / Functions / (procedures, methods, commands, macros, call them what you will!) are named blocks of code that help split code up. They're usually a good thing, allowing for modular testing and re-use of code - and in order to optimize those benefits, you want to keep the interface that others use to call your subroutines / functions small, simple, robust and unchanging.

Colin had found a subroutine in one of the CSD's manpower planning models that he called "the Heinz" - it had 57 parameters being passed in to it, and with parameters being in by position, a slight slip in coding would have been hard to locate and debug.

The shoe is on the other foot these days (though my feet don't fill Colin's shoes) and a delegate was asking me how - in PHP - I would reduce the complexity of calls to functions. And here are the tips / techniques that I offered, starting with the less effective ones and building to a climax!

• Optional parameters - put parameters which can usually be defaulted on the end, with the "most optional" on the very end - that way, you can often make the call shorter and simpler.

• Globals. There may be a small number of items that you pass in and out of lots of closely linked functions, and if that's the case, you may consider globals. Personally, I use them on web pages for my global error message block and flags, and PHP itself uses superglobals $_REQUEST, $_GET, $_FILE, $_SESSION, $_POST, $_SERVER, $_COOKIE and $_ENV.

• 2 way passing. If you add an & into the declaration of a variable in a function, that variable is passed by reference not by value. What that means is that you can alter the variable in the function, and the variable that's in the main code from which it originates is also changed. So this gives you an extra way of returning values - you get a "buy one, get one free" deal!

• Find the narrow waist. Your functions should logically group together code to perform a particular task, which should reduce the number of values to be passed in and out - you'll naturally find the narrow waistline of your application that lies between the broader chest and the thighs. And you can use the other techniques in this section to reduce further your parameters - like applying a corset to your code.

• Passing collections. Rather than pass a whole series of separate parameters, you can pass over collection variables. In PHP, that means you can replace (say) 10 parameters with an array of 10 elements - or (better) with an associative array with 10 elements or (best) with an object with 10 properties.


With these techniques applied together, you'll reduce your "Heinz 57" calls to calls which have just a handful of parameters - reducing, perhaps, 57 to a far more practical 5 parameters. I'm not going to give you a set rule as to what a maximum should be for good coding, save to save that I worry is I see a function where the number of parameters being passed in has reached double figures.

Note

PHP does not support "named parameters" where you can cherry pick and name parameters in the call. But you can achieve exactly the same results by passing in an associative array.


This article has been written to follow up on discussions on our Intermediate PHP for hobby / leisure users course which is running this Saturday and Sunday ... and will be running again later in the year. We also offer a beginners course for the leisure market, and more frequent weekday courses which are tailored for the business user. See [here] for an overview of all our PHP courses.
(written 2010-04-24, updated 2010-05-14)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q101 - Object Orientation and General technical topics - Programming Principles
  [2001] I have not programmed before, and need to learn - (2009-01-19)
  [2022] Pre and post increment - the ++ operator - (2009-02-03)
  [2228] Where do I start when writing a program? - (2009-06-11)
  [2310] Learning to write high quality code in Lua - (2009-07-30)
  [2327] Planning! - (2009-08-08)
  [2415] Variable names like i and j - why? - (2009-09-22)
  [2510] The music of the stock market - (2009-11-22)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2769] Easy - but for whom? - (2010-05-18)
  [2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
  [3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
  [3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
  [3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3878] From Structured to Object Oriented Programming. - (2012-10-02)
  [3928] Storing your intermediate data - what format should you you choose? - (2012-11-20)
  [3954] Lesson 1 in programing - write clean, reuseable and maintainable tidy code - (2012-12-16)
  [4003] Web and console - same principle, same code - Ruby example - (2013-02-14)
  [4061] Seamless, integrated IT - we have a long way to go! - (2013-04-11)
  [4090] Test Driven Development in Python - Customer Comes First - (2013-05-16)
  [4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
  [4153] Rooms available tonight - how to code an algorithm from first principles - (2013-08-19)
  [4206] Writing the perfect program in Tcl? - (2013-11-13)
  [4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
  [4611] Hungarian, Camel, Snake and Kebab - variable naming conventions - (2016-01-03)
  [4632] Remember to ask the question before you listen for the answer - (2016-01-26)
  [4645] What are callbacks? Why use them? An example in Python - (2016-02-11)

H105 - PHP - Functions
  [96] Variable Scope - (2004-10-22)
  [223] There is a function in PHP to do that - (2005-02-21)
  [308] Call by name v call by value - (2005-05-11)
  [339] Passing information into and out of PHP functions - (2005-06-07)
  [340] Code and code maintainance efficiency - (2005-06-08)
  [409] Functions and commands with dangerous names - (2005-08-11)
  [421] Don't repeat code - use loops or functions - (2005-08-21)
  [775] Do not duplicate your code - (2006-06-23)
  [866] A lazy programmer is a good programmer - (2006-09-15)
  [936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
  [1021] PHP - static declaration - (2007-01-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)
  [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)
  [1784] Global - Tcl, PHP, Python - (2008-09-03)
  [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)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)


Back to
Perl Course FAQ
Previous and next
or
Horse's mouth home
Forward to
What is all this SESSION stuff about? (PHP)
Some other Articles
What is a factory?
Melksham Hustings at George Ward School
Melksham Scouts
What is all this SESSION stuff about? (PHP)
Improving your function calls (APIs) - General and PHP
Perl Course FAQ
Type checking, Java arrays and collections
for and foreach in Java
Travel Troubles and Jesus again
Asking about Jesus
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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2737_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb