Skip to content
Open
Changes from all commits
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
23 changes: 18 additions & 5 deletions _rules/2202.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ rather than:

Prefer:

if (startDate != null) ...
if (startDate is null) ...

rather than:

if (startDate == null) ...

Prefer:

if (startDate is not null) ...
Comment on lines +27 to +35
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good addition, but it doesn't belong in this rule. This rule is about using language syntax instead of calling framework methods. Neither of these is a framework method. Also applies to the other addition down below.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the subtlety here? I don't want to suggest != null anymore and instead promote is not null

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm indifferent between the two styles, but it's good to choose one, so I'm fine with the addition. Just not in this rule, because that is about preferring language syntax over implementations (for example: int vs System.Int32). It doesn't belong in this rule because != and is not are both language syntax.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps an idea to put this in a new rule that prescibes when to prefer pattern syntax (and when not)?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'll do that.


rather than:

Expand All @@ -40,11 +48,16 @@ rather than:

Prefer:

(DateTime startTime, TimeSpan duration) tuple1 = GetTimeRange();
(DateTime startTime, TimeSpan duration) tuple2 = GetTimeRange();
List<string> items = [];

rather than:

List<string> items = new List<string>();

Prefer (C# 14):

if (tuple1 == tuple2) ...
list ??= [];

rather than:

if (tuple1.startTime == tuple2.startTime && tuple1.duration == tuple2.duration) ...
if (list == null) list = [];