The document discusses several ways to write readable code, including using proper formatting and spacing, clear and consistent naming conventions, modular code structure, and refactoring techniques. It provides examples of good code formatting practices like adding spacing before returns, arranging variables consistently, using descriptive names instead of abbreviations, and prefixing variables and methods for clarity. The document emphasizes that readable code clearly communicates intent to readers.
3. “Readable code is code that
clearly communicates its intent to the reader”
Source: https://fellow.app/blog/engineering/the-complete-guide-to-readable-code/
18. only two hard things in Computer Science: cache invalidation and nam
— Phil Karlton
19. Why naming matters?
• Naming is communication and bad naming prevents code from
communicating clearly.
• Code with obfuscated names is hard to read and understand.
• Other people should be able to read and understand the code.
35. Only if there was no else!
function send_email() {
if ( $user->is_active() ) {
if ( $user->has_subscribed() ) {
return send_subscription_email( $user );
} else {
return send_marketing_email( $user );
}
} else {
return send_activation_email( $user );
}
}
36. Only if there was no else!
function send_email() {
if ( !$user->is_active() ) {
return send_activation_email( $user );
}
if ( $user->hasSubscribed() ) {
return send_subscription_email( $user );
}
return send_marketing_email( $user );
}
While it’s easier said than done and often a challenge for new developers, creating readable code is as important as solving any software problem—this is why this should be one of the first techniques a developer learns on the job.
It must be correct i.e it must produce a result that is expected when executed.
The code must be easily readable and understandable by ourselves and other developers.
Since our software code can be shared with other developers, we’ll want to make it easier to work with this shared code. To do this, we’ll have to make sure that everyone involved, including ourselves, understands the same thing easily and quickly. Code that is readable for one person is not necessarily readable for another.
Code that is not readable takes more time to understand and can make us lose a lot of time on what should be a simple task. The worst-case scenario is that it could even take several iterations to fix some problems. In some cases, we might spend so much time trying to understand code that we might want to rewrite it completely.
It must be correct i.e it must produce a result that is expected when executed.
The code must be easily readable and understandable by ourselves and other developers.
Since our software code can be shared with other developers, we’ll want to make it easier to work with this shared code. To do this, we’ll have to make sure that everyone involved, including ourselves, understands the same thing easily and quickly. Code that is readable for one person is not necessarily readable for another.
Code that is not readable takes more time to understand and can make us lose a lot of time on what should be a simple task. The worst-case scenario is that it could even take several iterations to fix some problems. In some cases, we might spend so much time trying to understand code that we might want to rewrite it completely.
Appearance is everything. As a human, you always want to appear best so that others like you. Not just others, but you should also like your own appearance in mirror.
The same is the case with code. It should be written in a way that it appears best to others and when you read your own code after a few months, it should still appear best to you as well.
Use Wordpress Coding Standards. If using some other CMS or framework then start with that community standard
Follow PSR (PHP Standards Recommendation)
- Makes code well formatted
- Consistent
- Robust
Use PHP_CodeSniffer
WordPress Coding Standards for PHP_CodeSniffer
PHP Coding Standards Fixer aka php-cs-fixer
VS Code and other editor plugins
Use Wordpress Coding Standards. If using some other CMS or framework then start with that community standard
Follow PSR (PHP Standards Recommendation)
- Makes code well formatted
- Consistent
- Robust
Use PHP_CodeSniffer
WordPress Coding Standards for PHP_CodeSniffer
PHP Coding Standards Fixer aka php-cs-fixer
VS Code and other editor plugins
Hard to read
Can’t figure out the code blocks
Clear distinguishes blocks of code and logic
Gives a much needed breathing room to your code
Return value getting lost
Can’t determine just by glance if function is returning something or not
If the first block is large then hard to see what is happening in else condition
Code is first processing positive scenario and then negative. You should arrange the code such that it first tackles all the negative scenarios
Unnecessary else
Nomenclature is naming things
The hardest thing for new parents is to name their baby. Same is the case with the code. Your code is your baby and it is really difficult to give proper and meaningful names to variables, functions, classes and files.
TODO: ALL BULLETS AT THE SAME TIME
Naming is communication and bad naming prevents code from communicating clearly. For example, if we name a boy as Preeti and a girl as Rahul - do you think this is a good naming?
Code with obfuscated names is hard to read and understand.
Other people should be able to read and understand the code.
What is this guy doing? He is either thinking about the best way to bust the cache or thinking how to name a class.
Do you want to get in this situation? Let’s see how to come out of this situation.
TODO: Make it shorter
English is the standard language to use in programming. Code can be managed by programmers from other regions and countries and everyone understands English.
Avoid using emojis and local language for variables, functions etc.
Meaningful names like $days instead of $d. Have seen many using $a, $b etc.Never do that. Can use commonly accepted single letter variables for loops like $i.
Synonyms - Head_of_School -> Principal $loginAsOtherUser -> $impersonate, photos_and_videos -> media
create_new_user should be create_user
new_group_created_send_notification() -> send_group_created_notification()
update_old_data() -> update_data()
TODO: Make it shorter
Meaningful names like $days instead of $d. Have seen many using $a, $b etc.Never do that. Can use commonly accepted single letter variables for loops like $i.
Synonyms - $loginAsOtherUser -> $impersonate, photos_and_videos -> media
new_group_created_send_notification() -> send_group_created_notification()
update_old_data() -> update_data()
After this discuss Adding metrics name to variables
$expiryTime = 60;
$expiryTimeInMinutes = 60;
Adding prefix to method names
$user->subscribed(); // returns true or false
$user->hasSubscribed();
Boolean database fields are used for storing true or false.
Example - todos.completed
This only tells us whether a todo is completed or not.
Replace that field with a timestamp nullable field completed_at
Now we can determine if the todo has been completed or not as well as the time it was completed at.
Boolean database fields are used for storing true or false.
Example - todos.completed
This only tells us whether a todo is completed or not.
Replace that field with a timestamp nullable field completed_at
Now we can determine if the todo has been completed or not as well as the time it was completed at.
Avoid else. It makes your code a lot cleaner
Else has a wide scope.
Reduce cyclomatic complexity