An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
Code Smell 93 - Send me Anything
Magic functions that can receive a lot of different (and not polymorphic arguments)
TL;DR: Create a clear contract. Expect just one protocol.
Problems
Fail Fast principle violation
Error prune
Readability
Bad Cohesion
Solutions
Take just one "kind" of input
Arguments should adhere to a single protocol.
Sample Code
Wrong
<?function parseArguments($arguments) { $arguments = $arguments ?: null; //Always the billion-dollar mistake if (is_empty($arguments)) { $this->arguments = http_build_query($_REQUEST); //Global coupling and side effects } elseif (is_array($arguments)) { $this->arguments = http_build_query($arguments); } elseif (!$arguments) { //null unmasked $this->arguments = null; } else { $this->arguments = (string)$arguments; }}
Right
<?function parseArguments(array $arguments) { $this->arguments = $arguments; //much cleaner, isn't it ?}
Detection
We can detect this kind of methods when they do different things, asking for the argument kind
Tags
- If Polluter
Conclusion
Magic castings and flexibility have a price. They put the rubbish under the rug and violate fail fast principle.
Relations
Code Smell 69 - Big Bang (JavaScript Ridiculous Castings)
Maxi Contieri May 4 2 min read
Code Smell 06 - Too Clever Programmer
Maxi Contieri Oct 25 '20 2 min read
Credits
Photo by Hennie Stander on Unsplash
Referential transparency is a very desirable property: it implies that functions consistently yield the same results given the same input, irrespective of where and when they are invoked.
Edward Garson
Software Engineering Great Quotes
Maxi Contieri Dec 28 '20 13 min read
This article is part of the CodeSmell Series.
Original Link: https://dev.to/mcsee/code-smell-93-send-me-anything-16m3
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To