DEV Community

Discussion on: A beginner's guide to writing cleaner code

Collapse
 
eljayadobe profile image
Eljay-Adobe • Edited

Clean Code is well isolated. Which means that the code exhibits high cohesion and low coupling.

If the code is unit tested, it means the code must be isolated to be unit testable.

Having unit tested code squelches spooky action at a distance behavior.

Having unit tested code also suppresses emergent behavior. And acts as developer pressure to abide by SOLID principles.

Having unit tested code also means the code can be refactored aggressively and with confidence.

Code that is not isolated, is not clean. It is hard to reason about. It is hard to change. It is easy to break (i.e., introduce bugs) when changing. Code that is not isolated has highly intertwined coupling, which is why the code is hard to reason about, hard to change, and easy to break.

The enormous value of unit tests is that they validate the code is isolated.

Note: DRY is great for code, not so good for unit tests. Unit tests should follow the Write Expressive Tests (WET) principle; they should be independent from one another, avoid common setup/teardown, no loops or branches, and follow the arrange-act-assert pattern.

Collapse
 
morgenstern2573 profile image
Paul Akinyemi

Thank you for the detailed addition!
I agree, unit testing is very good practice