Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EC92] [C#] Use string Length #342

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [C# #67](https://github.com/green-code-initiative/ecoCode-csharp/pull/54) [EC92] [C#] Use string.Length instead of comparison with empty string
Djoums marked this conversation as resolved.
Show resolved Hide resolved

### Added

- [C# #66](https://github.com/green-code-initiative/ecoCode-csharp/pull/66) [EC93] [C#] Return Task directly

### Changed
Expand Down
5 changes: 3 additions & 2 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ 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 | | ❓ | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ |
| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | ✅ | ❓ |
| EC89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited datas
Djoums marked this conversation as resolved.
Show resolved Hide resolved
| 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 readable. | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | 🚫 |
| EC93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | | ❓ | ❓ | ❓ | ❓ | ❓ | ✅ | ❓ |
| EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | 🚀 | 🚫 |
| EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | 🚫 |
| | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚀 |
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 readable.
Djoums marked this conversation as resolved.
Show resolved Hide resolved

### 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");
}
}
----
Loading