The Art of Clean Code
Best Practices to Eliminate Complexity and Simplify Your Life
by Christian Mayer
THE ART OF CLEAN CODE. Copyright 2022 by Christian Mayer.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
First printing
26 25 24 23 22 1 2 3 4 5 6 7 8 9
ISBN-13: 978-1-7185-0218-5 (print)
ISBN-13: 978-1-7185-0219-2 (ebook)
Publisher: William Pollock
Production Manager: Rachel Monaghan
Production Editor: Katrina Taylor
Developmental Editor: Liz Chadwick
Cover Illustrator: Gina Redman
Interior Design: Octopod Studios
Technical Reviewer: Noah Spahn
Copyeditor: Sadie Barry
Compositor: Jeff Lytle, Happenstance Type-O-Rama
Proofreader: Paula L. Fleming, Fleming Editorial Services
Indexer: BIM Creatives, Inc.
For information on distribution, bulk sales, corporate sales, or translations, please contact No Starch Press, Inc. directly at or:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900
www.nostarch.com
Library of Congress Control Number: 2022932408
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To my kids, Amalie and Gabriel
About the Author
Christian Mayer is the founder of the popular Python site Finxter.com, an educational platform that helps teach more than five million people a year how to program. He has a PhD in computer science and has published a number of books, including Python One-Liners (No Starch Press, 2020), Leaving the Rat Race with Python (2021), and the Coffee Break Python series.
About the Technical Reviewer
Noah Spahn has a wide-ranging background in software engineering. He has a masters degree in software engineering from California State University, Fullerton, and currently works in the Computer Security Group at the University of California, Santa Barbara (UCSB). Noah has taught Python courses at the UCSB Interdisciplinary Collaboratory and an upper division course on the Concepts of Programming Languages at Westmont College. Noah is glad to teach anyone who is interested in learning.
Foreword
I remember how excited I was when I learned my first line of Python code; it was like I had just entered a whole new universe of magical powers. As time passed, I learned to manipulate Python variables, lists, and dictionaries. Then I learned to write Python functions and enthusiastically started writing more complex Python code. But it didnt take me long to realize that writing code did not make me a skillful programmerit was as if I had just learned a few magic tricks but was very far from becoming a programming wizard.
Although it got the job done, my code was dreadful: repetitive and hard to read. When Chris told me about this book, I thought to myself I wish I had this book when I first started coding. There are plenty of books that teach you the technicalities of coding, but books like The Art of Clean Code are rare. This book will show you how to apply nine principles to improve your programming competence. And good programming skills lead to cleaner code, greater focus, more effective use of your time, and higher quality results.
Reading about How Complexity Harms Your Productivity ( Chapter 1 ) would have been very helpful to me as I was learning Python and data visualization, because I would have realized early on that I could build powerful dashboards with less code thats easier to read. When I was first starting, as I learned more Python functions and operations, I just wanted to use all these new magical tricks to build formidable data visualizations. But then I learned to build them with cleaner code instead of just using new tricks, and debugging my code became infinitely simpler and faster.
The state of flow and the Unix philosophy, covered in Chapters 6 and 7 , were two additional principles I wish I had known years ago. Multitasking is a capability that our culture tends to see as a desirable skill. I was often proud of my ability to code while paying attention to my email and phone. It took me a while to realize how impactful it could be to shut down distractions and dedicate my attention solely to the piece of code at hand. A few months later, I started blocking out time on my calendar to focus on coding. Not only did I end up writing better code with fewer errors, but I also got much more enjoyment out of the process.
By applying the principles described in this book, you shorten the path to becoming a skillful programmer. In fact, I have had the opportunity to witness firsthand the benefits of applying the books principles: Chriss code is clean, his writing is compelling, and he is prolific. I consider myself fortunate to have worked with Chris and to have seen how he embodies the principles that he emphasizes in this book.
Knowing how to code well requires curiosity and practice. However, there is a difference between a good coder and a good programmerthis book will help you become a good programmer who is more focused, more productive, and more effective.
Adam Schroeder, Community Manager at Plotly
Co-author of Python Dash (No Starch Press, 2022)
Acknowledgments
Putting together a programming book builds on the ideas and contributions of a multitude of people. Rather than trying to list them all, I want to follow my own advice: less is more.
First and foremost, I want to thank you. Ive written this book for you to help you improve your coding skills and solve practical problems in the real world. For trusting me with your valuable time, Im grateful. My primary goal with this book is to make it worth your while by sharing tips and strategies to save you time and reduce your stress throughout your coding career.
My greatest source of motivation came from the active members of the Finxter community. Every day, I get encouraging messages from Finxter students that motivate me to keep producing content. As you read this book, I want to wholeheartedly welcome you to the Finxter community. Its a pleasure to have you here!
My deep gratitude goes to the No Starch Press team for making the book-writing process such an enlightening experience. I want to thank my editor, Liz Chadwick; its because of her outstanding lead that the book reached a level of clarity I wouldnt be able to pull off myself. Katrina Taylor carried the book from draft to publication with a rare talent for people management and textual understanding. Thanks for making the book real, Katrina! My technical reviewer, Noah Spahn, invested his excellent technical skills to debug my writing. Special thanks to No Starch Presss founder, Bill Pollock, for allowing me to contribute in a small waywith yet another book alongside
Next page