Too many parsing, exploding, regex, strcomp, strpos and string manipulation functions.
TL;DR: Use real abstractions and real objects instead of string accidental manipulation.
- Complexity
- Readability
- Maintainability
- Lack of Abstractions
-
Work with objects instead.
-
Replace strings with data structures dealing with object relations.
-
Go back to Perl :)
-
Find Bijection problems between real objects and the strings.
-Serializers
-Parsers
<?
$schoolDescription = 'College of Springfield';
preg_match('/[^ ]*$/', $schoolDescription, $results);
$location = $results[0]; // $location = 'Springfield'.
$school = preg_split('/[\s,]+/', $schoolDescription, 3)[0]; //'College'
<?
class School {
private $name;
private $location;
function description() {
return $this->name . ' of ' . $this->location->name;
}
}
Automated detection is not easy. If code uses too many string functions it can trigger a warning.
Code Smell 122 - Primitive Obsession
Code Smell 121 - String Validations
- Mapping
Don't abuse strings. Favor real objects. Find absent protocol to distinguish them from strings.
Photo by Nathaniel Shuman on Unsplash
This article is part of the CodeSmell Series.