简体   繁体   English

同时优化和可测试性 - 如何将代码分解为更小的单元

[英]Optimization and testability at the same time - how to break up code into smaller units

I am trying to break up a long "main" program in order to be able to modify it, and also perhaps to unit-test it. 我试图打破一个漫长的“主要”程序,以便能够修改它,也可能对它进行单元测试。 It uses some huge data, so I hesitate: 它使用了一些巨大的数据,所以我犹豫了:

What is best: to have function calls, with possibly extremely large (memory-wise) data being passed, 什么是最好的:具有函数调用,可能传递极大(内存)数据,

(a) by value, or (a)按价值,或

(b) by reference (b)参考

(by extremely large, I mean maps and vectors of vectors of some structures and small classes... even images... that can be really large) (非常大,我指的是一些结构和小类的矢量的地图和矢量......甚至图像......可能非常大)

(c) Or to have private data that all the functions can access ? (c)或者拥有所有功能都可以访问的私人数据? That may also mean that main_processing() or something could have a vector of all of them, while some functions will only have an item... With the advantage of functions being testable. 这也可能意味着main_processing()或者其他东西可以有所有这些的向量,而某些函数只有一个项目...具有可测试的函数的优点。

My question though has to do with optimization, while I am trying to break this monster into baby monsters, I also do not want to run out of memory. 我的问题虽然与优化有关,但我试图将这个怪物打破成宝贝怪物,我也不想耗尽内存。

It is not very clear to me how many copies of data I am going to have, if I create local variables. 如果我创建局部变量,我不清楚我将拥有多少数据副本。

Could someone please explain ? 有人可以解释一下吗?

Edit: this is not a generic "how to break down a very large program into classes". 编辑:这不是一个通用的“如何将一个非常大的程序分解成类”。 This program is part of a large solution, that is already broken down into small entities. 该程序是大型解决方案的一部分,已经分解为小型实体。

The executable I am looking at, while fairly large, is a single entity, with non-divisible data. 我正在查看的可执行文件虽然相当大,但它是一个单独的实体,具有不可分割的数据。 So the data will either be all created as member variable in a single class, which I have already created, or it will (all of it) be passed around as argument around functions. 因此,数据将全部创建为单个类中的成员变量,我已经创建了它,或者它(所有这些)将作为函数的参数传递。

Which is better ? 哪个更好 ?

If you want unit testing, you cannot "have private data that all the functions can access" because then, all of that data would be a part of each test case. 如果您需要单元测试,则不能“拥有所有功能都可以访问的私有数据”,因为这样,所有这些数据都将成为每个测试用例的一部分。

So, you must think about each function, and define exactly on which part of the data it works. 因此,您必须考虑每个函数,并准确定义它工作的数据部分。 As for function parameters and return values, it's very simple: use pass-by-value for small objects, and pass-by-reference for large objects. 至于函数参数和返回值,它非常简单:对小对象使用pass-by-value,对大对象使用pass-by-reference。

You can use a guesstimate for the threshold that separates small and large. 您可以将猜测用于分隔小型和大型的阈值。 I use the rule "8 is small, anything more is large" but what is good for my system cannot be equally good for yours. 我使用规则“8很小,任何更大”但对我的系统有益的是对你的同样好。

This seems more like a general question about OOP. 这似乎更像是关于OOP的一般性问题。 Split up your data into logically grouped concepts (classes), and place the code that works with those data elements with the data (member functions), then tie it all together with composition, inheritance, etc. 将数据拆分为逻辑分组的概念(类),并将与这些数据元素一起使用的代码与数据(成员函数)放在一起,然后将它们与组合,继承等结合在一起。

Your question is too broad to give more specific advice. 您的问题太广泛,无法提供更具体的建议。

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

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