Posts Tagged ‘code’

Goodbye for my office and me

December 31st, 2008

111408151048Good things come and go but now in the last day of 2008 i have to say goodbye to my beloved old office. I will start my own company tomorrow called Web nation and that makes me really happy. I have some words for my old office as well..

Good bye dear office you where my throne for a while but now i have to say good bye. In these strange time as we are living in managed to start a business of my own in Web Development.

Okay now with that out of the way there is a bit of jolly news as well…. Now that i am starting a new company i was promoted to be a top 1000 developer for Wordpress.  This news excites me more to continue developing wonderful tools for you all to use including commercial tools.

In 2009 i will be showing you more tutorials and screen casts but lately i was a bid busy getting my company off the ground for the next year to come. Next year i will give you readers a brief introduction into Iphone development so you can go out and sell your own Iphone Applications in Apple her app store. We will have discussions about mobile development like Iphone related and Google`s Android. You may ask why ? read the slogan of this blog

Our work our cool

I do hope that all of you will all be here in 2009 and bring me the fun of blogging like you guys did in this year.

Happy new year to all of you out there !!

The road to readable code

August 1st, 2008

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:
  1. ]
  2.  
  3. sName      = 'Johnny';          /* String */
  4. iAge       = 26;                   /* Int */
  5. bSingle    = true;                /* Boolean */
  6. aSisters   = new Array ('Amanda', 'Laura'); /* Array */
  7. dCodeLines = 100.4;           /* Double */
  8. fSubtotal  = 20.45;             /* Float */
  9. 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:
  1. 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:
  1.  
  2. function makesum ($p_iVariableOne, $p_iVariableTwo)
  3. {
  4. return ($p_iVariableOne + $p_iVariableTwo);
  5. }
  6.  
  7. $iVarOne = 5;
  8. $iVarTwo = 5;
  9.  
  10. echo makesum ($iVarOne, $iVarTwo);

 
And the javascript version

JAVASCRIPT:
  1. function makesum (p_iVariableOne, p_iVariableTwo)
  2. {
  3. return (p_iVariableOne + p_iVariableTwo);
  4. }
  5.  
  6. var iVarOne = 5;
  7. var iVarTwo = 5;
  8.  
  9. 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:
  1. function getPersonalLocationString (p_sYourName)
  2. {
  3. var sYourName = p_sYourName;
  4. return sYourName + ' you are located in ' + g_sHomeTown;
  5. }
  6. var g_sHomeTown = 'London';
  7. var g_sMyName    = 'Johnny';
  8.  
  9. 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:
  1. var iMyAge:Number = 8;
  2.  
  3. function olderthenEight (p_iAge:Number):Boolean
  4. {
  5. return p_iAge> 8;
  6. }
  7. trace ('Your older then 8 years old ' +  olderthenEight ( iMyAge ) );
  8. 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

Would you help in Twitme Beta ?

View Results

Loading ... Loading ...