OpendTect software development rulesIntro | Requirements | Explicit rules | Where to go from hereIntroSoftware 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:
RequirementsNice 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:
and:
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:
UniformThis 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:
Simple & EasyAny 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 rulesThe 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 rulesFirst 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.
LayoutNo 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 hereFrom 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 codeThe rules are:
|