tags:

views:

526

answers:

9

Is there a good website, textbook, or other reference for coding practices which can help minimise errors? (No, I couldn't find an answer to this on stackoverflow, or through google searches)

I'm talking about ways to structure the code, rather than things like code reviews/unit testing/etc.

Example: Structuring an if test to place the CONST on the left of the logical expression. If the programmer accidentally types '=' rather than '==', a complation error will result:

if (CONST == variable) -> compile error if accidentally type '='
if (variable = CONST) -> not detected by complier, always evaluates to true

EDIT (re. accepted answer): "Code Complete" chapter 19 'General Control Issues' explicitly covers my example, and "Writing Sold Code" pg 4-5 talk about = vs ==.

Like anything, there are pros and cons. "Code Complete" does a good job of discussing these.

A: 

There's a zillion. Google for "coding standards" or "coding guidelines".

Michael Burr
+1  A: 

Writing Solid Code.

C&P:

Winner of the Software Development Productivity Award--1994

Written by a former Microsoft developer and troubleshooter, this book takes on the problem of software errors by examining the kinds of mistakes developers typically make and includes Microsoft techniques for developing bug-free C programs. With the growing complexity of software today and the associated climb in bug rates, it's becoming increasingly necessary for programmers to produce bug-free code much earlier in the development cycle, before the code is first sent to Testing. The key to writing bug-free code is to become more aware of how and why bugs come about. Programmers can gain this awareness by asking two simple questions for every bug they encounter: "How could I have prevented this bug?" and "How could I have automatically detected this bug?" The guidelines presented in this book are the result of programmers regularly asking these questions over a number of years. WRITING SOLID CODE provides practical approaches to the prevention and automatic detection of bugs. Throughout, Steve Maguire draws candidly on the history of application development at Microsoft for cases in point--both good and bad--and shows you how to use proven programming techniques to produce solid code. If you're serious about developing world-class code, you'll benefit from Maguire's experience and practical advice in WRITING SOLID CODE.

1800 INFORMATION
+1  A: 
John
A: 

I think it is more impotent to learn how to debug things then waste time with trying to "minimize bugs by typing a certain way" - especially if you are a beginner. Of course you should be careful as to what you type; but it would be better to learn how to quickly take out bugs then to waste time/money reversing the operands in your if statement ;)

I don't mean ignore these things at all - but what I do mean is that you should organize your priorities

nlaq
A: 

There is also Correctness by Construction. Quite different from what you are discussing but still with the same goals.

DISCLAIMER: A bit involved and I have not yet figured out what it is.

Vulcan Eager
+3  A: 

"Code Complete" by Steve McConnell and "Writing Solid Code" both by Steve McGuire both give good advice on that sort of thing.

The Pragmatic programmer series has books on Unit Testing, Automation and version control.

Hope this helps.

ps. Also currently reading Kernighan and Pike "The Practice of Programming" which offers similar advice.

A: 

If you are using Eclipse, you can use the "CheckStyle" plug-in that will check your code for any specified errors.

The great thing about checkstyle if that you can apply standards like the Sun standards, etc... to your code and know that if you ewre to ever work at Sun, your code would look the same.

You can also define your own coding warnings/errors and everything, its pretty flexible.

Not sure if there is an equivallent for VS though...

Mark
I don't understand why this answer was voted down. I voted it up, simply because it's exactly what Kevin Haines was asking about
Sandman
Thanks Sandman, I guess we'll never know!
Mark
A: 

Whitespace, newlines. Long files are easier to read, superconcise code raises geek cred but is hard to read and maintain.

longfile is loooooong.

Paul Nathan
+1  A: 

The amount of bugs in my code went down when I started to adopt the rule of just a single loop in a function. The result is that you get a lot of little 6 - 10 line-functions which are just correct -- you can see that (and you can easily do very precise unittesting). This does not help you with conceptual bugs, but at least for me, it reduced the technical bugs a lot.

Also I adopted the practice of having small objects for about everything. Earlier, I rather hestiated when creating a new object. Now I just create one and look if it reduces complexity. If it does -- even only a bit -- keep it. Bugs are like Grues, just with Complexity and Code-lenght instead of darkness.

Tetha