Add Me!Close Menu Navigation

PHP | Web Development | Object Oriented | MVC | MySQL | Apache

Add Me!Open Categories Menu

A quick word about readability in PHP

Some people think that code should be something hard to read with little readability. Those people think that only by obfuscating their original source will they gain a sense of security. Inevitably, they eventually pay for this choice. Maintenance is a bitch when you can’t read your source.

Here is one thing (out of many)to make your code more readable.

First, constants. Constants? How can constants help you? Fear not! Here is an example.

<?php
function fu( $bar = 1  ){
if( $bar == 1 ){
return 1;
} else {
return 0;
}
}?>

What’s wrong with this you will ask? Nothing, if you are going to use this as a one-shot function. Let’s see how we could do this differently with a better readability.

<?php
define( 'RETURN_SUCCESS' ,  1 );
define( 'RETURN_FAILED' ,  0 );
function fu( $bar = 1  ){
if( $bar == 1 ){
return RETURN_SUCCESS;
} else {
return RETURN_FAILED;
}
}
?>

Not such a big change but when you will read your code again, the return values will instantly mean something to you. Extending this to other aspects of your code can greatly improve readability and maintainability of your code.

More hints soon. I have a coffee losing heat in front of me!

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Posted By admin

Leave a Reply




Tag Cloud

Categories

Archive

Pages

Popular Posts