cover-img

Code Smell 19 - Optional Arguments

Disguised as a friendly shortcut is another coupling smell.

24 October, 2022

7

7

2

TL;DR: Optional Arguments generate a hidden coupling in the name of smaller code.

Problems

Coupling

Unexpected results

Side effects

Ripple Effect

In languages with optional arguments but limited to basic types, we need to set a flag and add an accidental IF (another smell).

Solutions

1.

Make arguments explicit.

2.

All function calls must have same arity.

3.

Use Named Parameters if your language supports them.

Sample Code

Wrong

Right

Detection

Detection is easy if language supports optional arguments.

Tags

Optional

Laziness

Conclusion

Be explicit. Favor readability over shorter (and more coupled) function call.

More info

The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.

Seymour Cray
This article is part of the CodeSmell Series.

7

7

2

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.