Abdu Taviq

Clean Code

YouTube Video

Summary

The Why

Messy code exists for many reasons, maybe you wanted to go fast or the promised features are late. This leads to a mess which can slow you on the long run.

Creating a new team to re-write the project again won’t solve it as both the new and current team will be in a race to keep the changes which may take forever. That’s why keeping the codebase clean is important for project survival.

Programmers should improve their sense to spot bad code and improve it.

Clean code should be easy to read and extend and help other developers to understand the code. It should be like a well-written prose.

Meaningful names

Functions

Comments

Formatting

Objects and Data Structure

Error Handling

Boundaries

Unit tests

Classes

Systems

// Continue here

Emergence

Simple design rules

Concurrency

Concurrency separates the what and when for code execution which increases the throughput of the application. It’s only needed for high-traffic apps otherwise it’s useless and maybe harmful. Writing clean concurrency is to avoid unpredictable outcomes due to many many code execution paths. Apply SRP, limit data sharing, independent threads. Another problem is that threads will compete on resources which require writing good algorithims to avoid deadlocks. Models of concurrency are Producer-Consumer, Readers-Writers, Dining Philosophers.

Make concurrent code as small as possible, write graceful shutdown code for child threads.

When testing concurrent code, it’s complex so try testing for failure and make it more configurable to change order, setting, environment and treat weird failures as a potential bug not a mistake.

Don’t ignore system failures as one-offs, the more it’s used the high count it will happens, it’s basic probablility. Check first the code is running normally before trying threading. Test with more threads that processors to increase switching and find bugs easier. Different OS run the code differently. Use jiggling strategies to find weird or unexpected behaviour due to multi-threading.

Client routines shouldn’t manage shared data/state

Successive Refinement

Writing the code that just works is bad, there should multiple successive refinement to avoid it being rotten. (skipped this chapter)

Smells and Heuristics

This chapter contains many things to do or avoid and I believe should be read all of it. Some ideas are: