Back to LinkedIn posts

LinkedIn post 212

πŸ’‘ Look at this great idea to better organize constants in your code:

πŸ’‘ Look at this great idea to better organize constants in your code:
a constant class with inner classes to separate constants by subject.

Seven months ago I had posted the thoughts below, which came close, but listed them as 2 separate options: a constant class versus separate inner classes in each class where they would be used.
πŸ™ Jannis Schreiber made the leap that brought a better solution. πŸ™

Sometimes we think a dichotomy exists but it is worth to ask "why not both?" (from the meme "ΒΏporque no los dos?").

πŸ‘πŸΌ CONSTANT CLASS PROS:
* All constants in one place can make it easier to manage and update them.
* Simplifies the search for constants across the application.
* A single class promotes modularity and separates constants from the logic.
* Keeps consistency across the application, reduces the risk of discrepancies.

πŸ‘ŽπŸΌ CONSTANT CLASS CONS:
* It can become cluttered.
* Violates the Single Responsibility Principle.
* Can make it difficult to navigate within the class and find specific constants quickly.
* Creates coupling between different parts of the code.
* Changes in this class might have wide impacts and bring dependencies issues.
* It is a premature optimization and may end up as wasted effort with constants not actually reused.

πŸ‘πŸΌ INNER Constant Class PROS:
* Can organize constants into cleaner logical groups
* Keeps related constants together and tied to their context
* Reduces clutter

πŸ‘ŽπŸΌ INNER Constant Class CONS:
* May add unnecessary complexity and overhead
* Makes constants harder to access, harder to be reused across multiple classes

https://www.linkedin.com/posts/fernandofernandez1_i-opt-to-use-a-separate-class-for-constants-activity-7214047704639844353-rG4u/

πŸ’‘ Look at this great idea to better organize constants in your code:
πŸ’‘ Look at this great idea to better organize constants in your code: