Understand the Machine

December 6, 2018 0 By Rosa

Approved by Valentin Utkin

When programmers are writing code, they feel like computer gods and their code is the best. And sometimes it is true! However, more often than not, their code is not efficient or the programmer used a shortcut or dirty code to get something to work. Sometimes, the inexperience of the programmer leads to inefficient code. Since I am a programmer on intermediate level, I do not have much experience with writing the most efficient programs. That is why I am reading the book “Write Great Code Volume 1: Understand the Machine” by Randall Hyde.

Before reading about computer architectures and operating system design, I figured it would be a good start to get some insight into efficient code. With high-level programming languages dominating the market these days, I’ve grown up programming Java. These languages do a lot for the programmer and provide you with many ‘magical’ methods. However, without you even knowing it, many of these methods cost you a lot of performance. Write Great Code Volume 1 provides you with the information about the things that are going on under the hood. The book displays to a certain degree what the CPU does for you when you call upon certain action. This will help you understand and appreciate what the CPU and the lower-level programming languages do for you.

It looked like the perfect book to start studying for developing my own operating system. I am planning on posting about the things I read, so you can learn it too! I will touch upon several representation, memory, CPU architecture and much more! But first: what is great code?

Great code is software that is written using a consistent and
prioritized set of good software characteristics. In particular,
great code follows a set of rules that guide the decisions a
programmer makes when implementing an algorithm as
source code

Randall Hyde, Write Great Code Volume 1

According to Randall Hyde, good software has a certain set of software characteristics. I have seen many of these characteristics being discussed during my Software Engineering course. Although everyone has different requirements for software, there is a general consensus which of these characteristics contribute most to good software. They also transcent over multiple decades and can be applied on nearly every platform. These characteristics are:

  • It uses the CPU efficiently
    • Efficient code is equivalent to fast code! Who doesn’t love fast software. That’s right, no one!
  • It uses memory efficiently
    • With high-level programming languages, it is harder to comply to this characteristic. You can apply a certain degree of memory management but you cannot change the JVM code which ultimately uses the memory. For real memory efficiency, programming with lower-level languages is the solution. In those cases, small code is usually the most efficient.
  • It uses system resources efficiently
    • I bet you’ve seen the images and memes about all browsers and how much they love your RAM. The more you have, the more they’ll eat it. Browsers are an excellent example of a program that doesn’t use your system resources efficiently. Resource management is hard, but it helps to boost the performance of your software.
  • It is easy to read and maintain
    • Everybody says their code is easy to read but more often than not the opposite has been proven. I confess, I bet my code is much harder to read with all its one-line if-statements. However, inserting comments to a certain degree does help your fellow programmer to understand your code and built further upon it.
  • It follows a consistent set of style guidelines
    • To increase readability of the code, the way the code is written has to be consistent. These conventions may differ per project but when written down it becomes a style guideline. When a new programmer starts on the project, he or she can read the guideline and can start programming with the same style as the rest. This keeps the code clean, consistent, and thus more readable.
  • It is easy to enhance
    • Software is continuously growing and expanding. Thus it is essential that software is modular and expandable. Adding new features for new releases or attaching a new service should be possible with relative ease.
  • It is well-tested and robust
    • When your code is well-tested and robust, you can say one thing for use: it works and won’t break easily. Testing is a highly important part of software development, but often neglected unfortunately. However, there is nothing worse than your software working sometimes. As there are many ways to test your software, I’ll be covering software testing in a separate post.
  • It is well-documented
    • This is another, often neglected, vital part of software development. I get that you won’t document your software when you’ve written it for yourself. However, when developing in a team, new members will come and go and they need to understand what they are working on. What has been done, what does this method do, what does that method do, which architecture is used, where can I attach my component? These are all questions that can be answered by having good documentation of software.

There are many more requirements, but these are the key characteristics that make good software. I would highly recommend to apply as many of these characteristics to your software. However, I can understand that applying them all at once might be a difficult task. So, when you are still new to programming, start with one and try to apply another characteristic when starting on a new project. It could help you gradually adjust your coding style to comply to all the characteristics of good software.