Skip to content

Commit

Permalink
[EC92] [C#] Use string Length (#342)
Browse files Browse the repository at this point in the history
Co-authored-by: Baptiste HENRY <[email protected]>
Co-authored-by: Vianney de Bellabre <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2024
1 parent bfe52cc commit 52885f4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [C# #54](https://github.com/green-code-initiative/ecoCode-csharp/pull/54) [EC92] [C#] Use string.Length instead of comparison with empty string

### Changed

### Deleted
Expand Down
3 changes: 2 additions & 1 deletion RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ Some are applicable for different technologies.
| EC86 | GC.Collect should not be called | In most cases, the cost of calling GC.Collect far outweighs the benefits | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| EC87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas | ||||||||
| EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited data | ||||||||
| EC92 | Use string.Length instead of comparison with empty string | Comparing a string to an empty string is unnecessary and can be replaced by a call to `string.Length` which is more performant and more readable. | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | ||||||||
| EC94 | Use orElseGet instead of orElse | Parameter of orElse() is evaluated, even when having a non-empty Optional. Supplier method of orElseGet passed as an argument is only executed when an Optional value isn’t present. Therefore, using orElseGet() will save computing time. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚧 |||||||
| EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚀 | 🚀 | 🚀 || 🚀 | 🚀 | 🚫 |
Expand Down
16 changes: 16 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC92/EC92.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"title": "Use string.Length instead of comparison with empty string",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"eco-design",
"ecocode",
"performance",
"bad-practice"
],
"defaultSeverity": "Minor"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
:!sectids:

Use string.Length instead of comparison with empty string

## Why is this an issue ?

Comparing a string to an empty string is unnecessary and can be replaced by a call to `string.Length`, which is more performant and more readable.

### When can it be ignored ?

This rule should not be ignored.

## Non-compliant examples

[source, cs]
----
public void Main()
{
string s = "";
if (s == "") // Non-compliant
{
Console.WriteLine("Empty string");
}
}
----

## Compliant examples

[source, cs]
----
public void Main()
{
string s = "";
if (s.Length == 0) // Compliant
{
Console.WriteLine("Empty string");
}
}
----

0 comments on commit 52885f4

Please sign in to comment.