DEV Community

Cover image for Code Smell 60 - Global Classes
Maxi Contieri
Maxi Contieri

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

2

Code Smell 60 - Global Classes

Classes are handy. We can call them and invoke them any time. Is this good?

TL;DR: Don't use your classes as a global point of access.

Problems

  • Coupling

  • Classes are global unless we use Namespaces.

  • Name polluting

  • Static Methods

  • Static Constants

  • Singletons

Solutions

  1. Use namespaces, module qualifiers or similar

  2. Avoid namespace polluting, keep the Global names as short as possible.

  3. Class single Responsibility is to create instances.

Sample Code

Wrong

<?

final class StringUtilHelper {
    static function reformatYYYYDDMMtoYYYYMMDD($date) {
    }
}

class Singleton {

}

final class DatabaseAccessor extends Singleton {

}
Enter fullscreen mode Exit fullscreen mode

Right

<?

namespace Date;

final class DateFormatter {

    function reformatYYYYDDMMtoYYYYMMDD(Date $date) {
    }
    //function is not static since class single responsibility is to create instances and not be a library of utils

}

namespace OracleDatabase;

class DatabaseAccessor {
    //Database is not a singleton and it is namespace scoped
}
Enter fullscreen mode Exit fullscreen mode

Detection

We can use almost any linter or create dependency rules searching for bad class references.

Tags

  • Globals

Conclusion

We should restrict our classes to small domains and expose just facades to the outside. This greatly reduces coupling.

Relations

More info

Credits

Photo by Alfons Morales on Unsplash


Write shy code — modules that don't reveal anything unnecessary to other modules and that don't rely on other modules' implementations.

Dave Thomas



This article is part of the CodeSmell Series.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (3)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Namespaced values are just globals with a detour. If your Class is in a public namespace, it's a public class.

The only way to truly avoid these problems is a Lua-like system where there are no real globals and everything can be assigned to a name chosen by the user.

Collapse
 
mcsee profile image
Maxi Contieri

There are a lot of languages supporting scoped Namespaces

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Sadly, there's also many languages that Don't. Ruby comes to mind as an example.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay