Chapter 12. Turning Thoughts into Code
You do not really understand something unless you can explain it to your grandmother.
âAlbert Einstein
When explaining a complex idea to someone, itâs easy to confuse them with all the little details. Itâs a valuable skill to be able to explain an idea âin plain English,â so that someone less knowledgeable than you can understand. It requires distilling an idea down to the most important concepts. Doing this not only helps the other person understand but also helps you think about your own ideas more clearly.
The same skill should be used when âpresentingâ code to your reader. We take the view that source code is the primary way to explain what a program is doing. So the code should be written âin plain English.â
In this chapter, weâll use a simple process that can help you code more clearly:
Describe what code needs to do, in plain English, as you would to a colleague.
Pay attention to the key words and phrases used in this description.
Write your code to match this description.
Describing Logic Clearly
Here is a snippet of code from a web page in PHP. This code is at the top of a secured page. It checks whether the user is authorized to see the page, and if not, immediately returns a page telling the user she is not authorized:
$is_admin = is_admin_request(); if ($document) { if (!$is_admin && ($document['username'] != $_SESSION['username'])) ...
Get The Art of Readable Code now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.