From e46e70709918ad816544461e8cfb76dc9088c75f Mon Sep 17 00:00:00 2001 From: bkoelman Date: Sun, 6 May 2018 20:21:35 +0200 Subject: [PATCH] Added AV2202 --- _pages/2200_FrameworkGuidelines.md | 46 ++++++++++++++++++++++++++++++ _pages/Cheatsheet.md | 1 + 2 files changed, 47 insertions(+) diff --git a/_pages/2200_FrameworkGuidelines.md b/_pages/2200_FrameworkGuidelines.md index 55713c0..8e5e447 100644 --- a/_pages/2200_FrameworkGuidelines.md +++ b/_pages/2200_FrameworkGuidelines.md @@ -11,6 +11,52 @@ For instance, use `object` instead of `Object`, `string` instead of `String`, an **Exception:** When referring to static members of those types, it is custom to use the full CLS name, e.g. `Int32.Parse()` instead of `int.Parse()`. The same applies to members that need to specify the type they return, e.g. `ReadInt32`, `GetUInt16`. +### Prefer language syntax over explicit calls to underlying implementations (AV2202) ![](/assets/images/1.png) +Language syntax makes code more concise. The abstractions make later refactorings easier (and sometimes allow for extra optimizations). + +Prefer: + + (string, int) tuple = ("", 1); + +rather than: + + ValueTuple tuple = new ValueTuple("", 1); + +Prefer: + + DateTime? startDate; + +rather than: + + Nullable startDate; + +Prefer: + + if (startDate != null) ... + +rather than: + + if (startDate.HasValue) ... + +Prefer: + + if (startDate > DateTime.Now) ... + +rather than: + + if (startDate.HasValue && startDate.Value > DateTime.Now) ... + +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) ... + ### Don't hard-code strings that change based on the deployment (AV2207) ![](/assets/images/3.png) Examples include connection strings, server addresses, etc. Use `Resources`, the `ConnectionStrings` property of the `ConfigurationManager` class, or the `Settings` class generated by Visual Studio. Maintain the actual values into the `app.config` or `web.config` (and most definitely not in a custom configuration store). diff --git a/_pages/Cheatsheet.md b/_pages/Cheatsheet.md index 38b1907..9f6ce2e 100644 --- a/_pages/Cheatsheet.md +++ b/_pages/Cheatsheet.md @@ -98,6 +98,7 @@ NOTE: Requires Markdown Extra. See http://michelf.ca/projects/php-markdown/extra **Framework Guidelines** * Use C# type aliases instead of the types from the `System` namespace (AV2201) +* Prefer language syntax over explicit calls to underlying implementations (AV2202) * Build with the highest warning level (AV2210) * Use Lambda expressions instead of anonymous functions (AV2221) * Only use the `dynamic` keyword when talking to a dynamic object (AV2230)