cover-img

Code Smell 45 - Not Polymorphic

Methods should be interchangeable if they do the same.

24 January, 2023

4

4

0

Problems

  • Missed Polymorphism
  • Coupling
  • IFs / Type check Polluting.
  • Names coupled to types.

Solutions

  1. Rename methods after what they do.
  1. Favor polymorphism.

Sample Code

Wrong

<?

class array {
public function arraySort() {
}
}

class list {
public function listSort() {
}
}

class Set {
public function setSort() {
}
}

Right

<?

class Array {
public function sort() {
}
}

class List {
public function sort() {
}
}

class Set {
public function sort() {
}
}

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

Code Smell 36 - Switch/case/elseif/else/if statements

More Info

What exactly is a name - Part II Rehab

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

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

4

4

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.