views:

537

answers:

17

First let me say, I am not a coder but I help manage a coding team. No one on the team has more than about 5 years experience, and most of them have only worked for this company.. So we are flying a bit blind, hence the question.

We are trying to make our software more stable and are looking to implement some "best practices" and coding standards. Recently we started taking this very seriously as we determined that much of the instability in our product could be linked back to the fact that we allowed Warnings to go through without fixing when compiling. We also never bothered to take memory leaks seriously enough.

In reading through this site we are now quickly fixing this problem with our team but it begs the question, what other practices can we implement team wide that will help us?

Edit: We do fairly complex 2D/3D Graphics Software that is cross-platform Mac/Windows in C++.

+4  A: 

Test-Driven Development. TDD helps check for logic errors at the development phase.

Daniel A. White
+5  A: 

There are a lot of consultants/companies who have coding rules to sell you, you should have no difficulty finding one. However, one that doesn't first ask you the field you are in (you didn't mention it in your question) is providing you with snake oil.

Pascal Cuoq
@Daniel: Makes perfect sense. Web design rules are different than embedded rules.
Paul Nathan
I've added what kind of software we do, thanks.
zerocl
You're right that there is no silver bullet, but there are a lot of practices which apply pretty well to most domains.
Brendan Enrick
@benrick @Daniel I speak as someone who works with developers of critical embedded software (rather confidential. "Embedded" makes their needs particular and "critical" makes them particular again inside "embedded").
Pascal Cuoq
+10  A: 

Typically, the level of precision/exactingness in coding standards/process is directly connected to the safety level required. E.g., if you are working in aerospace, you will tightly control pretty much everything. But, on the other end of the spectrum, if you are working on a computer gaming forum site...if something breaks, no biggie. You can have slop. So YMMV, depending on your field.

The classic book on coding is Code Complete 2nd edition, by Steve McConnell. Have a team copy & strongly recommend your developers purchase it(or have the company get it for them). That will satisfy probably 70% of the stylistic questions. CC addresses the majority of development cases.

edit:

Graphics software, C++, Mac/Windows.

Since you're doing cross-platform work, I would recommend having an automated "compile-on-checkin" process for your Mac(10.4(maybe), 10.5, 10.6), and Windows(XP(maybe), Vista, 7). This ensures your software at the least compiles, and you know when it doesn't.

Your source control(which you are using, I assume), should support branching, and your branching strategy can reflect cross-platformy-ness as well. It's also advantageous to have mainline branches, dev branches, and experimental branches. YMMV; you will probably need to iterate on that and consult with with people who are familiar with configuration management.

Since it's C++, you will probably want to be running Valgrind or similar to know if there is a memory leak. There are some static analyzers which you can get: I don't know how effective they are at the modern C++ idiom. You can also invest in writing some wrappers to help watch memory allocations.

Regarding C++...The books Effective C++, More Effective C++, and Effective STL(all by Scott Meyers) should be on someone's shelf, as well as Modern C++ by Andrescu. You may find Lippman's book on the C++ object model useful as well, I don't know.

HTH.

Paul Nathan
2nd the motion for Code Complete. I was just about to post it myself.
wheaties
Ithink your answer is great, but I strictly code every thing I do. Coding strictly with every thing keeps me in practice. Also, since the small apps are more simple, I learn things I wouldn't have if I wrote sloppy code. Like the other day, I found that, in Perl, you can append to a list you're iterating over. Why I didn't know this already after 10 years of perl development I have no idea.
Elizabeth Buckwalter
+1 for source control, especially distributed source control.
Tchalvak
@Elizabeth: I'm not *advocating* sloppy code. I'm simply noting that the quality/formality/process/strictness bar moves much higher as you go towards life-critical systems. You can always jump high; sometimes you must jump high. :-)
Paul Nathan
+1  A: 

This blog post describes a lot of the common practices of mediocre programming. These are some of the potential issues you're team is having. It includes a quick explanation of the "best practice" for each one.

Brendan Enrick
+3  A: 

You don't mention any language, and while it is true that most of coding standards are language independent, it will also help you in your search. On most of the companies I had work they have different coding standards for different programming languages. So my advice will be:

  • Choose your language
  • Search the web since there are plenty of standards out there for your language
  • Gather all the standards you found
  • Divide your team into groups and give them a few of the documents to analyze. They should come with a list of things they think worthy to have in their new standards.
  • Have a meeting so each group present its findings to everybody (there will be a lot of redundancy between groups). That should be an open discussion and everybody's opinion should be accounted.
  • Compile a list of the standards that were selected by the majority of the coders and that should be your starting point.
  • Perform semi annual reviews of the standards, to add or remove things.

Now, The logic behind this is : Most of the problems from putting a coding standard from scratch is developer's acceptance. Each of us have a way of doing things and it sucks when somebody from the outside believes one way of doing things is better from another. So, if developers understand the logic and the purpose of the coding standards then you have half of the work done. The other thing is that standards should be design and created specifically for your company's needs. There will be some things that will made sense, and some that don't. With the above approach you could discriminate between those. The other thing is that standards should be able to change over time to reflect the company needs, so a coding standard should be a living document.

Freddy
+4  A: 

Get everyone to read and discuss various standards and guidelines. I (as well as Stroustrup) suggest the Joint Strike Fighter coding standards. Ask your developers to classify the guidelines therein among

  • Already met
  • Could be met easily (few changes from current condition)
  • Should work toward in old code and follow in new development
  • Not worth it

Have the long technical discussions, and settle on a set for the team to adopt.

Novelocrat
The JSF C++ coding standards are available in .pdf format here: http://www2.research.att.com/~bs/JSF-AV-rules.pdf
Dominic Dos Santos
+1  A: 

The first thing you need to consider when adding coding standards/best practices is the effect it will have on your team's morale and cohesiveness. Developers usually resent any practices that are imposed on them even if they are good ideas. The people issues have to be addressed for a big change to be successful.

You will need to involve your group in developing the standards and try to achieve consensus. That said, you will never get universal agreement on anything, so you will have to balance consensus and getting to standards. I've seen major fights over something as simple as tabs versus spaces in source.

The best book I've seen for C/C++ guidelines in complicated projects is Large Scale C++ Software Design. That book along with Code Complete (which is a must-read classic) are good starting points.

David G
About the tabs vs. spaces: It is sometimes worth major fights. :)People constantly reformatting code with tabs/spaces can make merging branches really unpleasant.
Mattias Nilsson
I agree completely that you should pick one or the other. Obviously, spaces are better than tabs :)
David G
+1  A: 

One thing you should have rules about is some kind of naming standard. It just makes life easier for people while not being really invasive.

Other than that, I'd have to say it depends on the level of your team. Some need more rules than others. The better people are, the less "support" they need from rules.

If you want a complete set of coding rules to control every little detail, you're going to spend lots of time arguing about rules and exceptions to rules and what you should write rules about. I'd go with something already written instead.

If you are concerned about quality then one thing you could do that really isn't about rules, is: Automated building and testing. This has helped me a lot. Once you find a problem, it really helps to have an environment where you can write a test to verify the problem. Fix the problem and then easily add your test to an automatic test suite that makes sure that sort of problem can't come back without being spotted. Then make sure these run often. Preferably every time someone checks something in.

Mattias Nilsson
+1  A: 

If you decide to have coding standards, you want to be very careful about what you put in. If the document is too long or focuses on arbitrary stylistic details, it will just get ignored and nobody will bother to read it. Often a lot of what goes into coding standards is just the preferences of the person that wrote the document (or some standards that have been copied off the web!). If something is in the standard, it needs to be very clear to the reader how it improves quality and why it is important.

I would argue that a large proportion of what makes code readable is to do with design rather than the layout of the code. I have seen a lot of code that would adhere to the standards but still be difficult to read (really long methods, bad naming etc.) - you can't have everything it the standards, at some point it comes down to how skilled and disciplined your developers are - do what you can to increase their skills.

Perhaps rather than a coding standards document, try to get the team to learn about good design (easier said than done, I know). Make them aware of things like the SOLID principles, how to separate concerns, how to handle exceptions properly. If they design well, the code will be easy to read and it won't matter if there are enough white lines or the curly braces are in the right place.

Get some books about design principles (see a couple of recommendations below). Maybe get the the team to do some workshops to discuss some of the topics. Perhaps get them to collectively write a document on what principles might be important for their project. Whatever you do, make sure it is the team as a whole who decides what the standards / principles are.

http://www.amazon.co.uk/Principles-Patterns-Practices-Robert-Martin/dp/0131857258/ http://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

Martin Wickett
+1  A: 

If your framework requires certain rules to function well, put those in your coding standard.

Joshua
+1  A: 

These basics are good for most any industry or team size:

  1. Use Agile methodology (scrum is a good example). http://www3.software.ibm.com/ibmdl/pub/software/rational/web/whitepapers/2003/rup_bestpractices.pdf
  2. Use Test-driven development. http://www.agiledata.org/essays/tdd.html
  3. Use consistent coding standards. Here is an example document: http://www.dotnetspider.com/tutorials/BestPractices.aspx
  4. Get your team familiar with good design patterns. http://www.dofactory.com/Patterns/Patterns.aspx

You can't go wrong with these basics. Build from there with new team members who have been there and done that. I'd strongly suggest pair programming once you've got those guys on the team. It is the best way to infect people with best practices.

Best of luck to you!

Rap
+2  A: 

Don't write your own standards from scratch.

Chances are there are several out there that define what you want already, and are more complete than you could come up with on your own. That said, don't worry too much if you don't agree 100% with it on minor matters, you can swap in some parts of others, or call some infraction of it an warning rather than an error - depending on your own needs. (for example, some standards would throw a warning if the length of a line is more than 80 characters long, I prefer no more than 120 as a hard limit, but would make sure there was a good reason - readability & clarity for example - if there was > 80).

Also, do try to find automated methods of checking your code against the standard - including your own minor changes as required.

Alister Bulman
+3  A: 

Code reviews have been shown to provide significant benefits to code quality, even more so than traditional testing. I would suggest getting in the habit of performing routine design and code reviews; the number of stages at which reviews are performed, the formality and detail of the reviews, and the percentage of work subject to review can all be set according to your business requirements. Coding standards can be useful when done right (and if everyone's code looks similar, it is also easier to review), but where you put your braces and how far you indent blocks isn't really going to affect defect rates.

Also, it's worth familiarizing yourself and your peers with the concept of technical debt and working bit by bit to redesign and improve parts of the system as you come in contact with them. However, unless you have comprehensive unit testing and/or processes in place to ensure high code quality, this may not help things.

qid
+3  A: 

Given that this is Stack Overflow, someone should reference The Joel Test. I like to automate as much as possible, so using Lint is also a must.

jackjumper
+1  A: 

Besides books already recommended, I would also mention,

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices by Herb Sutter and Andrei Alexandrescu (Paperback - Nov 4, 2004)

grokus
well-recommended authors. I've not read it myself, but I'm sure it's good.
Paul Nathan
A: 

If you're programming on VB.NET, make sure Option Explicit and Option Strict are set to ON. This will save you a lot of grief tracking down mysterious bugs. These can be set at project level so that you never have to remember to to set them in your code files

Conrad
A: 

I really like: MISRA C standard (it's a little strict tho' but the ideas hold for C++) and Hi-Integrity's http://www.codingstandard.com/HICPPCM/index.html C++ standard which borrows heavily from MISRA

LDRA (a static analysis tool) uses these standards to grade your work (this I don't use as it's expensive) but I can vouch for running cppcheck as a good 'free/libre' static analysis checker.

queBurro

related questions