== Why is this an issue?

When you call `empty()`, it clearly communicates the code's intention, which is to check if the collection is empty. Using `size() == 0` for this purpose is less direct and makes the code slightly more complex.

Moreover, in the standard library, depending on the implementation, the `size()` method can have a time complexity of `O(n)` where `n` is the number of elements in the collection. On the other hand, `empty()` simply checks if there is at least one element in the collection, which is a constant time operation, `O(1)`. Note that this rule also identifies similar method names in user-defined types, where the semantics and complexity may differ.

[source,cpp,diff-id=1,diff-type=noncompliant]
----
void fun(const std::vector<int> &myVector) {
  if (myVector.size() == 0) { // Noncompliant
    // do something
  }
}
----

Prefer using `empty()` or to test for emptiness over `size()`.

[source,cpp,diff-id=1,diff-type=compliant]
----
void fun(const std::vector<int> &myVector) {
  if (myVector.empty()) {
    // do something
  }
}
----

== Resources

=== External coding guidelines

* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#t143-dont-write-unintentionally-non-generic-code[T.143: Don't write unintentionally non-generic code]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Use empty() to check whether the container is empty or not.

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
