Discouraged by Object-Oriented Programmings, Many mixed languages support it. And developers abuse them.
TL;DR: Global function bring a lot of coupling. Don't use them.
Problems
Coupling
Readability
Maintainability
Testability
Solutions
- Wrap the function in a context object.
Examples
- External Resources Access, Database access, Time and Operating System resources.
Sample Code
Wrong
<?
class Employee {
function taxesPayedUntilToday() {
return database()->select(
"SELECT TAXES FROM EMPLOYEE".
" WHERE ID = " . $this->id() .
" AND DATE < " . currentDate());
}
}
Right
<?
final class EmployeeTaxesCalculator {
function taxesPayedUntilToday($context) {
return $context->SelectTaxesForEmployeeUntil(
$this->ssn,
$context->currentDate());
}
}
Detection
Many modern languages avoid them. For the permissive ones, scope rules can be applied and automatically checked.
Tags
- Global
Conclusion
Structured programming considers global functions harmful. Yet, we can observe some bad practices cross paradigm boundaries.
Relations
- Singleton and Classes are global points of access.
More Info
Credits
The road to programming hell is paved with global variables.
Steve McConnell
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21
Last update: 2021/06/26
Top comments (0)