Code Smell 04 - String Abusers

Code Smell 04 - String Abusers

Maxi Contieri
·Oct 23, 2020·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

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


1) Work with objects instead.

2) Replace strings with data structures dealing with object relations.

3) Go back to Perl :)

4) Find Bijection problems between real objects and the strings.




Sample Code



$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.


  • Primitive Obsession

More info


  • 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.

Last update: 2021/06/03

Share this


Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)