cover-img

Code Smell 15 - Missed Preconditions

If you turn off your assertions just in production your phone will ring at late hours.

15 October, 2022

0

0

0

Assertions, Preconditions, Postconditions, and invariants are our allies in avoiding invalid objects. Avoiding them leads to hard-to-find errors.

TD;DR: If you turn off your assertions just in production your phone will ring at late hours.

Problems

Consistency

Contract breaking

Hard to debug

Bad cohesion

Solutions

Create strong preconditions

Raise exceptions

Fail Fast

Defensive Programming

Examples

Constructors are an excellent first line of defense

Anemic Objects lack these rules.

Sample Code

Wrong

Right

Detection

It's difficult to find missing preconditions, as long with assertions and invariants.

Tags

Consistency

Conclusion

Always be explicit on object integrity.
Turn on production assertions.
Even if it brings performance penalties.
Data and object corruption is harder to find.
Fail fast is a blessing.

More info

Relations

Credits

Writing a class without its contract would be similar to producing an engineering component (electrical circuit, VLSI (Very Large Scale Integration) chip, bridge, engine…) without a spec. No professional engineer would even consider the idea.

Bertrand Meyer
This article is part of the CodeSmell Series.

programming

refactoring

cleancode

codesmell

0

0

0

programming

refactoring

cleancode

codesmell

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.