dennisdoomen / CSharpGuidelines

A set of coding guidelines for C# 9.0, design principles and layout rules for improving the overall quality of your code development.
https://www.csharpcodingguidelines.com
Other
746 stars 271 forks source link

Prefer language syntax over explicit calls to underlying implementations #138

Closed bkoelman closed 6 years ago

bkoelman commented 6 years ago

How about this for a new rule? AV1526 or AV2201: Prefer language syntax over explicit calls to underlying implementations


Prefer:

    (string, int) tuple = ("", 1);

rather than:

    ValueTuple<string, int> tuple = new ValueTuple<string, int>("", 1);

Prefer:

    DateTime? startDate;

rather than:

    Nullable<DateTime> startDate;

Prefer:

    if (startDate != null) ...

rather than:

    if (startDate.HasValue) ...

Prefer:

    if (startDate > DateTime.Now) ...

rather than:

    if (startDate.HasValue && startDate.Value > DateTime.Now) ...
dennisdoomen commented 6 years ago

Sure. I think the Framework Guidelines are the best fit.

Would be nice if we can also remove one...

bkoelman commented 6 years ago

Another one, which is new in C# 7.3 (released recently):

Prefer:

(DateTime StartTime, TimeSpan Duration) tuple1 = GetTimeRange();
(DateTime StartTime, TimeSpan Duration) tuple2 = GetTimeRange();

if (tuple1 == tuple2) ...

rather than:

if (tuple1.StartTime == tuple2.StartTime && tuple1.Duration == tuple2.Duration) ...

Would be nice if we can also remove one...

How about merging these with AV2201?

If I had to pick one to remove, it would be AV2215. AssemblyInfo is not so relevant anymore since we have NuGet. Assembly version info is usually auto-filled by cibuild tools these days. And AssemblyInfo is auto-generated when using the new project file format (PackageVersion, PackageAuthor etc).

Another candidate to remove could be AV2205:

  • Use Pascal casing in resource keys.

Can be moved to/near the casing table of AV1702

  • Provide descriptive identifiers rather than short ones. Keep them concise where possible, but don't sacrifice readability.
  • Use only alphanumeric characters in naming resources.

I haven't been working with localized applications in the last years, but these apply to most identifiers I would say.

dennisdoomen commented 6 years ago

How about merging these with AV2201?

That one is only about type aliases, so it needs something else.

If I had to pick one to remove, it would be AV2215.

Agreed

Another candidate to remove could be AV2205:

Agreed