简体   繁体   中英

Could the S of S.O.L.I.D be extended for every single element of the code?

The S of the famous Object Oriented Programming design stands for:

Single responsibility principle, the notion that an object should have only a single responsibility.

I was wondering, can this principle, be extended even to arrays, variables, and all the elements of a program?

For example, let's say we have:

int A[100];

And we use it to store the result of a function, but somehow we use the same A[100] to check, for example, what indexes of A have we already checked and elaborated. Could this be considered wrong? Shouldn't we create another element to store, for example, the indexes that we have already checked? Isn't this an hint of future messy code?

PS: I'm sorry if my question is not comprehensible but English is not my primary language. If you have any problem understanding the point of it please let me know in a comment below.

If same A instance is used in different program code portions you must follow this principle. If A is a auxiliary variable, local one for example, I think you don't need to be care about it.

If you are tracking the use of bits of the array that have been updated, then you probably shouldn't be using an array, but a map instead.

In any case, if you need that sort of extra control over the array, then basically, you should be considering a class that contains both the contents of the array and the various information about what has and hasn't been done. So your array becomes local to the class object, as do your controls, and voila. You have single responsibility again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM