Posts

Showing posts from 2017

#12. Always keep correctness in mind

Image
Bertrand Meyer starts the first chapter of his book Object Oriented Software Construction, 2nd Edition with the following sentence: Engineering seeks quality; software engineering is the production of quality software. He goes on to analyze the concept of software quality throughout the chapter. He defines it as a combination of several factors, some external (observable by the users of the software) and other internal (observable only by those who have access to its source code). External software quality factors are correctness, robustness, extendibility, reusability, compatibility, efficiency, portability, ease of use, functionality and timeliness. All of these are important and should be balanced one against the other. But one clearly stands out among them: correctness. Quoting [Meyer 1997] again: Correctness is the ability of software products to perform their exact tasks, as defined by their specification. If a software is not correct, everything else matters little. C...

#11. Don't use syntactic overloading

Syntactic overloading (or function overloading ) is a C++ feature which allows a class to have several functions with the same name, but with different parameters. To say "Don't use syntactic overloading" is the same as to say "Don't give the same name to different functions". class C {     ...     void add(int i);     void add(double d);     void add(C other);     .... } When you write this client code: C c; c.add(x); The compiler will call the appropiate version of the funcion add, depending on the type of x . This is what syntactic overloading is. It is called syntactic overloading to distinguish it from semantic overloading 8more properly called dynamic binding ), which is the one that happens when you use inheritance and polymorphism. Syntactic overloading may seem useful at first sight, but it has lots of drawbacks for no real advantage. It just can go wrong in too many ways. Code is more readable without syn...

#10. Choose the right loop structure

C++ has three loop instructions: for , while and do ... while . To choose wisely among them, you need to know what their differences are. 1) for A  for  loop makes sense when you repeat an action for a known number of times, or for all the elements of a known set. This is the syntax of a  for   loop: for (initialization ; condition; expression) {     statement } statement  is actually a compound statement, that is, a block of code. This is called the body of the loop. In contrast, the initialization, condition and expression together are called the header of the loop. The initialization is a single statement which ends at the semicolon I wrote after it. The condition is a boolean expression. The expression is a statement. It should be a single statement which modifies one variable which is involved in the condition (see #guideline #5 ). The initialization is performed. Then, the condition is evaluated. Then, two things can happen. I...