Let's talk about Clean Code

You usually write code. This act implies somebody will read it later. The reader can be your pair-programming partner, your teammate, your boss, any open source contributors, etc. Even if your code is read only by the computer, you might have to build on it later so that you will be the reader. Even if you don’t plan to read it in the future, your incentive should be to write clean code which is not only pleasant to read but also simple to build upon.

“Programs are meant to be read by humans and only incidentally for computers to execute.” - Donald Knuth

Clean code basics

The following link contains a presentation to introduce you the concept of clean code:

Introduction to clean code in ProgBasics

This table contains the most basic clean code issues providing easily recognizable symptoms to every problem. When you write or refactor (rewrite, optimize, etc.) code, pay attention to these and act accordingly.

Issue Symptom Symptom Symptom
Bad naming Non-English identifiers,
One letter variable names
Not following the name formatting convention (e.g., camel-case or other word separation instead of underscores in Python) Deceptive variable / method name (e.g., variable numbers contains names)
Badly formatted code Multiple statements in one line Unnecessary blank lines Wrong indentation
Repeated code Code block or line is repeated when it could be organized into functions Not using parameters where it could reduce repeating logic in multiple functions
Long method Long method block (> 30 lines) Long parameter list (>3)
Wrong comment usage Uncommented code block
(where comment could clear up the functionality of a code block)
Useless comment No comment at all
Dead code Contains unused code block or line Never reachable code lines (e.g., through a bad condition)

The ProgBasics module focuses on the first 3 issues but you should be aware of the other ones as well if you want to write more clean code.

Clean code advanced

All of the clean code aspects have been published as a book, which has become the definitive guide of clean code. This book contains more materials on clean code that you’ll ever learn in Codecool but remember to grab it from time to time, you’ll find other parts useful as you advance in programming :)

clean-code-book.jpg

Martin, Robert C. (2009)
Clean Code: A Handbook of Agile Software Craftsmanship
Upper Saddle River, NJ: Prentice Hall
ISBN 9780132350884

Read it via:

Most of the covered topics of the book are available as a video series: https://cleancoders.com/videos However, we find these a bit pricey. You can get the information contained in a YouTube search from Uncle Bob’s university lectures and conference talks for free: https://www.youtube.com/results?search_query=uncle+bob