1. Follow the coding style guide
When in rome, do as the romans do. When coding with any framework follow the coding style which it defines. For example, PHP might be the common denominator between WordPress and CodeIgniter but you should apply different coding styles when using each.
If the style guide is ambiguous, study how the core code was written and follow that. The goal is to have your code look like it was written by the core team.
2. Your code should read like plain english
If you read your code out loud to a non-programmer the person should understand the pseudo logic. i'll explain more below.
3. Less code is mostly better
i) Remove '=== FALSE'
if ( ! $this->form_validation->run())
the above from version 2 reads in plain english as "if (NOT TRUE) do something"
if ($this->form_validation->run() === FALSE)
compared to the above from version 1... "if (TRUE identical to FALSE) do something"
the above from version 2 reads in plain english as "if (NOT TRUE) do something"
if ($this->form_validation->run() === FALSE)
compared to the above from version 1... "if (TRUE identical to FALSE) do something"
judge for yourself which is faster for your brain to process... the benefit here is that you're typing less but more importantly its easier to read and understand. writing the version 1 way is like putting your arm behind your head to touch your nose... it gets the job done but it's unnecessary.
ii) Lose the brackets
Whenever possible having less curly brackets is better for code readability. Things get really hard to read once you start to have nested brackets and you have to scroll up and down to check and match opening/closing brackets.
iii) Use 'return' to stop the logic flow
In version 2, since 'return' was called after the 'do A'. the person reading the code won't need to continue reading as the logic flow ends there. Also in version 1, if the else clause happened to be very long, the reader would have to scroll down and check if there was any more logic after the 'if... else' statement.
4. Write comments
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability."As a general rule, code should be group into 'blocks' of 1-5 statements and each should have a comment explaining what it does. Try understanding the code without the comments... then read the comments.


