DEV Community

Cover image for Code Smell 228 - Multiple Classes per File
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

1

Code Smell 228 - Multiple Classes per File

More than one class is a mess

TL;DR: Follow the separation of concerns principle and file organization

Problems

  • Code Organization

  • Coupling

  • Autoloading problems

  • Debugging

  • Version control and merge conflicts

Solutions

  1. Declare a single class per file

  2. Use name scoping

Context

In languages that declare classes using a file system, having one class per file is generally considered a best practice.

This approach helps improve code organization and maintainability and reduces potential issues.

You can organize namespaces into separate directories within your project structure.

This way, you can maintain a logical and efficient codebase while avoiding the issues of declaring multiple classes in a single file.

Sample Code

Wrong

<?

namespace MyNamespace;

class Class1 {
    public function sayHello() {
        echo "Hello from Class1!\n";
    }
}

class Class2 {
    public function sayHello() {
        echo "Hello from Class2!\n";
    }
}
Enter fullscreen mode Exit fullscreen mode

Right

<?

namespace MyNamespace;

class Class1 {
    public function sayHello() {
        echo "Hello from Class1!\n";
    }
}
Enter fullscreen mode Exit fullscreen mode
<?

namespace MyNamespace;

class Class2 {
    public function sayHello() {
        echo "Hello from Class2!\n";
    }
}
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Automatic

Many standards enforce this rule

Tags

  • Coupling

Conclusion

Keep your code organized and follow known standards

Relations

More Info

Wikipedia Namespaces

Sonar Source

Disclaimer

Code Smells are my opinion.

Credits

Photo by Marjan Blan on Unsplash


Without requirements or design, programming is the art of adding bugs to an empty text file.

Louis Srygley


This article is part of the CodeSmell Series.

Effortless, secure sharing for even your largest files.

Effortless, secure sharing for even your largest files.

No centralized keys. Post-quantum, end-to-end encryption means no one, not even VIA, can access your data.

To celebrate our launch and Cybersecurity Awareness Month, we’re giving you 1 TB of FREE data transfers when you sign up in October.

See more 🎥

Top comments (0)

Frontend, backend — everything in Java.

Frontend, backend — everything in Java.

Build production-ready web applications without juggling multiple languages. Vaadin is an open-source, secure web application framework for Java developers.

Learn More

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay