Code Smell 04 – Stop Abusing Strings—Use Real Objects Instead

Use real abstractions and real objects instead of accidental string manipulation.


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Too much parsing, exploding, regex, strcmp, strpos and string manipulation functions.

TL;DR: Use real abstractions and real objects instead of accidental string manipulation.

Problems 😔

  • Complexity
  • Readability
  • Maintainability
  • Lack of abstractions
  • Fragile logic
  • Hidden intent
  • Hard debugging
  • Poor modeling
  • Regex mess

Solutions 😃

  1. Work with objects instead.
  2. Replace strings with data structures dealing with object relations.
  3. Go back to Perl :)
  4. identify bijection problems between real objects and the strings.

Examples

  • Serializers
  • Parsers

Context 💬

When you abuse strings, you try to represent structured concepts with plain text.

You parse, explode, and regex your way around instead of modeling the domain.

This creates fragile code that breaks with small input changes.

Sample Code 📖

Wrong 🚫

<?php

$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;
    }
}

Detection 🔍

  • Semi-Automatic

Automated detection is not easy.

If your code uses too many string functions, linters can trigger a warning.

Tags 🏷️

  • Primitive Obsession

Level 🔋

  • Beginner

Why the Bijection Is Important 🗺️

You must mirror the real-world domain in your code.

When you flatten roles, addresses, or money into raw strings, you lose control.

This mismatch leads to errors, duplication, and weak models.

One-to-one mapping between domain and code gives you clarity and robustness.

AI Generation 🤖

AI generators often produce string-abusing code because it looks shorter and easier.

The generated solution can be correct for toy cases but fragile in real systems.

AI Detection 🧲

You can instruct AI tools to replace string checks with domain objects.

With clear prompts, AI can spot and fix string abuse effectively.

Try Them! 🛠

Remember: AI Assistants make lots of mistakes

Suggested Prompt: Convert it to more declarative

| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | You | You | | Gemini | Gemini | | DeepSeek | DeepSeek | | Meta AI | Meta AI | | Grok | Grok | | Qwen | Qwen |

Conclusion 🏁

Don't abuse strings.

Favor real objects.

Add missing protocol to distinguish them from raw strings.

Relations 👩‍❤️‍💋‍👨

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv

https://maximilianocontieri.com/code-smell-295-string-concatenation?embedable=true

More Information 📕

Credits 🙏

Photo by Nathaniel Shuman on Unsplash


This article is part of the CodeSmell Series.

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd?embedable=true

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2025-08-28T06:27:59+00:00) Code Smell 04 – Stop Abusing Strings—Use Real Objects Instead. Retrieved from https://www.scien.cx/2025/08/28/code-smell-04-stop-abusing-strings-use-real-objects-instead/

MLA
" » Code Smell 04 – Stop Abusing Strings—Use Real Objects Instead." Maximiliano Contieri | Sciencx - Thursday August 28, 2025, https://www.scien.cx/2025/08/28/code-smell-04-stop-abusing-strings-use-real-objects-instead/
HARVARD
Maximiliano Contieri | Sciencx Thursday August 28, 2025 » Code Smell 04 – Stop Abusing Strings—Use Real Objects Instead., viewed ,<https://www.scien.cx/2025/08/28/code-smell-04-stop-abusing-strings-use-real-objects-instead/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 04 – Stop Abusing Strings—Use Real Objects Instead. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/28/code-smell-04-stop-abusing-strings-use-real-objects-instead/
CHICAGO
" » Code Smell 04 – Stop Abusing Strings—Use Real Objects Instead." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2025/08/28/code-smell-04-stop-abusing-strings-use-real-objects-instead/
IEEE
" » Code Smell 04 – Stop Abusing Strings—Use Real Objects Instead." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2025/08/28/code-smell-04-stop-abusing-strings-use-real-objects-instead/. [Accessed: ]
rf:citation
» Code Smell 04 – Stop Abusing Strings—Use Real Objects Instead | Maximiliano Contieri | Sciencx | https://www.scien.cx/2025/08/28/code-smell-04-stop-abusing-strings-use-real-objects-instead/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.