简体   繁体   English

对象与 Static 保留变量 function state

[英]Objects vs. Static Variables for retaining function state

I have a function which processes data that comes as a sequence.我有一个 function 处理作为序列的数据。 Because of this, I need to know the value of certain variables from the last function call during the current function call.因此,我需要知道当前 function 调用期间最后一次 function 调用中某些变量的值。

My current approach to doing this is to use static variables.我目前的做法是使用 static 变量。 My function goes something like this:我的 function 是这样的:

bool processData(Object message){
    static int lastVar1 = -1;

    int curVar1 = message.var1;
    if (curVar1 > lastVar1){
        // Do something
    }

    lastVar1 = curVar1;
}

This is just a small sample of the code;这只是代码的一小部分; in reality I have 10+ static variables tracking different things.实际上,我有 10 多个 static 变量跟踪不同的事物。 My gut tells me using so many static variables probably isn't a good idea, though I have nothing to back that feeling up.我的直觉告诉我使用这么多 static 变量可能不是一个好主意,尽管我没有什么可以支持这种感觉。

My question: Is there a better way to do this?我的问题:有没有更好的方法来做到这一点?

An alternative I've been looking into is using an object whose fields are lastVar1, lastVar2, etc. However, I'm not sure if keeping an object in memory would be more efficient than using static variables. An alternative I've been looking into is using an object whose fields are lastVar1, lastVar2, etc. However, I'm not sure if keeping an object in memory would be more efficient than using static variables.

Your question has a taste of being purely about style and opinions, though there are aspects that are not a matter of opinion: multithreading and testing.您的问题有一种纯粹是关于风格和意见的味道,尽管有些方面不是意见问题:多线程和测试。

Consider this:考虑一下:

bool foo(int x) {
    static last_val = -1;
    bool result = (x == last_val);
    last_val = x;
    return result;
}

You can call this function concurrently from multiple threads but it wont do the expected.您可以从多个线程同时调用此 function 但它不会达到预期的效果。 Moreover you can only test the function by asserting that it does the right thing:此外,您只能通过断言它做正确的事情来测试 function:

   foo(1);
   assert( foo(1) );    // silenty assumes that the last call did the right thing

To setup the preconditions for the test (first line) you already have to assume that foo(1) does the right thing, which somehow defeats the purpose of testing that call in the second line.要为测试(第一行)设置先决条件,您已经必须假设foo(1)做了正确的事情,这在某种程度上违背了在第二行中测试调用的目的。

If the methods need the current object and the previous object, simply pass both:如果方法需要当前的 object 和以前的 object,只需通过两者:

bool processData(const Object& message,const Object& previous_message){

    if (message.var1 > previous_message.var1){
        // Do something
        return true;
    }   
    return false;
}

Of course this just shifts the issue of keeping track of the previous message to the caller, though thats straight-forward and requires not messing around with statics:当然,这只是将跟踪上一条消息的问题转移给了调用者,尽管那是直截了当的并且不需要搞乱静态:

 Object message, old_message;
 while ( get_more( message )) {
     processData(message, old_message);
     old_message = message;
 }

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

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