3

this is my first time here.

I'm a physics grad student and I've recently found a research group to work with. We study statistical mechanics using computer models, so there is a significant programming aspect to it.

My first job has been to take the old data analysis code (written in java) and clean it up in c++. The guy before me who wrote the original program used the traditional physics method of using an enormous amount of spaghetti code and shoving it all in a constructor. This irked me quite a bit, so I divided it up into separate classes and now it has a reasonable class structure.

In the past, whenever we had defined a class we would put its definitions and its methods in a header function, then to use it we would just include it. I know this breaks the rules of how to use .h and .cpp files, but is there any reason other than code etiquette to compile and link a class object rather than include it?

Sorry for the long question.

1 Answer 1

5

The idea of headers is that we separate the public interface (i.e. declarations) from internal implementation details (i.e. the actual method bodies). This split has all kinds of advantages:

  • There's a helper function I need? Let's just put it into the .cpp file, and outside code cannot see it. I can also do things like using std without interfering with other code.
  • We are stuck in a C mindset and want to use a macro? Declare it in the .cpp file to avoid polluting outside code.
  • There is a bug in a method? After fixing it, we only need to recompile that single .cpp file and re-link the application. This is much faster than recompiling everything.
  • Related to that: header files are often included multiple times, and are therefore recompiled again and again … conserver compilation time, and put as little information as possible into the headers.
  • Headers make the handling of cyclic dependencies across multiple files possible.

There are also a few drawbacks with headers, but you can't do anything to change that: headers are code duplication, they are idiotic language design, the C preprocessor is of an outdated design, and encapsulation (the split between public interface and implementation details) doesn't work in C++ because private fields of a class are part of the public interface.

What do we take from that? Mainly the point about compilation time. Headers allow you to recompile only those parts that have changed. This should be supported by virtually any build chain – learn about the make program if you are currently doing everything by hand.

2
  • As an aside: Physicists in general know far too little about software development for the amount of code they write, and I immediately recognized the code style you talked about :( Thank you so much for trying to bring a bit of sanity to physics code.
    – amon
    Commented Apr 17, 2014 at 20:03
  • Good answer. Generalise: all scientists, engineers, economists and business people are guilty of the same sins, plus they won't pay experts to help them get it right. Come the revolution...
    – david.pfx
    Commented May 31, 2014 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.