What makes good code (7/7)
Code is data, data is code
Your software is not just code. For example, you cannot have good code in a software that uses databases, if you have a bad data design in your database. If you are making games, you will need files to store levels, objects definition files, images, sound files and models, so the way you handle these files is very crucial to your workflow and software quality. Good code is not just code, data must also follow good practices. Your code is also information that you are coding to the machine, it is knowledge in a sense. The maturity of a software is proportional to how much it has been used, tested and the information that is generated around it.
Once, a professor taught me an especially important lesson. He was talking about how internet search engines were built in the early days. They were indexes of pages, mostly done by human interaction, but the most important detail was that they were categorized in hierarchies. You had super categories like: entertainment, health, communication, education and so on. And for each category you have subcategories and eventually you have the sites that fit that hierarchy, like a file system with folders, subfolders and files. This is a good approach for file systems, not for search engines. Soon, they realized that if you have a website about educational entertainment or health communication you need to repeat them in both categories and that could scale quickly.
So, they came with a simple solution, there is no more hierarchy, we just add tags to the sites. As a result, an educational entertainment web site has the education tag and the entertainment tag. This example is the most iconic for me to explain the difference between Hierarchy and Aggregation. When you are making object oriented code you may have a tendency to make hierarchy relationships between classes, because it’s the default way, but the aggregation can handle more use cases than hierarchy, hence the Component Design Pattern was built. The idea is that your class has a list of components and these components will add or implement the behaviors of your “Base Class”.
Look at the big chest in the image above and imagine that it has something inside that you use every day. You will have to remove all the items on top of it, open the chest with a key, get what you want and place everything back every day. In computers, we have a concept of memory hierarchy where larger memory storage has lower access speed forming a pyramid. If you want to have quick access to some data, it must be at the top of the pyramid. The process of storing data from a result to avoid performing a calculation again or putting a data with frequent usage in a fast memory access is called Caching. Caching is used from processor architecture to web browsers and your code can benefit from it too.
Finally, we have in mind that we build software to run in a machine. There is a lot of effort in Computer Science to build abstractions that we can work with and somehow the compiler or some magic middleware will make the best implementation possible for us. Such promise has been there for years, but we are not even close to that. During my undergrad years, Object Orientation was presented to me as the pinnacle of solutions, but this idea is wrong. Object orientation is a tool and like every other tool it has its advantages and disadvantages. Do not rely on a single technology/tool to solve all your problems.
We have Data Oriented as another approach to design systems that takes how data is processed in the machine into consideration. It is the only approach if you want to deliver applications with high performance. Dealing with low level requirements of machines might seem a burden, but if you rely on a magic system to give you performance and it fails to do so, now you have to understand how to achieve performance at the low level and how to trick the magic system to behave the way you want. Data Oriented is not useful just for memory management, it is useful to deal with data in files and databases. You should pay attention to what file format and database system best describes and handles your data.