Chapter 1. What You Need to Know to Write Great Code
1.1 The Write Great Code Series
Write Great Code: Understanding the Machine is the first of four volumes in the Write Great Code series. Writing great code requires a combination of knowledge, experience, and skill that programmers usually obtain only after years of mistakes and discoveries. The purpose of this series is to share with both new and experienced programmers a few decades worth of observations and experience. I hope that these books will help shorten the time and reduce the frustration that it takes to learn things the hard way.
This first volume, Understanding the Machine , is intended to fill in the low-level details that are often skimmed over in a typical computer science or engineering curriculum. The information in this volume is the foundation upon which great software is built. You cannot write efficient code without this information, and the solutions to many problems require a thorough grounding in this subject. Though Im attempting to keep each volume as independent as possible of the others, Understanding the Machine might be considered a prerequisite for all the following volumes.
The second volume, Thinking Low-Level, Writing High-Level , will immediately apply the knowledge gained in this first volume. Thinking Low-Level, Writing High-Level will teach you how to analyze code written in a high-level language to determine the quality of the machine code that a compiler would generate for that code. Armed with this knowledge, you will be able to write high-level language programs that are nearly as efficient as programs handwritten in assembly language. High-level language programmers often get the mistaken impression that optimizing compilers will always generate the best machine code possible, regardless of the source code the programmer gives them. This simply isnt true. The statements and data structures you choose in your source files can have a big impact on the efficiency of the machine code a compiler generates. By teaching you how to analyze the machine code your compiler generates, Thinking Low-Level, Writing High-Level will teach you how to write efficient code without resorting to assembly language.
There are many other attributes of great code besides efficiency, and the third volume in this series, Engineering Software , will cover some of those. Engineering Software will discuss how to create source code that is easily read and maintained by other individuals and how to improve your productivity without burdening you with the busy work that many software engineering books discuss. Engineering Software will teach you how to write code that other programmers will be happy to work with, rather than code that causes them to use some choice words about your capabilities behind your back.
Great code works . Therefore, I would be remiss not to include a volume on testing, debugging, and quality assurance. Whether you view software testing with fear or with disgust, or you feel its something that only junior engineers should get stuck doing, an almost universal truth is that few programmers properly test their code. This generally isnt because programmers actually find testing boring or beneath them, but because they simply dont know how to test their programs, eradicate defects, and ensure the quality of their code. As a result, few applications receive high-quality testing, which has led the world at large to have a very low opinion of the software engineering profession. To help overcome this problem, the fourth volume in this series, Testing, Debugging, and Quality Assurance , will describe how to efficiently test your applications without all the drudgery engineers normally associate with this task.
1.2 What This Volume Covers
In order to write great code, you need to know how to write efficient code, and to write efficient code, you must understand how computer systems execute programs and how abstractions found in programming languages map to the low-level hardware capabilities of the machine. This first volume teaches you the details of the underlying machine so youll know how to write software that best uses the available hardware resources. While efficiency is not the only attribute great code possesses, inefficient code is never great. So if youre not writing efficient code, youre not writing great code.
In the past, learning great coding techniques has required learning assembly language. While this is not a bad approach, it is overkill. Learning assembly language involves learning two related subjects: (1) machine organization and (2) programming in assembly language. While learning assembly language programming helps, the real benefits of learning assembly language come from learning machine organization at the same time. Few books have taught machine organization without also teaching assembly language programming. To rectify this problem, this book teaches machine organization independently of assembly language so you can learn to write great code without the excessive overhead of learning assembly language.
So what is machine organization? youre probably wondering. Well, machine organization is a subset of computer architecture, and this book concentrates on those parts of computer architecture and machine organization that are visible to the programmer or are helpful for understanding why system architects chose a particular system design. The goal of learning machine organization is not to enable you to design your own CPU or computer system, but to teach you how to make the most efficient use of existing computer designs.