From 77ba50dff9737001e34f1e0d85f94ff0d1ad5a3a Mon Sep 17 00:00:00 2001 From: Tim Seaward Date: Tue, 14 Nov 2017 00:45:54 +0000 Subject: [PATCH] Adds coding standards (#22) * Adds code requirements * Update to coding standard --- .editorconfig | 94 +++++++++++++++++++ sample/StackTrace/Program.cs | 35 ++----- .../EnhancedStackTrace.Frames.cs | 18 ++-- .../Enumerable/EnumerableIList.cs | 7 +- src/Ben.Demystifier/Internal/ILReader.cs | 35 +++---- .../Internal/PortablePdbReader.cs | 5 +- test/Ben.Demystifier.Test/MixedStack.cs | 17 +--- .../NonThrownException.cs | 2 +- 8 files changed, 135 insertions(+), 78 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b6011aa --- /dev/null +++ b/.editorconfig @@ -0,0 +1,94 @@ +# ASP.NET Core EditorConfig file + +# NOTE: This file focuses on settings Visual Studio 2017 supports natively. For example, VS does not support insert_final_newline. +# We do use it, but it's harder to enforce without a separate VS extension or an editor that supports it. +# See https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options for more + +# Mark this file as the "root" for everything below this point. This means that editor config files above +# this file will be ignored +root = true + +# Default settings +[*] +indent_style = space +indent_size = 4 +charset = utf-8 +insert_final_newline = true + +# Unix-only files +[*.sh] +end_of_line = lf + +# 2-space files +[{*.json,*.yml}] +indent_size = 2 + +# .NET Code Style Settings +# See https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference +# REVIEW: Should these be errors? warnings? suggestions? +[{*.cs,*.vb}] +dotnet_sort_system_directives_first = true + +# Don't use 'this.'/'Me.' prefix for anything +dotnet_style_qualification_for_field = false:error +dotnet_style_qualification_for_property = false:error +dotnet_style_qualification_for_method = false:error +dotnet_style_qualification_for_event = false:error + +# Use language keywords over framework type names for type references +# i.e. prefer 'string' over 'String' +dotnet_style_predefined_type_for_locals_parameters_members = true:error +dotnet_style_predefined_type_for_member_access = true:error + +# Prefer object/collection initializers +# This is a suggestion because there are cases where this is necessary +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion + +# C# 7: Prefer using named tuple names over '.Item1', '.Item2', etc. +dotnet_style_explicit_tuple_names = true:error + +# Prefer using 'foo ?? bar' over 'foo != null ? foo : bar' +dotnet_style_coalesce_expression = true:error + +# Prefer using '?.' over ternary null checking where possible +dotnet_style_null_propagation = true:error + +# Use 'var' in all cases where it can be used +csharp_style_var_for_built_in_types = true:error +csharp_style_var_when_type_is_apparent = true:error +csharp_style_var_elsewhere = true:error + +# C# 7: Prefer using pattern matching over "if(x is T) { var t = (T)x; }" and "var t = x as T; if(t != null) { ... }" +# REVIEW: Since this is a new C# 7 feature that replaces an existing pattern, I'm making it a suggestion +csharp_style_pattern_matching_over_is_with_cast_check = true:warning +csharp_style_pattern_matching_over_as_with_null_check = true:warning + +# C# 7: Prefer using 'out var' where possible +# REVIEW: Since this is a new C# 7 feature that replaces an existing pattern, I'm making it a suggestion +csharp_style_inlined_variable_declaration = true:error + +# C# 7: Use throw expressions when null-checking +# @davidfowl hates them :) +csharp_style_throw_expression = false:error + +# Prefer using "func?.Invoke(args)" over "if(func != null) { func(args); }" +# REVIEW: Maybe an error? +csharp_style_conditional_delegate_call = true:error + +# Newline settings +# Unsure where docs are. Got these from https://github.com/dotnet/roslyn/blob/master/.editorconfig +csharp_new_line_before_open_brace = all +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true + +# Prefer expression-bodied methods, constructors, operators, etc. +csharp_style_expression_bodied_methods = true:suggestion +csharp_style_expression_bodied_constructors = true:suggestion +csharp_style_expression_bodied_operators = true:suggestion +csharp_style_expression_bodied_properties = true:suggestion +csharp_style_expression_bodied_indexers = true:suggestion +csharp_style_expression_bodied_accessors = true:suggestion \ No newline at end of file diff --git a/sample/StackTrace/Program.cs b/sample/StackTrace/Program.cs index 2eb42de..05f9df5 100644 --- a/sample/StackTrace/Program.cs +++ b/sample/StackTrace/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -38,10 +38,7 @@ class Program } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - Program(Action action) - { - RunAction((state) => _action((s) => action(), state), null); - } + Program(Action action) => RunAction((state) => _action((s) => action(), state), null); [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static IEnumerable Iterator(int startAt) @@ -75,31 +72,23 @@ class Program } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - static void RunAction(Action lambda, object state) - { - lambda(state); - } + static void RunAction(Action lambda, object state) => lambda(state); [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - static string RunLambda(Func lambda) - { - - return lambda(); - } + static string RunLambda(Func lambda) => lambda(); [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static (string val, bool) Method(string value) { +#pragma warning disable IDE0039 // Use local function Func func = () => MethodAsync(value).GetAwaiter().GetResult(); +#pragma warning restore IDE0039 // Use local function var anonType = new { func }; return (RunLambda(() => anonType.func()), true); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - static ref string RefMethod(int value) - { - return ref s; - } + static ref string RefMethod(int value) => ref s; [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static string RefMethod(in string value) @@ -137,16 +126,10 @@ class Program } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - static ref string RefMethod(bool value) - { - return ref s; - } + static ref string RefMethod(bool value) => ref s; [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - static void Start((string val, bool) param) - { - s_action.Invoke(param.val, param.Item2); - } + static void Start((string val, bool) param) => s_action.Invoke(param.val, param.Item2); class GenericClass diff --git a/src/Ben.Demystifier/EnhancedStackTrace.Frames.cs b/src/Ben.Demystifier/EnhancedStackTrace.Frames.cs index cfde1f5..97d0683 100644 --- a/src/Ben.Demystifier/EnhancedStackTrace.Frames.cs +++ b/src/Ben.Demystifier/EnhancedStackTrace.Frames.cs @@ -1,4 +1,4 @@ -// Copyright (c) Ben A Adams. All rights reserved. +// Copyright (c) Ben A Adams. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. @@ -86,10 +86,12 @@ namespace System.Diagnostics return null; } - MethodBase method = originMethod; + var method = originMethod; - var methodDisplayInfo = new ResolvedMethod(); - methodDisplayInfo.SubMethodBase = method; + var methodDisplayInfo = new ResolvedMethod + { + SubMethodBase = method + }; // Type name var type = method.DeclaringType; @@ -240,7 +242,7 @@ namespace System.Diagnostics var generatedName = methodName; - if (!TryParseGeneratedName(generatedName, out kind, out int openBracketOffset, out int closeBracketOffset)) + if (!TryParseGeneratedName(generatedName, out kind, out var openBracketOffset, out var closeBracketOffset)) { return false; } @@ -464,10 +466,10 @@ namespace System.Diagnostics private static int IndexOfBalancedParenthesis(string str, int openingOffset, char closing) { - char opening = str[openingOffset]; + var opening = str[openingOffset]; - int depth = 1; - for (int i = openingOffset + 1; i < str.Length; i++) + var depth = 1; + for (var i = openingOffset + 1; i < str.Length; i++) { var c = str[i]; if (c == opening) diff --git a/src/Ben.Demystifier/Enumerable/EnumerableIList.cs b/src/Ben.Demystifier/Enumerable/EnumerableIList.cs index 58ced0b..ef883df 100644 --- a/src/Ben.Demystifier/Enumerable/EnumerableIList.cs +++ b/src/Ben.Demystifier/Enumerable/EnumerableIList.cs @@ -1,4 +1,4 @@ -// Copyright (c) Ben A Adams. All rights reserved. +// Copyright (c) Ben A Adams. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. namespace System.Collections.Generic.Enumerable @@ -12,10 +12,7 @@ namespace System.Collections.Generic.Enumerable { private readonly IList _list; - public EnumerableIList(IList list) - { - _list = list; - } + public EnumerableIList(IList list) => _list = list; public EnumeratorIList GetEnumerator() => new EnumeratorIList(_list); diff --git a/src/Ben.Demystifier/Internal/ILReader.cs b/src/Ben.Demystifier/Internal/ILReader.cs index 6e8e0d5..6da2be9 100644 --- a/src/Ben.Demystifier/Internal/ILReader.cs +++ b/src/Ben.Demystifier/Internal/ILReader.cs @@ -1,4 +1,4 @@ -using System.Reflection; +using System.Reflection; using System.Reflection.Emit; namespace System.Diagnostics.Internal @@ -12,10 +12,7 @@ namespace System.Diagnostics.Internal private int ptr; - public ILReader(byte[] cil) - { - _cil = cil; - } + public ILReader(byte[] cil) => _cil = cil; public OpCode OpCode { get; private set; } public int MetadataToken { get; private set; } @@ -34,7 +31,7 @@ namespace System.Diagnostics.Internal OpCode ReadOpCode() { - byte instruction = ReadByte(); + var instruction = ReadByte(); if (instruction < 254) return singleByteOpCode[instruction]; else @@ -71,18 +68,15 @@ namespace System.Diagnostics.Internal return null; } - byte ReadByte() - { - return _cil[ptr++]; - } + byte ReadByte() => _cil[ptr++]; int ReadInt() { - byte b1 = ReadByte(); - byte b2 = ReadByte(); - byte b3 = ReadByte(); - byte b4 = ReadByte(); - return (int)b1 | (((int)b2) << 8) | (((int)b3) << 16) | (((int)b4) << 24); + var b1 = ReadByte(); + var b2 = ReadByte(); + var b3 = ReadByte(); + var b4 = ReadByte(); + return b1 | b2 << 8 | b3 << 16 | b4 << 24; } static ILReader() @@ -90,11 +84,11 @@ namespace System.Diagnostics.Internal singleByteOpCode = new OpCode[225]; doubleByteOpCode = new OpCode[31]; - FieldInfo[] fields = GetOpCodeFields(); + var fields = GetOpCodeFields(); - for (int i = 0; i < fields.Length; i++) + for (var i = 0; i < fields.Length; i++) { - OpCode code = (OpCode)fields[i].GetValue(null); + var code = (OpCode)fields[i].GetValue(null); if (code.OpCodeType == OpCodeType.Nternal) continue; @@ -105,9 +99,6 @@ namespace System.Diagnostics.Internal } } - static FieldInfo[] GetOpCodeFields() - { - return typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static); - } + static FieldInfo[] GetOpCodeFields() => typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static); } } diff --git a/src/Ben.Demystifier/Internal/PortablePdbReader.cs b/src/Ben.Demystifier/Internal/PortablePdbReader.cs index d9ce03f..d92d394 100644 --- a/src/Ben.Demystifier/Internal/PortablePdbReader.cs +++ b/src/Ben.Demystifier/Internal/PortablePdbReader.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; @@ -70,8 +70,7 @@ namespace System.Diagnostics.Internal private MetadataReader GetMetadataReader(string assemblyPath) { - MetadataReaderProvider provider = null; - if (!_cache.TryGetValue(assemblyPath, out provider)) + if (!_cache.TryGetValue(assemblyPath, out var provider)) { var pdbPath = GetPdbPath(assemblyPath); diff --git a/test/Ben.Demystifier.Test/MixedStack.cs b/test/Ben.Demystifier.Test/MixedStack.cs index 880a3ce..c98db5d 100644 --- a/test/Ben.Demystifier.Test/MixedStack.cs +++ b/test/Ben.Demystifier.Test/MixedStack.cs @@ -14,7 +14,7 @@ namespace Demystify public void ProducesReadableFrames() { // Arrange - Exception exception = GetMixedStackException(); + var exception = GetMixedStackException(); // Act var methodNames = new EnhancedStackTrace(exception) @@ -91,16 +91,10 @@ namespace Demystify } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - static async Task MethodAsync(TValue value) - { - return await MethodAsync(1); - } + static async Task MethodAsync(TValue value) => await MethodAsync(1); [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - static (string val, bool) Method(string value) - { - return (MethodAsync(value).GetAwaiter().GetResult(), true); - } + static (string val, bool) Method(string value) => (MethodAsync(value).GetAwaiter().GetResult(), true); [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static ref string RefMethod(string value) @@ -110,10 +104,7 @@ namespace Demystify } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] - static void Start((string val, bool) param) - { - s_action.Invoke(param.val, param.Item2); - } + static void Start((string val, bool) param) => s_action.Invoke(param.val, param.Item2); static Action s_action = (string s, bool b) => s_func(s, b); static Func s_func = (string s, bool b) => (RefMethod(s), b); diff --git a/test/Ben.Demystifier.Test/NonThrownException.cs b/test/Ben.Demystifier.Test/NonThrownException.cs index 865c68b..5f71385 100644 --- a/test/Ben.Demystifier.Test/NonThrownException.cs +++ b/test/Ben.Demystifier.Test/NonThrownException.cs @@ -24,7 +24,7 @@ namespace Demystify } // Act - Exception demystifiedException = new Exception(innerException.Message, innerException).Demystify(); + var demystifiedException = new Exception(innerException.Message, innerException).Demystify(); // Assert var stackTrace = demystifiedException.ToString();