I have some data being loaded from a server, but there's no guarantee that I'll have it all when the UI starts to display it to the user. Every frame there's a tick function. When new data is received a flag is set so I know that it's time to load it into my data structure. Which of the following ways is a more sane way to decide when to actually run the function?
我从服务器上加载了一些数据,但是当UI开始向用户显示时,我无法保证能够拥有这些数据。每一帧都有一个滴答功能。收到新数据时会设置一个标志,所以我知道是时候将它加载到我的数据结构中了。以下哪种方式是更明智的方式来决定何时实际运行该功能?
AddNewStuffToList()
{
// Clear the list and reload it with new data
}
Foo_Tick()
{
if (updated)
AddNewStuffToList();
// Rest of tick function
}
Versus:
AddNewStuffToList()
{
if (updated)
{
// Clear the list and reload it with new data
}
}
Foo_Tick()
{
AddNewStuffToList();
// Rest of tick function
}
I've omitted a lot of the irrelevant details for the sake of the example.
为了这个例子,我省略了很多不相关的细节。
3 个解决方案
#1
IMHO first one. This version separates:
恕我直言第一个。此版本分为:
- when to update data (Foo_Tick)
何时更新数据(Foo_Tick)
FROM
- how to loading data (AddNewStuffToList()).
如何加载数据(AddNewStuffToList())。
2nd option just mixing all things together.
第二种选择只是混合所有的东西。
#2
You should probably not run the function until it is updated. That way, the function can be used for more purposes.
在更新之前,您可能不应该运行该功能。这样,该功能可用于更多目的。
Let's say you have 2 calls that both are going to come and put in data to the list. With the first set up, checking the variable inside of the function, you could only check if one call has came in. Instead, if you check it in the function that calls the data, you can have as many input sources as you want, without having to change the beginning function.
假设您有2个调用,它们都会将数据放入列表中。通过第一次设置,检查函数内部的变量,您只能检查是否有一个调用。相反,如果您在调用数据的函数中检查它,您可以拥有任意数量的输入源,无需更改开始功能。
Functions should be really precise on what they are doing, and should avoid needing information created by another function unless it is passed in.
函数应该非常精确地确定它们正在做什么,并且应该避免需要由另一个函数创建的信息,除非它被传入。
#3
In the first version the simple variable check "updated" will be checked each time and only if true would AddNewStuffToList be called.
在第一个版本中,每次都会检查简单变量检查“updated”,并且仅当为true时才会调用AddNewStuffToList。
With the second version you will call AddNewStuffToList followed by a check to "updated" every time.
使用第二个版本,您将调用AddNewStuffToList,然后每次都检查“更新”。
In this particular instance, given that function calls are generally expensive compared to a variable check I personally prefer the first version.
在这个特定的例子中,假设与变量检查相比,函数调用通常很昂贵,我个人更喜欢第一个版本。
However, there are situations when a check inside the function would be better. e.g.
但是,有些情况下功能内的检查会更好。例如
doSomething(Pointer *p){
p->doSomethingElse();
}
FooTick(){
Pointer *p = new Pointer();
// do stuff ...
// lets do something
if (p){
doSomething(p);
}
}
This is clumbsy because every time you call doSomething you should really check you're not passing in a bad pointer. What if this is forgotten? we could get an access violation. In this case, the following is better as you're only writing the check in one place and there is no extra overhead added because we always want to ensure we're not passing in a bad pointer.
这是不可能的,因为每次你调用doSomething时你都应该检查你是不是传递了一个糟糕的指针。如果忘记这会怎么样?我们可以获得访问冲突。在这种情况下,以下情况更好,因为您只在一个地方编写支票,并且没有额外的开销,因为我们总是希望确保我们没有传入错误的指针。
doSomething(Pointer *p){
if (p){
p->doSomethingElse();
}
}
So in general, it depends on the situation. There are no right and wrong answers, just pros and cons here.
所以一般来说,这取决于具体情况。这里没有正确和错误的答案,只有利弊。
#1
IMHO first one. This version separates:
恕我直言第一个。此版本分为:
- when to update data (Foo_Tick)
何时更新数据(Foo_Tick)
FROM
- how to loading data (AddNewStuffToList()).
如何加载数据(AddNewStuffToList())。
2nd option just mixing all things together.
第二种选择只是混合所有的东西。
#2
You should probably not run the function until it is updated. That way, the function can be used for more purposes.
在更新之前,您可能不应该运行该功能。这样,该功能可用于更多目的。
Let's say you have 2 calls that both are going to come and put in data to the list. With the first set up, checking the variable inside of the function, you could only check if one call has came in. Instead, if you check it in the function that calls the data, you can have as many input sources as you want, without having to change the beginning function.
假设您有2个调用,它们都会将数据放入列表中。通过第一次设置,检查函数内部的变量,您只能检查是否有一个调用。相反,如果您在调用数据的函数中检查它,您可以拥有任意数量的输入源,无需更改开始功能。
Functions should be really precise on what they are doing, and should avoid needing information created by another function unless it is passed in.
函数应该非常精确地确定它们正在做什么,并且应该避免需要由另一个函数创建的信息,除非它被传入。
#3
In the first version the simple variable check "updated" will be checked each time and only if true would AddNewStuffToList be called.
在第一个版本中,每次都会检查简单变量检查“updated”,并且仅当为true时才会调用AddNewStuffToList。
With the second version you will call AddNewStuffToList followed by a check to "updated" every time.
使用第二个版本,您将调用AddNewStuffToList,然后每次都检查“更新”。
In this particular instance, given that function calls are generally expensive compared to a variable check I personally prefer the first version.
在这个特定的例子中,假设与变量检查相比,函数调用通常很昂贵,我个人更喜欢第一个版本。
However, there are situations when a check inside the function would be better. e.g.
但是,有些情况下功能内的检查会更好。例如
doSomething(Pointer *p){
p->doSomethingElse();
}
FooTick(){
Pointer *p = new Pointer();
// do stuff ...
// lets do something
if (p){
doSomething(p);
}
}
This is clumbsy because every time you call doSomething you should really check you're not passing in a bad pointer. What if this is forgotten? we could get an access violation. In this case, the following is better as you're only writing the check in one place and there is no extra overhead added because we always want to ensure we're not passing in a bad pointer.
这是不可能的,因为每次你调用doSomething时你都应该检查你是不是传递了一个糟糕的指针。如果忘记这会怎么样?我们可以获得访问冲突。在这种情况下,以下情况更好,因为您只在一个地方编写支票,并且没有额外的开销,因为我们总是希望确保我们没有传入错误的指针。
doSomething(Pointer *p){
if (p){
p->doSomethingElse();
}
}
So in general, it depends on the situation. There are no right and wrong answers, just pros and cons here.
所以一般来说,这取决于具体情况。这里没有正确和错误的答案,只有利弊。