Coding standards

It has come to my attention that most of the people that learn how to program in highschool / college / university have bad habbits when it comes to writing their code down.
I’m not saying the code itself is incorrect, i’m pretty sure they teach the correct stuff, but i’m talking about other things that usually get ignored…
In order to program effectively, it’s important that you keep to a set of rules.
The ones that irritate me the most are:

  • Variable naming
  • No indent in your code

Okay, most of the time code written by people that don’t really have an interest in coding, but just take those classes because “they have to do something to get a diploma” looks like this:


// PHP Variable Naming example
$mystring = "Hello World!";
$myarray = array("item1", "item2", "item3", "item4");
$mynumber = 1;
$mybool = true;

// Output: Hello World! My array holds 4 items, my number is 1 and this all is 1
printf("$mystring My array holds ".count($myarray)." items, my number is $mynumber and this all is $mybool

"); /** Output: * item1 * item2 * item3 * item4 **/ foreach($myarray as $item){ printf($item."
"); }

Now imagine that part of code x 2000.
I know i’ve used pretty obvious variable names, like mystring which indicates it’s a string etc, but i could’ve used any other name such as “$dog” where you won’t know what type the variable really is.
I mean, it could be a string where you define the dog name, or the dog type,
but it could also be a integer or a boolean: dog = true (yes, it’s a dog) or dog = 4 (A certain pre-defined dog type or whatever).

Anyhow, back ontopic:

If I would be working at a company with a programming team, and one of my team members would send me a large file filled with code and variables names like that, i’d spent more time figuring out what type what variable is.

That’s why it’s important that you make sure that people can tell by the name of your variable what type is stored inside the variable.
When I was learning OO in PHP, i’ve forced myself to keep using this set of standards, and i’ve been using them in all my programs since then in all the coding languages i know (PHP, Java, VB.NET, NASC, …)

According to the Hungarian Naming Convention all variable names should be built as following:

  • A letter for the scope of the variable (g = global, m = member, l = local, p = parameter)
    • Mind you, that for local they usually don’t put a l infront of it (Ex: l_sMyString) but leave it out (Ex: sMyString). This is due to the fact that all local variables are being used local, and all variables that orginate from outside the local scope, already have a scope indication
  • An underscore after each scope letter
  • A “few” letters that describe the type of the variable, these are all lowercase
    • a = Array
    • b = Boolean
    • c = constant
    • d = Date
    • fn = Function
    • n = Numeric
      • f = Float
      • i = Integer
      • l = Long
    • o / obj = Object
    • r = reference
    • s = String
    • v = variant
  • The describing name of the variable, starting with a capital letter at each seperate part of the name (Ex: FillString, MyVariable, MyString, MyInteger, …)

I’ll show you a quick example of a class in PHP:

	// Organized example
	class Foo
	{
		protected $m_sName;
		protected $m_nAge;

		public function getInfo( $p_bAge )
		{
			// Show name with age or without age
			if( $p_bAge == true )
			{
				printf( $this->m_sName." is ".$this->m_nAge." years old!" );
			}
			else
			{
				printf($this->m_sName." doesn't want to show his/her age!");
			}
		}
	}

As you can see, i’ve used my own standards and anyone that knows PHP and reads this code will know exactly what kind of value is inside each variable.
I hope you’ve also noticed that i’ve indent my code, including the make-up for the if-else etc

The basic idea behind indent is to have a much better overview of your code, it’ll be easier to determine what belongs to where.
Let’s say we got the above code, but I won’t indent it nor make it pretty:

// Organized example
class Foo
{
protected $m_sName;
protected $m_nAge;

public function getInfo( $p_bAge )
{
// Show name with age or without age
if( $p_bAge == true ){
printf( $this->m_sName." is ".$this->m_nAge." years old!" );
}else{
printf($this->m_sName." doesn't want to show his/her age!");}
}
}

Now imagine that again x 2000.
I’ll bet you $100 USD you’ll get confused before you’ve read through the first 100 lines of the code 😛

In cases like these, it’s not easy to determine which closing tag belongs to which opening tag, unless you’re using a special software to write your code in (Notepad++ for example highlights start & ending tags if you click on them)

Bottom line is: use correct names and learn to ident your code, make a habbit out of it!

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.