简体   繁体   English

可以将SOLID的S扩展到代码的每个元素吗?

[英]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: 著名的面向对象程序设计的S代表:

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. 并且我们使用它来存储函数的结果,但是以某种方式我们使用相同的A [100]来检查例如已经检查和阐述的A的哪些索引。 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. PS:很抱歉,我的问题无法理解,但英语不是我的主要语言。 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. 如果在不同的程序代码部分中使用相同的A实例,则必须遵循此原则。 If A is a auxiliary variable, local one for example, I think you don't need to be care about it. 如果A是一个辅助变量,例如局部变量,那么我认为您不需要关心它。

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. 您又要承担单一责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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