How to Make a Perfect Software
Sorry guys, if you’re coming here just to find out ‘How to make a perfect software’ then you are in the wrong place. Because you never Write Perfect Software, just accept it as an axiom of life. And you should know that there is no perfect software exist in this world. But I can help you with this kind of problem, because a Pragmatic Programmer can turn it into an advantage. That’s what I want to tell you in this session.
So, basically knowing that no one can writes a perfect code, Pragmatic Programmers code can prevent their own mistakes. One of the approaches that they do is :
Design by Contract: clients and suppliers must agree on rights and responsibilities.
Contract defines your rights and responsibilities, it is like you have an agreement with other party that concerning repercussions if either party fails to abide by the contract. So, every part in the contract you must followed and deal with rules so that you and the other party can achieve the goal that you want to go. But, how Contract works in programming?
According to Bertrand Meyer who developed the concept of Design by Contract. “It is a simple yet powerful technique that focuses on documenting (and agreeing to) the rights and responsibilities of software modules to ensure program correctness. What is a correct program? One that does no more and no less than it claims to do. Documenting and verifying that claim is the heart of Design by Contract.”- That was he said.
So, perhaps you want to build a function that important to your system or project. You have to consider first what kind of function that you want to make, like before the function start maybe the function needs something to satisfy the output. This is a several expectations that you can follow to make your function better :
- Preconditions. Basically, it means what must be true in order for the routine to be called; the routine's requirements. A routine should never get called when its preconditions would be violated. It is the caller's responsibility to pass good data.
- Postconditions. what the routine is guaranteed to do; the state of the world when the routine is done. The fact that the routine has a postcondition implies that it will conclude: infinite loops aren't allowed.
- Class invariant. A class ensures that this condition is always true from the perspective of a caller. During internal processing of a routine, the invariant may not hold, but by the time the routine exits and control returns to the caller, the invariant must be true. Basically it is more like conditional to determine what is the result of the function.
You can see that from the picture beside that preconditions really need to ensure either the input values are good for the function or not. And inside the function there is an exceptions to catch the outlier input values so that every side effect can be throw out. And the postconditions are the output that needed for the system. But where is the invariant? So, basically invariant class is to make sure that condition is true from the perspective of the function. But there is also a way to implement the invariant by using Loop Invariants. It is usually using for eventual goal of a loop, but it can be optional because it is also valid before the loop process and each iteration through the loop.
The picture above shows the example of using the Preconditions, Postconditions, and Invariants. Here we are saying that nodes in this list must always be in increasing order. When you insert a new node, it can't exist already, and we guarantee that the node will be found after you have inserted it.
So, maybe that’s all that I can share with you about how DBC(Design by Contract) can help for make a better of software instead of perfect software cause there is no perfect software. And if you want to know more about the content, you can check this book. Because mostly all of this content is coming from that book.
Last but not least, even thought there is no perfect software. You have to embrace with it and celebrate with it, because perfect software obviously doesn’t exist. Just thinking how to turn it into an advantage.