DEV Community

Cover image for Code Smell 45 - Not Polymorphic
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

1

Code Smell 45 - Not Polymorphic

Methods should be interchangeable if they do the same.

Problems

  • Missed Polymorphism

  • Coupling

  • IFs / Type check Polluting

  • Names coupled to types.

Solutions

  1. Rename methods after what they do.

  2. Favor polymorphism.

Sample Code

Wrong

<?
class array {
public function arraySort() {
}
}
class list {
public function listSort() {
}
}
class Stack {
public function stackSort() {
}
}

Right

<?
interface Sortable {
public function sort();
}
class Array implements Sortable {
public function sort() {
// Implementation of the sort() method for Array
}
}
class List implements Sortable {
public function sort() {
// Implementation of the sort() method for List
}
}
class Stack implements Sortable {
public function sort() {
// Implementation of the sort() method for Stack
}
}
view raw polymorphic.php hosted with ❤ by GitHub

Detection

This is a semantic mistake. We could add a warning for similar method names on Polymorphic classes.

Tags

  • Polymorphic

Conclusion

Naming is very important. We need to name after concepts and not after accidental types,

Relations

More info

what is in a name


If you have three pet dogs, give them names. If you have 10,000 head of cattle, don't bother. Nowadays, the idea of giving a name to every file on your computer is ridiculous.

David Gelernter

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

Top comments (0)

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay