Click here to Skip to main content
15,886,740 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 201K   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

 
GeneralRe: Point #2 Pin
Michael P Butler21-Oct-03 7:41
Michael P Butler21-Oct-03 7:41 
GeneralRe: Point #2 Pin
Kevin McFarlane28-Oct-03 0:13
Kevin McFarlane28-Oct-03 0:13 
GeneralRe: Point #2 Pin
danitrol28-Oct-03 20:54
danitrol28-Oct-03 20:54 
GeneralRe: Point #2 Pin
Pit M.21-Oct-03 22:46
Pit M.21-Oct-03 22:46 
GeneralRe: Point #2 Pin
Arnaud Brejeon21-Oct-03 23:41
Arnaud Brejeon21-Oct-03 23:41 
GeneralRe: Point #2 Pin
Kevin McFarlane28-Oct-03 0:25
Kevin McFarlane28-Oct-03 0:25 
GeneralRe: Point #2 Pin
jhwurmbach29-Oct-03 21:14
jhwurmbach29-Oct-03 21:14 
GeneralRe: Point #2 Pin
Kevin McFarlane30-Oct-03 0:02
Kevin McFarlane30-Oct-03 0:02 
jhwurmbach wrote:
Oh yes!
And I am guilty of that myself.


Me too! Though I now try to get into the habit of continual refactoring, at least for new code written by myself.

jhwurmbach wrote:
It simply gets longer as more and more special cases have to work with this one method.

I think that a couple of things tend to happen:

1. As the code gets exercised during development developers gradually have to add complexity to their algorithms. What they should do is then tidy things up and factor out the extra code to private helpers. But many developers can't be bothered to do this and then at the end there's not enough time for them to do it.

2. As the software ages more developers get involved in maintenance and keep adding to what may once have been elegant routines. They too can't be bothered to factor out their changes to private helpers. Also it can be difficult to do this, especially if they've inherited very bad code.

jhwurmbach wrote:
Here, tools for semi-automatic refactoring would come in very handy.
Is there such a tool for C++?


There doesn't seem to be one. This may be because C++ is generally too mesy. Certainly most existing C++ code, at an implementation level, is not very good in my experience. Also see Bjarne Stroustrup on this.


jhwurmbach wrote:
I know it is for Java.But who wants to program Java? *duck&run*

Well, I wouldn't mind programming in Java, but no-one will pay me to unless I can claim to have several years' commercial experience.

But, in general, I think it's a case of choosing the most appropriate tool for the job. I'm primarily a C++ programmer but I recognise that it is not the most appropriate tool to use in all situations even though, more than any other language, it is capable of being successfully used in the widest range of situations.

BTW, if you don't like Java then I think C# appeals more to the diehard C++ programmer as it is closer to C++ than Java is. It does not jettison as many C++ features. Of course, Java is more comprehensively cross-platform. So if that is your primary consideration...

OTOH, if you want something that has equivalent power and performance as C++ you could try Eiffel. It's nowhere near as widely-used, of course.



Kevin
GeneralRe: Point #2 Pin
jhwurmbach30-Oct-03 0:08
jhwurmbach30-Oct-03 0:08 
GeneralRe: Point #2 Pin
Paul Conrad16-Aug-05 17:10
professionalPaul Conrad16-Aug-05 17:10 
GeneralRe: Point #2 Pin
KevinHall23-Oct-03 9:14
KevinHall23-Oct-03 9:14 
QuestionWhat about test first ? Pin
mcarbenay4-May-03 3:28
mcarbenay4-May-03 3:28 
AnswerRe: What about test first ? Pin
Duncan Edwards Jones2-Oct-03 3:23
professionalDuncan Edwards Jones2-Oct-03 3:23 
GeneralAnd ... use English for comments etc. Pin
Alexander Bischofberger15-Dec-02 22:29
Alexander Bischofberger15-Dec-02 22:29 
GeneralRe: And ... use English for comments etc. Pin
Dieter Hammer21-Oct-03 7:09
Dieter Hammer21-Oct-03 7:09 
GeneralRe: And ... use English for comments etc. Pin
jhwurmbach21-Oct-03 22:05
jhwurmbach21-Oct-03 22:05 
GeneralGood useful article Pin
Earl Allen15-Dec-02 22:10
Earl Allen15-Dec-02 22:10 
General... Pin
Jon Rista13-Dec-02 15:03
Jon Rista13-Dec-02 15:03 
GeneralRe: I must disagree... Pin
Anonymous13-Dec-02 15:57
Anonymous13-Dec-02 15:57 
GeneralRe: I must disagree... Pin
John Carson13-Dec-02 19:02
John Carson13-Dec-02 19:02 
GeneralRe: I must disagree... Pin
Johann Gerell14-Dec-02 2:46
Johann Gerell14-Dec-02 2:46 
GeneralRe: I must disagree... Pin
John Carson14-Dec-02 4:42
John Carson14-Dec-02 4:42 
GeneralRe: I must disagree... Pin
Johann Gerell14-Dec-02 7:45
Johann Gerell14-Dec-02 7:45 
GeneralRe: I must disagree... Pin
John Carson14-Dec-02 12:06
John Carson14-Dec-02 12:06 
GeneralRe: I must disagree... Pin
Dana Holt21-Oct-03 5:52
Dana Holt21-Oct-03 5:52 

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.