Writing clean code is very important. The cleaner your code, the easier is to fix it, grow it and make it work the way you want.
So, here are some actionable tips to keep your PHP code neat and clean.
Tip #1: Variables Names.
How do you choose the names of your variables? Your variables names must be: - Short enough to be spotted and read quickly. - Descriptive enough so that is clear what the variable is used for.
So, here is a wrong example: $a = 0.9; $b = 10; $c = 10 * 0.9;
Here, the names $a, $b and $c are meaningless. What do they mean?
It's impossible to say.
But this is wrong too: $usdToEurConversionRatio = 0.9; $valueInUsdCurrency = 10; $valueInEurCurrency = $valueInUsdCurrency * $usdToEurConversionRatio;
Yes, their meaning is clear, but such names are far too long.
Finally, here is a good compromise: $usdToEur = 0.9; $usdVal = 10; $eurVal = $usdVal * $usdToEur;
Now you can read the names very fast and still understand exactly what they mean.
Tip #2: Stick With One (Human) Language.
The names of many code elements, such as variables, functions and classes, use real words. Sometimes, if we are not native English speakers, we use a mix of English and non-English words. For instance, I am Italian and sometimes I find myself using Italian words for variables.
It's best, however, to stick with only one
language. You can either use English or your own language. Make your choice and stick with it for your entire project. Both choices are fine, but if you are going to share your code with people from around the world, then you should use English.
Tip #3: Lowercase and Uppercase.
Here are some basic rules shared by the majority of software developers.
Variables and function names must start with a lowercase letter (ex. $myVar).
Classes and Namespaces must start with an uppercase letter (ex. class MyClass).
Constants must be all uppercase (ex. const MY_CONST).
These are standard naming conventions. Follow them to make your code clear for yourself as well as for other developers. On top of that, you will find other people's code easier to read, because they will probably use the same conventions.
Tip #4: Camel Case or Underscore Naming?
Some developers prefer to use camel case for names: $likeThis. Others prefer underscore naming: $like_this.
Both are fine. In fact, some frameworks like Codeigniter suggest underscore naming, others like Zend suggest camel case.
Both conventions have their pros and cons. But in the end, it's just a matter of personal taste. Just don't mix them. Make your choice and stick with it.
PHP standard functions almost always use underscore naming, but that doesn't mean you should do that too. In fact, using camel case is a smart way to distinguish PHP native code from your own!
Tip #5: Do not Mix Abbreviations.
Word abbreviations are perfectly fine. For example, $passwd is just as clear as $password, but it's shorter and easier to read.
However, avoid using
different abbreviations for the same word. You can abbreviate $password with $passwd or with $pwd, but don't use sometimes one and sometimes the other. Choose one and use it in all your projects.
Tip #6: Use Transitive Verbs for Class Methods (When it Makes Sense).
If you want to make your class methods really readable, then use verbs for their names. More specifically, transitive verbs (that want an object for their action).
For example, many class methods set or read a value. In these cases, you can add the set or get prefix to the method name to make it clear what it does.
For example, suppose your class has a $price attribute. This is the class method to set a new price: function setPrice()
And this is the class method to read the current price: function getPrice()
As you can see, these names are so clear that you can tell what these methods do just by
reading their names.
That's all for today.
Now it's your turn to send me a reply. What is your favourite clean code tip?
Until next time, Alex
P.S. Stay tuned for more clean code tips :)
Share the knowledge
Did you like this email? Share it with your friends and colleagues. Click here to share it
Why do some PHP apps get hacked and others don’t?
Are your apps safe from web attacks? You are not so sure, are you? You only hope that your code is secure. And that keeps you constantly worried. But with the right defense techniques, you can protect your apps from all kinds of attacks. So you can finally feel safe and stop worrying.
PHP Security Mastery is my personal course that will teach you exactly how to make your code secure. Click here for the details.
Need help with your PHP code?
If you can't make your PHP code work, you can ask me for a Code Review. I will verify your code, fix it and make sure it works the way you want. Click here for the details.
Resources
Alex Web Develop- My blog where you can find my tutorials. Alex PHP café- My Facebook group where you can talk with me and other developers.