Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SOLID principles, and hard code configuration inside a class

I have noticed in a lot of code lately that people put hard coded configuration (like port numbers, etc.) values deep inside of classes/methods, making it difficult to find, and also not configurable.

Is this a violation of the SOLID principles? If not, is there another "principle" that I can cite to my team members about why it's not a good idea? I don't want to just say "it's bad because I don't like it" but I am having trouble thinking of a good argument.

like image 459
skb Avatar asked Oct 22 '22 10:10

skb


1 Answers

A good argument against hardcoding a TCP port number in a class would be 'Context independence' violation. From GOOS, with my emphasis:

Context Independence

... the "context independence" rule helps us decide whether an object hides too much or hides the wrong information. A system is easier to change if its objects are context-independent; that is, if each object has no built-in knowledge about the system in which it executes. This allows us to take units of behavior (objects) and apply them in new situations. To be context-independent, whatever an object needs to know about the larger environment it’s running in must be passed in.

In this specific case of Context Independence I would call it 'Environment Independence'. In other words a class with hardcoded port number has inappropriate dependency on a runtime OS environment, essentially stating 'I know that port 7778 will always be available' which is clearly wrong.

like image 129
Dmitry Avatar answered Nov 18 '22 10:11

Dmitry