ThinkGeek - Cool Stuff for Geeks and Technophiles

March 11, 2004

My coding standards

I've read about coding standards, or coding best practices recently ( like this one from Dominick ). Well, here are my coding standards:

First of all, code should be self-explanatory. So, in my place of work we don't write too many comments ( in fact we never write comments, although we write class diagrams, and a lot of UML ).

Here you can see an example class. I've commented it just to avoid a longer explanation:

/*
The class name is cualified with the project name ( pn )

This class extends another class
*/
class net.designnation.pnTestClass extends pnSuperClass
{
 
 /*
 Here are the private vars.
 They will be accesed through getter/setters
 so their name ends with "Val"
 */
 private var strVal	  : String;
 private var boolVal	: String;
 private var mcVal	: MovieClip
 
 private var arg		: Number;
 
 /*
 Then, you could find the class constructor
 */
 function pnTestClass( argParam: Number )
 {
  this.arg = argParam
 }
 
 /*
 Then the getter and setters
 We only define the methods that are needed to access the
 property from outside the class
 */
 function set str( value: String )
 {
  this.strVal = value;
 }
 
 function get str( ): String
 {
  return this.strVal;
 }
 
 /*
 Here we only define the setter. That's all we need
 */
 function set bool( value: Param )
 {
  this.boolVal = value;
 }
 
 function set mc( value: MovieClip )
 {
  this.mcVal = value;
 }
 
 //The private methods
 private function privateMethod( Void ): Void
 {
  
 }
 
 // The rest of public and interface methods
 public function publicMethod( Void ): Boolean
 {
  //We are accessing a private property from
  //inside the class, so we don't need a getter
  var returnVal: Boolean = false;
  
  if ( this.boolVal )
  {
   returnVal = true;
  }
  return returnVal;
 }
 
 /*
 And finally, all the callbacks and listeners
 */
 public function onKeyUp( )
 {
  
 }
 
}

Some important points: We allways use the prefix "this.", so it's easier to see when a method or a property belongs to the class. If there is no "this" prefix, it means that it's a method or property of the superclass

Spacing is very important for us. It makes our code easier to read, and to understand. We don't mind about the code size, there are always a lot of whitelines.

Brackets are always paired. And there is also a white space between operators.

Also, all methods that return a value must have only one return point.

Hope it helps. Sure it does for us.

Posted by Cesar Tardaguila Date: March 11, 2004 11:41 PM
Comments

Something similar: http://www.peldi.com/blog/000047.html

Posted by: Peldi en: March 12, 2004 02:07 AM

Sorry, Peldi, I remember your post perfectly, but I forgot to add a link to it.

Posted by: Cesar Tardaguila en: March 12, 2004 07:37 AM