Today we are going to discuss the best ways to keep your code clean and so more readable. When you develop in a team everyday like me you have sure that your code remains readable and clean for the other developers to read it.
The absolute basis is "Hungarian Naming Convention" or HNC for short, this method of development takes naming object so that they are recognizable for you and those who read your code.
Lets sum things up what kind of variables do you usually have in any programming language.
String, Int, Boolean, Array, Double, Float, Mixed, Resource, Global,
Private, Parameters (as for functions)
The general usage of HNC on variable names is that you give them prefixes to the variable types they are. For example when you have a variable that will contain a string then you prefix your variable with an 's' and a capital first char of your variable name. Here are examples from the list above coded in javascript.
JAVASCRIPT:
-
]
-
-
sName = 'Johnny'; /* String */
-
iAge = 26; /* Int */
-
bSingle = true; /* Boolean */
-
aSisters = new Array ('Amanda', 'Laura'); /* Array */
-
dCodeLines = 100.4; /* Double */
-
fSubtotal = 20.45; /* Float */
-
rHandle = setTimeout (function() { return false; }, 3000); /* Resource */
As you can see this will realy help you to increase your code readability because you could see in a glance what kind of variable your dealing with and also that when you see the following comparation you should raise an eyebrow in means of hu ?
PHP:
-
if ($sPath == $iPath) dosomething();
It really pays off to name your variables in an efficient way if you do so you already turned 70% of your code into sexy code. As extra bonus you will get the feeling that programming languages like php and javascript really start to look like at each other. Here are 2 functions that do exactly the same thing in both php and javascript, Pay attention to its readability.
PHP:
-
-
function makesum ($p_iVariableOne, $p_iVariableTwo)
-
{
-
return ($p_iVariableOne + $p_iVariableTwo);
-
}
-
-
$iVarOne = 5;
-
$iVarTwo = 5;
-
-
echo makesum
($iVarOne,
$iVarTwo);
And the javascript version
JAVASCRIPT:
-
function makesum (p_iVariableOne, p_iVariableTwo)
-
{
-
return (p_iVariableOne + p_iVariableTwo);
-
}
-
-
var iVarOne = 5;
-
var iVarTwo = 5;
-
-
alert (makesum (iVarOne, iVarTwo));
Define your variables
This is very important for more then one reason lets sum them up.
- Preventing undefined variable notices in php
- Preventing 'undefined' bugs in javascript
- Readability
Global variables
When setting variables outside of any function or class its suggested to name your variables with a g_ prefix so that if any variables that are not defined inside of a function will be instantly recognized as being global an variable.
Parameters to functions
Inside of functions everything falls together because you would use your global variables and the parameters given to your functions but also the variables defined inside of your function. For parameters given to functions always prefix your parameter variables with an p_ for parameter. Ow and ladies and gentleman always threat your parameters as a reference and read-only. Parameters are meant to be parameters given to calculate the final result (why you wrote your function).
Inside of functions chaos is can quickly raise because there you will have a mix of all kinds of variables you will have your globals your locals and parameters. Lets construct a simple function with a mix of those cases. But we will do it the correct way.
JAVASCRIPT:
-
function getPersonalLocationString (p_sYourName)
-
{
-
var sYourName = p_sYourName;
-
return sYourName + ' you are located in ' + g_sHomeTown;
-
}
-
var g_sHomeTown = 'London';
-
var g_sMyName = 'Johnny';
-
-
alert ( getPersonalLocationString ( g_sMyName ) );
A note for flash developers
Actionscript offers data typing your variables on creation, This is for the good for code readability so i suggest you would use them but not only on variables use it on functions as well here is an example.
ACTIONSCRIPT:
-
var iMyAge:Number = 8;
-
-
function olderthenEight (p_iAge:Number):Boolean
-
{
-
return p_iAge> 8;
-
}
-
trace ('Your older then 8 years old ' + olderthenEight ( iMyAge ) );
-
trace ('Your older then 8 years old ' + olderthenEight ( 9 ));
Use of the english language
Not only when you work on international projects its important to give your functions and variables
english names but also when your working for your self. The main reason is that your code becomes more reusable in terms of you could copy and paste functions you already created for some other project in a new project who might be international. The second reason is that if you ever need help on your project you could copy and paste your code straight onto a forum with international developers while asking for help.
Use of Regex
I would not suggest one developer using regular expressions because a regex is really something that you have to get into. At my work we have many websites and many projects. Having a large set of websites requires a load of memory. I suggest using alternative methods of parsing data then without using regex just for the simple reason that i makes your code more readable to other developers who do not have an understanding of regular expressions.
Use 4 spaces for a tab
Not all IDE`s use the same code indenting thats a pity but a fact. When ever you work with 2 development IDE`s i name Komodo IDE and Dreamweaver. Always check the number of space that one tab indents in your code this will makes the code more readable for a whole team and saves time for the team to fix the indent on a later stage in time. Here you can find an interesting article about this topic.
Conclusion
The phpJS team wrote a really good document about code readability for there team members. I really do feel that any team should set up some rules even before they start development together. There are so many more hints that can help you develop better code, This post is a good starter for a team to get started. But if you have any tips or hints to give for the readers leave a comment here so that every one could learn from it as well. Or if your bored just tell me what you think of this article
Phpjs document: here

Loading ...