Programming is just another tool like hammer or plier the only difference is that lot of it is mental while lot of work with hammer is physical. But this doesn’t mean that we should be using U-shaped hammer, that would be really stupid. The reason behind the design of the hammer is Force that can be produced by swinging it and it doesn’t require complexity, a simple piece of wood and iron head works. Programming should also be like that, having reasonable design.
In my early days, projects used to have messy programs but nevertheless they would just work fine. A function here and a function there with arbitrary code inside them that would just work. At that time, goal wasn’t writing the best program but to make the project work. The feeling that I have built something. As I have practiced more and more and built something for longer term made me realise that program should also have design. It should be simple and follow a pattern.
Patterns are like musical notes, once you get hold of few you can imagine what can come next.
See the picture at the top, that’s what most programs look like. We all write something which ends up looking like that. Functions getting called and written at arbitrary places, passing data and modifying at unknown places and sometimes there is a legacy code which just doesn’t let you write improved version because it’s just too much work. Hence, design is important. In programming, it is called design pattern.
Design pattern helps in structuring of code in such a way that if you know how one piece of the code works, rest can be figured out. They are inspired by real-life structures and manufacturing system. Visualising code as a physical system is of great help as brain can easily consume the information and can draw parallels. Design patterns are available for all languages (google “<language> design pattern”).
The other paradigm of programming is anti-pattern. Anti-pattern is a solution that seems to be working but is counter-productive. When working on a solution like this, it can be perceived that the solution can be easily implemented but when approached it becomes highly complex, confusing and probably inefficient. A programmer must study both aspects of the programming language, not only will this help in writing better code but in framing better solutions. The complexity of solution is often related to the design we have in our head and having structured thoughts leads to better and quick implementation.
The other reason why our programs should have a good design is the cost of maintenance. It may seem like that the cost of writing code that works is less but over the period it will end up costing more. Once there is a bad code in the system, it will continuously need fixes and rework which ends up costing more than it would have otherwise. The cost is not only money but time as well, which is far more important. But you should not go to the other extreme as well and write the great code because making it perfect is nearly impossible, it will always have shortcomings. A good code is which anyone can read and doesn’t require frequent maintenance.