views:

244

answers:

5

Hi

I have a large code base. The whole code is in C. I feel that inappropriate attention has been given to design.

What evidences should I look for in the C code base that are strong indicators of bad/poor design. And can some of theses indications be provided by some automated tools.

Thanks Vivek Kumar

+4  A: 

Whilst there is a relationship between design and implemetation, they are not 100% aligned. i.e. A good design could be coded badly, and vice versa.

Some indicators of poor coding:

  • Poor variable and method naming
  • Lack of error handling, or 'ignore all' error handling
  • Memory Leaks (use a tool to find them)
  • Casting all pointers to void *
  • Poor separation of concerns
  • Large amounts of repeated code
Mitch Wheat
"Large amounts of repeated code" is almost always a dead give away.
Boltimuss
+1 for the memory leaks
Frank V
+4  A: 

Considering it is C, I would say that a very good indications would be:

  • functions spanning over 1000s of lines
  • enormous switch - case constructs
  • more than 4 parameters (performance penalty in ARM systems)
  • every header file includes every other header file
  • "arrow code" (has been widely discussed in other questions)
Ignas Limanauskas
+13  A: 

Among the indicators:

  • non-orthogonality of the code. a modification here would introduce a cascade of modifications there and there and there
  • low encapsulation and implementation hiding. various parts of the code have to know internal details of other parts of the code in order to operate. A change in the implementation detail will ruin the functionality and require massive changes
  • presence of global variables
  • presence of a huge catch-all include file which is included by any file
  • absence of unit testing (may indicate poor modularization)
  • functions with gigantic numbers of parameters, static variables, large amount of variables.
  • insane preprocessor/macro tricks
  • dishomogeneity of function/variable naming
  • comments contradicting the code, or comments with large amount of ascii art (will never be updated)
  • presence of goto
  • presence of horrible things like modifying the loop variable inside a loop, converting pointers to void * in order to silence compiler warnings, usage of uninitialized buffers, use of well known traps like scanf, strcpy, strtok and so on.
  • software developed in the academia
Stefano Borini
+1 for absence of unit testing, you beat me to it.
Luca Matteis
I would be surprised if many legacy C code bases had unit tests.
Mitch Wheat
me too, but if there are, it's a potential sign of good design, so in this case is a required but not sufficient flag for a good or cleaned design.
Stefano Borini
+1  A: 

It seems no one has mentioned this: absence of comments

nairdaen
Some would say that the better the design, the less comments are needed..
Jim Arnold
Agree with Jim, but I would say comments and design have no direct relationship. However, no comment at all is a very bad sign, it's a pain for the maintainance. A good programmer making good designs is likely to write appropriate comments in a reasonable quantity. So, +1.
Jem
+2  A: 

If I had to analyze a big application looking for these indications I would run three analyzes on it:

  • One that measures cyclomatic complexity, coupling etc.
  • A lint program of some sort.
  • I would also compile the application myself and look at the warnings the compilation produces.

I would look at these results and if the application is poorly designed it will certainly show. Especially the first test tells a lot, code that has not been properly designed tends to have overly long functions, have complex structures and be tightly coupled. This sort of code also typically has lots of dead code and has a poor comments:code ratio. One free tool I found that does at least some of this analysis is cccc.

Makis