Rules [OpendTect Programmer's manual V4.2]

OpendTect Software development rules

Intro | Requirements | Explicit rules | Where to go from here

Intro

Software engineering is a game of trade-offs. Performance vs generality, flexibility vs stability, priorities vs general goals, and so on, and so on. A good software engineer weighs all pros and cons and comes up with (near-)optimal solutions, often trying to get the best of everything. Of course, in fact, sometimes things can be completely ignored (e.g. in dialog-UI's performance is seldomly an issue).

Goals

When creating and maintaining software code, invariably one wonders what choice will come out optimally. To define 'optimal', we have to define the goals we want to maximise. The most obvious is:
(1) Total time spent (man-hours)
Less obvious, but also very important is:
(2) Total programming pleasure
The issue is that we software developers have to 'live' in the code, for many hours each day. Nothing is more dissatisfying than to have to go through code that looks like crap, even though the code may be working. Not being able to find what you need, is another issue. Too complex code, too. Finally, maybe an item not really fitting in the list, but still:
(3) Team-readyness
If you find pleasure in typing 15-level '?'-statements - so be it. If your entire team likes it there is no problem either. But chances are you will be crucified by your team members when they have to do anything with your code.


Requirements

Nice and neat

Good code should look good. You have to find joy in making the things you deliver look as good as (reasonably) possible, and as easy to understand as possible. Compare these two class definitions:

class SizeKeeper
{
public:
		SizeKeeper() : sz_(0)		{}

    int		size() const			{ return sz_; }
    void	setSize( int n )		{ sz_ = n; }

protected:

    int		sz_;
};
and:
class X { public: X() : n(0) {}
protected: int n;
public: int N() const { return n; } void sn(int p) { n = p; } };

The second class definition looks like crap, and is difficult to understand, especially when you imagine the code where the class is used. Maybe the first definition isn't that clear for everybody immediately at the start, but it's easy to see that once you get used to the style, it will be easy and fun to work with this kind of code.

For this to work, a team must agree on a style. The style characteristics are chosen so they match the requirements of esthetics/pleasure and time minimisation.

It may look like the second class definition has an advantage over the first in the time spent creating it. Nothing is more true. Time in software development is spent on many things, and actually typing the code is just a tiny component:

For most practical purposes, the influence of typing time on the total time spent can be neglected.

That leads to an important principle:
Rule (1): Make your code look good right from the start.
Waiting for a clean-up stage is a serious mistake. Already during creation of the software, from the very start, the effects of sloppy code will hit you where it hurts. Even if you have to re-type sections 10 times, it is better to have the code really neat at all times. Only then you can see that the re-working is necessary. The earlier you detect that constructions are not intuitive, logical, and easy to understand the earlier you detect that your code is actually bad.

Uniform

This is not a point to be taken lightly. Other team members will at some point have to change your code, other team members will at some point have to debug your code. Uniformity makes sure this is as easy as possible. Remember this: changing code you haven't made yourself is never easy, so do everything you can to help your team members. Moreover, changing code you've made some time ago is never easy, so you're even doing it for yourself.

The implications are simple although a lot of programmers have a lot of problems with it:
Rule (2): Make your code look just like all the other code.
Combining (1) and (2) could casually be described as: make sure all team members feel at home in your code at all times.

Simple & Easy

Any complex process can be broken up into simple steps, any complex object can be broken up into simple objects. Always consider yourself as publishing something that needs to be read by others. Take them by the hand and make it easy to understand what you are doing, and why you are doing it. Avoid repetitions, complex constructions or long lines. Make things compact if that will make things clearer, or uncompact if needed.

The best code you will ever make will invariably look as if it has cost hardly any time to make. Like good dancers make it look like there is no effort involved, an excellent solution always looks simple, compact and easy to use.


Explicit rules

The way we do things in OpendTect is not a 100% fixed body of rules. Moreover, we tend to say 'rather do this than that', or sometimes we change our point of view. Still, we almost unanimously agree on almost every issue. To lower the time to discover how we do things, next to going through lots of code, you can make use of the rules that follow.

OO and general rules

C++ rules

Semantical/typographical rules

First of all: the naming of classes, variables, namespaces, etc. is extremely important. You want to optimise understandability and compactness, in doubt always go for understandability. Naming should be as intuitive as possible. If you cannot find an intuitive name, consider the possibility that your design is not right. Well designed classes and methods hardly ever have non-intuitive names. If you are really convinced you're right but still you can't find anything intuitive, make sure you explain the meaning in comments.

Layout

No other subject brings up this many discussions. While it's simple: choose a policy and stick to it. The end result is what counts: readability, compactness, understandability. Thus all rules can be broken if it really helps those properties, but they rarely are.


Where to go from here

From now on, new code will be as described above. What to do if the code you're changing is not good according to these standards? That depends on the amount of work vs the amount of time vs the importance of the deviation.

Adapting code

The rules are: