DEV Community

Cover image for Code Smell 92 - Isolated Subclasses Names
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Code Smell 92 - Isolated Subclasses Names

If your classes are globals, use fully qualified names

TL;DR: Don't use abbreviations in subclasses

Problems

  • Readability

  • Mistakes

Solutions

  1. Rename your classes to provide context

  2. Use modules, namespaces or fully qualified names

Sample Code

Wrong

abstract class PerserveranceDirection { 
}

class North extends PerserveranceDirection {}
class East extends PerserveranceDirection {}
class West extends PerserveranceDirection {}
class South extends PerserveranceDirection {}

//Subclasses have short names and meaningless outside the hierarchy
//If we reference East we might mistake it for the Cardinal Point
Enter fullscreen mode Exit fullscreen mode

Right

abstract class PerserveranceDirection { 
}

class PerserveranceDirectionNorth extends PerserveranceDirection {}
class PerserveranceDirectionEast extends PerserveranceDirection {}
class PerserveranceDirectionWest extends PerserveranceDirection {}
class PerserveranceDirectionSouth extends PerserveranceDirection {}

//Subclasses have fully quallified names
Enter fullscreen mode Exit fullscreen mode

Detection

Automatic detection is not an easy task. We could enforce local naming policies for subclasses.

Tags

  • Naming

Conclusion

Choose your names wisely.

If your language supports it, use modules, namespaces and local scopes.

Relations

More Info

Credits

Photo by Edvard Alexander Rølvaag on Unsplash


The programmer's primary weapon in the never-ending battle against slow system is to change the intramodular structure. Our first response should be to reorganize the modules' data structures.

Frederick P. Brooks


This article is part of the CodeSmell Series.

Discussion (0)