Skip to content

Commit

Permalink
Modify rule S1155: Adopt standard library method names
Browse files Browse the repository at this point in the history
Co-authored-by: Marco Borgeaud <[email protected]>
Co-authored-by: Michael Jabbour <[email protected]>
  • Loading branch information
3 people authored Nov 1, 2024
1 parent 56018c3 commit 419edfb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rules/S1155/cfamily/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "\"empty()\" or \"is_empty()\" should be used to test for emptiness",
"title": "\"empty()\" should be used to test for emptiness",
"tags": [
"cppcoreguidelines",
"clumsy"
Expand Down
8 changes: 4 additions & 4 deletions rules/S1155/cfamily/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
== Why is this an issue?

When you call `empty()` or `is_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.
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, depending on the implementation, the `size()`, `length()`, or `count()` methods can have a time complexity of `O(n)` where `n` is the number of elements in the collection. On the other hand, `empty()` and `is_empty()` simply check if there is at least one element in the collection, which is a constant time operation, `O(1)`.
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]
----
Expand All @@ -13,7 +13,7 @@ void fun(const std::vector<int> &myVector) {
}
----

Prefer using `empty()` or `is_empty()` to test for emptiness over `size()`, `length()`, or `count()`.
Prefer using `empty()` or to test for emptiness over `size()`.

[source,cpp,diff-id=1,diff-type=compliant]
----
Expand All @@ -38,7 +38,7 @@ ifdef::env-github,rspecator-view[]

=== Message

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

'''
== Comments And Links
Expand Down

0 comments on commit 419edfb

Please sign in to comment.