Click here to Skip to main content
15,887,906 members
Articles / Operating Systems / Windows

Some Habits of Successful Programmers

Rate me:
Please Sign up or sign in to vote.
3.55/5 (69 votes)
23 Feb 2015CPOL4 min read 201.2K   51   109
Tips to make you a better citizen in the developer community

1. Code for Human Consumption

It is one of the most pervasive misunderstandings in computing is that the source code is for the computer's consumption. Computers work with low-level binary code, a series of impenetrable 1s and 0s 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 often means coding for clarity first, over efficiency and cleverness second. This is not to condone bad / inefficient code but rather to address premature optimization and optimization that costs maintainability.

2. Comment Often and Comment Well

The comment is the extreme example of a language element for human consumption (most 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.

  • Good comment: Disable the update command until the record needs to be saved.
  • Bad comment: Set cmd enabled = False

A good indication that you have got the level of comment right: could someone understand what your program is intended to do if all but the comments were removed?

It is a commonly expressed statement that we should have "self documenting" code but bear in mind that the code can only tell you what the code does - not why.

3. Layout 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 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 they start and end.

Code should be split across files (and indeed across the file system - use sub folders under your solution where that makes sense).

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. Also use explicit data type conversion wherever possible.

5. Name Your Variables and Functions 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 MonthNumber, you give the programmer extra information as to what that variable is expected to contain. The key point is to use the name to indicate what the variable is used for.

6. Keep Your Functions and Subroutines Simple

A function or subroutine should ideally only do one thing. One of the greatest sources of misunderstandings, in my experience, is a subroutine that does a number of different operations. This should be split into separate functions for each different thing it is doing, so that these in turn are easy to reuse, and the scope of a code change is easy to understand.

7. Scope Functions and Variables Appropriately

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. This prevents any use of a variable or function outside of where it makes sense to use it.

If you declare anything as public, you are responsible for how it can be used - preventing invalid data states, race conditions and so on.

8. Test as Much as Possible, as Soon as Possible

If you are an adherent of Test Driven Development you already have this but even for those not following TDD, there is great value in testing as much of your code as soon as you can. This means unit testing in the IDE before any code change is checked in and also maintaining a safety net of tests over all the existing functionality to make sure your latest changes don't break anything.

You should also include instrumentation or use a code profiler to see how well your application is running. In my experience, performance issues are often introduced gradually in the middle of code changes to fix other issues.

9. Keep Growing

Software development is still a young industry and best practice is still subject to change. When a new magic silver bullet programming language or achitecture pattern comes along - give it a try with an open mind. There is much still left to discover.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
QuestionAmen Pin
d_c_j26-Feb-15 3:33
d_c_j26-Feb-15 3:33 
General"The comment is the extreme example of a language element" Pin
jediYL24-Feb-15 20:14
professionaljediYL24-Feb-15 20:14 
GeneralComment often and well: another perspective Pin
WestieBoy8-Nov-14 18:02
WestieBoy8-Nov-14 18:02 
GeneralRe: Comment often and well: another perspective Pin
Duncan Edwards Jones21-Nov-14 19:11
professionalDuncan Edwards Jones21-Nov-14 19:11 
GeneralMy vote of 5 Pin
Edward Quixote6-Nov-14 23:17
professionalEdward Quixote6-Nov-14 23:17 
QuestionA comment very rarely be needed if the code is clean and readable.. Pin
Blake A Niemyjski6-Nov-14 12:53
Blake A Niemyjski6-Nov-14 12:53 
AnswerRe: A comment very rarely be needed if the code is clean and readable.. Pin
Duncan Edwards Jones6-Nov-14 20:26
professionalDuncan Edwards Jones6-Nov-14 20:26 
GeneralRe: A comment very rarely be needed if the code is clean and readable.. Pin
DeliberateGeek7-Nov-14 6:18
professionalDeliberateGeek7-Nov-14 6:18 
AnswerRe: A comment very rarely be needed if the code is clean and readable.. Pin
jnlt24-Feb-15 11:50
jnlt24-Feb-15 11:50 
GeneralMy vote of 4 Pin
i006-Nov-14 11:37
i006-Nov-14 11:37 
Generally agree...

First of all I would like to say that some of this may not relate to every programming language...

2.
Duncan Edwards Jones wrote:
A good indication that you have got the level of comment right: could someone understand what your program is intended to do if all but the comments were removed?

I don't think so ... I think that comments are VERY important, but should complement code, not be a "psudo replacement" for it (for readability obviously).
If a block is completely understandable by itself why bother ... eg: why bother commenting a one line function called TrimTabsOnly

3.
Duncan Edwards Jones wrote:
Code should be split across files

Defiantly agree my solutions always have many solution folders, partial classes, and projects within them... some people (particularly seem to) hate that I have so many projects in my solutions, but in large solutions they can also decrease build time ALOT.
IMHO partial classes and multiple projects for different components / controls etc are defiantly under-used by most of the programming populous.

4.
Duncan Edwards Jones wrote:
Try to work out what conditions could cause your code to fail and test for them before they cause the program to fall over.

And now for the other end of the spectrum ... error handling is something many programmers OVER-use!
... a good programmer, like you said, will A. handle errors BEFORE they occur, B. know when to use error handlers to capture "expected" exceptions, and if an error is NOT expected somewhere then there should be no handler at all!
Unhandled exceptions can be useful (for debugging of course); I have seen too many programmers putting try catch blocks around MOST of their code just for the sake of it, and then wonder where the error actually occurs because they have a message box that shows the error. People tend not to want to show the callstack to the end user in error handlers, and this I agree with, instead just leave the errors unhandled and let the built in unhandled exception handler throw them or build your own that shows you nicer information about the error (call stack here is OK). If you have done A and B correctly, you will have none or minimal unhandled exceptions anyway.

5, 6 Agree

7.
Classes within classes (no not subclassing : Poke tongue | ;-P )
This is another widely underused "feature" ... I even believe that Microsoft is a culprit of this... and I know that I will probably be flamed for this ... but...
If a class is used widely in another class and relates directly to it then don't be afraid to put the class in the class it is used in... if you think it is messy all being in one file READ 3. AND RESEARCH PARTIAL CLASSES.

Eg of MS being a culprit too...
TreeNode relates directly to treeview ... so IMHO a new node should be defined like this:
VB
Dim TestNode As New TreeView.TreeNode


Let me know if you agree or disagree ... just giving my two cents Smile | :)
Kris

modified 6-Nov-14 18:18pm.

GeneralRe: My vote of 4 Pin
Duncan Edwards Jones6-Nov-14 20:23
professionalDuncan Edwards Jones6-Nov-14 20:23 
GeneralRe: My vote of 4 Pin
i006-Nov-14 20:59
i006-Nov-14 20:59 
GeneralRe: My vote of 4 Pin
Duncan Edwards Jones6-Nov-14 21:51
professionalDuncan Edwards Jones6-Nov-14 21:51 
GeneralMy vote of 5 Pin
Jörgen Andersson6-Nov-14 7:07
professionalJörgen Andersson6-Nov-14 7:07 
GeneralRe: My vote of 5 Pin
Duncan Edwards Jones6-Nov-14 11:35
professionalDuncan Edwards Jones6-Nov-14 11:35 
GeneralVB6 help Pin
Anonymous1-Jul-04 8:19
Anonymous1-Jul-04 8:19 
GeneralRe: VB6 help Pin
Duncan Edwards Jones22-Mar-05 6:36
professionalDuncan Edwards Jones22-Mar-05 6:36 
GeneralCode Complete Pin
edunphy1930-Oct-03 8:15
edunphy1930-Oct-03 8:15 
GeneralSeperate data from code Pin
twb1529-Oct-03 20:45
twb1529-Oct-03 20:45 
GeneralRe: Seperate data from code Pin
Duncan Edwards Jones6-Nov-14 20:28
professionalDuncan Edwards Jones6-Nov-14 20:28 
Generalsmartass remark Pin
k4_pacific29-Oct-03 3:08
k4_pacific29-Oct-03 3:08 
GeneralRe: smartass remark Pin
Duncan Edwards Jones29-Oct-03 7:46
professionalDuncan Edwards Jones29-Oct-03 7:46 
GeneralType of comments Pin
garyknights22-Oct-03 4:24
garyknights22-Oct-03 4:24 
GeneralRe: Type of comments Pin
Kevin McFarlane27-Oct-03 23:46
Kevin McFarlane27-Oct-03 23:46 
GeneralCosmetic ... Pin
Sebastien Lorion21-Oct-03 11:00
Sebastien Lorion21-Oct-03 11:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.