Snippet Why Prefer empty() Over size() == 0 in Flyff Code?

Xenus

Premium
Premium
Joined
Jun 15, 2024
Messages
63
Reaction score
26
Points
18
When you want to check if a container is empty, it's better to use
Hey There!
Please login and(or) register to see this awesome content today.
instead of
Hey There!
Please login and(or) register to see this awesome content today.
. It makes your code clearer and directly shows what you're trying to do: check if there are any elements in the container. Plus, empty() can be a bit faster since it just checks for presence instead of counting all the elements. Basically, it's a good practice to adopt; it keeps your code clean and shows you know your way around C++.


We can see in : Housing.cpp
Hey There!
Please login and(or) register to see this awesome content today.

We can replace that by :

Hey There!
Please login and(or) register to see this awesome content today.

Explanation of Changes

In the modified version of SetRemoveVisitable, we replaced the condition
Hey There!
Please login and(or) register to see this awesome content today.
with
Hey There!
Please login and(or) register to see this awesome content today.
. Here’s why this change is beneficial:

  1. Clarity: Using empty() makes it clear that we are checking for the absence of elements in the container. It's immediately obvious what the intention is, which improves the readability of the code.
  2. Performance: Although the performance difference might be minimal, empty() can be slightly faster than size() == 0 since it directly checks if there are any elements without needing to count them.
  3. Best Practices: This change adheres to common C++ best practices. It indicates that the developer is mindful of writing efficient and maintainable code.
By making this simple change, we enhance the overall quality of our code in the SetRemoveVisitable function.
 
Last edited:
While I agree with you,I actually don't think .size() performs any count.
These dynamic arrays store their current size as as member variables, calling .size() simply returns that value.

I'm pretty sure .empty() does a "return .size() == 0"
 
  • Like
Reactions: Xenus
While I agree with you,I actually don't think .size() performs any count.
These dynamic arrays store their current size as as member variables, calling .size() simply returns that value.

I'm pretty sure .empty() does a "return .size() == 0"

.empty() return a bool (true / false) based on the container
.size() == 0 always check the length of the container and then compare it to 0

So .empty() is more explicit and can be slightly faster as most of the containers implement that function by checking an internal flag to know if the container is empty or not without checking for the length

While .size() == 0 CAN be slower on some container (like chained lists) because it need to goes through the whole container to count the elements.

Take not that .size() == 0 can be fast as .empty() if the container contains vectors because the length is directly stocked in.
 
  • Like
Reactions: Xenus
Actually STL containers with iterators performs a check to whether .begin() == .end() in their 'empty' method. (I don't know for chained list)
.size() returns the stored size, or just the size if it's a static array.

I'm not trying to convince anyone that .size() == is better than .empty(), I myself use empty over size()==. I'm just trying to point out the right reasons to use it, and performance is not of of them.
 
Slightly off topic subject here but has to do with Flyff and std;
How about std::map and std::unordred_map? I've seen countless uses of map without the real need for an "ordered" list on the code, what do you think about it?
P.S: map is slightly more taxing on performance than unordred_map
 
Yeah, I’ve noticed that in Flyff code too people often use std::map when there’s no need for ordering. If order isn’t important, switching to std::unordered_map makes more sense since it’s faster with O(1) operations. Flyff being a real-time game, these small optimizations could make a difference in performance. So yeah, if you don’t need sorted elements, std::unordered_map is definitely the way to go!