Avoid these mistakes while writing code

Posted: June 3, 2008 in Best Practice
Tags:

1. Don’t write deeply nested code
It is bad programming style to write deeply nested code – the code has a tendency to drift across the page to the right and soon becomes unreadable. Try to limit most of your code to a maximum of two levels of indentation. This can be achieved by dividing the code into shorter functions.

2. Don’t write very large modules
A module should not contain more than 400 lines of source code. It is better to have several small modules than one large one.

3. Don’t write very long functions
Don’t write functions with more than 15 to 20 lines of code. Split large function into several smaller ones. Don’t solve the problem by writing long lines.

4. Don’t write very long lines
Don’t write very long lines. A line should not have more than 80 characters. 
Example:
if(objHideRevealPalette.tree["paletteButton.textHideIcon"].getSelected() || objHideRevealPalette.tree["paletteButton.textRevealIcon"].getSelected())

Write above line like this:
var bHide:Boolean = objHideRevealPalette.tree["paletteButton.textHideIcon"].getSelected();
var bReveal:Boolean = objHideRevealPalette.tree["paletteButton.textRevealIcon"].getSelected();
if(bHide || bReveal)

5. Variable names
Choose meaningful variable names – this is very difficult.

6. Function names
The function name must agree exactly with what the function does. It should return the kind of arguments implied by the function name. It should not surprise the reader. Use conventional names for conventional functions

7. Format programs in a consistent manner
A consistent programming style will help you, and other people, to understand your code. Different people have different styles concerning indentation, usage of spaces etc.

For example you might like to write tuples with a single comma between the elements:
{12,23,45}

Other people might use a comma followed by a blank:
{12, 23, 45}

Once you have adopted style – stick to it.
Within a larger project, the same style should be used in all parts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s