if

#! code Logo

Writing Function Code To Be More Readable

1st October 2008 - 3 minutes read time

Last month I started writing functions in a particular way, which has made my life as a programmer much easier on more than one occasion. No matter how many comments or verbose parameter names you put in you can end up writing code that you will get lost in. The reason is simple. Lets say you had a function that took in a couple of parameters.

function myFunction($intNum1, $intNum2){
 // function does something
}

Normal practice is to check the parameters to make sure that they are what you expected them to be before continuing on with the rest of the function. Let's say that we only want the numbers to be in a range. If they are not in the range the function should return false. Many programmers might start of writing something like this.

PHP Logo

Alternate If Statements In PHP

20th January 2008 - 3 minutes read time

If you have programmed in PHP for any amount of time then you will be farmiliar with the if statement. The syntax is as follows:

if($something == $somethingelse){
  //do something
}elseif($something == $anotherthing){
  //do another thing
}else{
  // default action
}

The PHP engine also allows you to do what I call "lazy programming" where you don't need the curly braces. Only the line underneath the if statement is run if the if clause if met.

if($something == $somethingelse)
  // do something

The issue here is that when this is put into the rest of the program the code becomes almost unreadable and therefore unmaintainable. The curly brases make it much easier to see what a program is doing. For readability and maintenance, many developers consider it bad style not to include them.