BBC Home

Explore the BBC

h2g2
6th January 2010
Accessibility help
Text only

Guide ID: A543070 (Edited)

Edited Guide Entry


SEARCH h2g2
Edited Entries only
Search h2g2Advanced Search


New visitors: Create your membership
Returning members: Sign in
BBC Homepage
The Guide to Life, The Universe and Everything.

3. Everything / Maths, Science & Technology / Computers / Computer Languages and Programming

Created: 15th May 2001
Seven Secrets of Successful Programmers
Contact Us


Like this page?
Send it to a friend!

 

1 Code for Human Consumption

It is one of the most pervasive misunderstandings in computing that the source code is for the computer's consumption. Computers work with low-level binary code, a series of impenetrable ones and zeroes, or hexadecimal numbers, not the structured high level languages we code in. The reason that these languages were developed was to help the programmer.

In practice, coding for human consumption means coding for clarity first, efficiency and speed second.

2 Comment Often and Comment Well

The comment is the extreme example of a language element for human consumption. Compilers will strip the comments from the executable program. The purpose of the comment is to tell you (and any future developer) what the program is intended to do. Write comments with this in mind and avoid simply restating the code. For example, a good comment: 'Disable button to prevent its activation'. And a bad comment: 'Set cmd = False'

A good test to see that you have got the level of comment right is to ask yourself the question, 'Could someone understand what the program does if all but the comments were removed?'

3 Lay Out Code to Increase Legibility

Just as it is important for an author to split a book into chapters and paragraphs that aid reading, so it is important for the developer to consider the layout of the code and how that can aid the readability of the code. In particular any code branch (an IF..THEN...ELSE construction) and any code repetition (a WHILE...END WHILE construction) should be indented so that it is easy to see where it starts and ends.

4 Expect the Unexpected and Deal With It

Before you open a file, make sure that the file is present. Before you set focus to a control, make sure that the control is visible and enabled. Try to work out what conditions could cause your code to fail and test for them before they cause the program to fall over.

5 Name Your Variables to Aid Readability

There are a number of strategies to variable naming. The key is to be consistent and to be as informative as possible. If you name a variable nMonth, you give the programmer extra information as to what that variable is expected to contain. The Researcher of this entry prefers what's known as the Hungarian notation. This involves putting certain combinations of letters at the beginning of a variable to indicate what type it is - 's' for a string, 'l' for a long. It is known as Hungarian notation because it was introduced by Dr Charles Simonyi of Microsoft, who hailed originally from Hungary. Whichever style you choose to use, just remember to be consistent.

6 Keep Your Functions and Subroutines Simple

A function or subroutine should ideally only do one thing. One of the commonest sources of misunderstandings is a function that performs a number of different operations. It should be split into separate functions for each operation which are easy to reuse, and so that the scope of a code change is easy to understand.

7 Avoid Global Variables

Functions and variables that are only used in one module should not be visible outside that module. Variables that are only used in a function or subroutine should not be visible outside that function or subroutine. Having global variables (variables which are visible to any and every part of the program with no protection from misuse) encourages the bad habit of changing critical variable values anywhere in your program, rather than in the specific area to which the variables relate. This can be an endless source of hard-to-find bugs. Keep your variables local and as private as your language allows.

There are many other hints and tips which can help you become a better and more efficient programmer and the programs you write more maintainable, but the seven secrets listed above will serve as a good foundation - however high you build upon them.



Clip/Bookmark this page
This article has not been bookmarked.
ENTRY DATA
Written and Researched by:

Is mise Duncan

Edited by:

Bernadette Lynn, Home Educator

Referenced Entries:

Java - the Programming Language
C - the Programming Language

Referenced Sites:

Dr Charles Simonyi of Mic...

Please note that the BBC is not responsible for the content of any external sites listed.


CONVERSATION TOPICS FOR THIS ENTRY:

Start a new conversation

People have been talking about this Guide Entry. Here are the most recent Conversations:

TITLE
LATEST POST
Further ReadingSep 24, 2004
Avoid writing comments where possibleJun 21, 2001
More thoughts....May 17, 2001
More on commentsMay 16, 2001
Get the Spec RightMay 16, 2001
Link to MSDN is brokenMay 16, 2001
How to write unmaintainable codeMay 16, 2001
Good parcticeMay 15, 2001




Disclaimer

Most of the content on h2g2 is created by h2g2's Researchers, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the BBC. The BBC is not responsible for the content of any external sites referenced. In the event that you consider anything on this page to be in breach of the site's House Rules, please click here. For any other comments, please start a Conversation above.




About the BBC | Help | Terms of Use | Privacy & Cookies Policy