Adds coding standards (#22)

* Adds code requirements

* Update to coding standard
This commit is contained in:
Tim Seaward 2017-11-14 00:45:54 +00:00 committed by Ben Adams
parent fa0ad4ca52
commit 77ba50dff9
8 changed files with 135 additions and 78 deletions

94
.editorconfig Normal file
View File

@ -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

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
@ -38,10 +38,7 @@ class Program
} }
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
Program(Action action) Program(Action action) => RunAction((state) => _action((s) => action(), state), null);
{
RunAction((state) => _action((s) => action(), state), null);
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static IEnumerable<string> Iterator(int startAt) static IEnumerable<string> Iterator(int startAt)
@ -75,31 +72,23 @@ class Program
} }
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static void RunAction(Action<object> lambda, object state) static void RunAction(Action<object> lambda, object state) => lambda(state);
{
lambda(state);
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static string RunLambda(Func<string> lambda) static string RunLambda(Func<string> lambda) => lambda();
{
return lambda();
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static (string val, bool) Method(string value) static (string val, bool) Method(string value)
{ {
#pragma warning disable IDE0039 // Use local function
Func<string> func = () => MethodAsync(value).GetAwaiter().GetResult(); Func<string> func = () => MethodAsync(value).GetAwaiter().GetResult();
#pragma warning restore IDE0039 // Use local function
var anonType = new { func }; var anonType = new { func };
return (RunLambda(() => anonType.func()), true); return (RunLambda(() => anonType.func()), true);
} }
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static ref string RefMethod(int value) static ref string RefMethod(int value) => ref s;
{
return ref s;
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static string RefMethod(in string value) static string RefMethod(in string value)
@ -137,16 +126,10 @@ class Program
} }
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static ref string RefMethod(bool value) static ref string RefMethod(bool value) => ref s;
{
return ref s;
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static void Start((string val, bool) param) static void Start((string val, bool) param) => s_action.Invoke(param.val, param.Item2);
{
s_action.Invoke(param.val, param.Item2);
}
class GenericClass<TSuperType> class GenericClass<TSuperType>

View File

@ -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. // 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. // 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. // 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; return null;
} }
MethodBase method = originMethod; var method = originMethod;
var methodDisplayInfo = new ResolvedMethod(); var methodDisplayInfo = new ResolvedMethod
methodDisplayInfo.SubMethodBase = method; {
SubMethodBase = method
};
// Type name // Type name
var type = method.DeclaringType; var type = method.DeclaringType;
@ -240,7 +242,7 @@ namespace System.Diagnostics
var generatedName = methodName; 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; return false;
} }
@ -464,10 +466,10 @@ namespace System.Diagnostics
private static int IndexOfBalancedParenthesis(string str, int openingOffset, char closing) private static int IndexOfBalancedParenthesis(string str, int openingOffset, char closing)
{ {
char opening = str[openingOffset]; var opening = str[openingOffset];
int depth = 1; var depth = 1;
for (int i = openingOffset + 1; i < str.Length; i++) for (var i = openingOffset + 1; i < str.Length; i++)
{ {
var c = str[i]; var c = str[i];
if (c == opening) if (c == opening)

View File

@ -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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace System.Collections.Generic.Enumerable namespace System.Collections.Generic.Enumerable
@ -12,10 +12,7 @@ namespace System.Collections.Generic.Enumerable
{ {
private readonly IList<T> _list; private readonly IList<T> _list;
public EnumerableIList(IList<T> list) public EnumerableIList(IList<T> list) => _list = list;
{
_list = list;
}
public EnumeratorIList<T> GetEnumerator() => new EnumeratorIList<T>(_list); public EnumeratorIList<T> GetEnumerator() => new EnumeratorIList<T>(_list);

View File

@ -1,4 +1,4 @@
using System.Reflection; using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;
namespace System.Diagnostics.Internal namespace System.Diagnostics.Internal
@ -12,10 +12,7 @@ namespace System.Diagnostics.Internal
private int ptr; private int ptr;
public ILReader(byte[] cil) public ILReader(byte[] cil) => _cil = cil;
{
_cil = cil;
}
public OpCode OpCode { get; private set; } public OpCode OpCode { get; private set; }
public int MetadataToken { get; private set; } public int MetadataToken { get; private set; }
@ -34,7 +31,7 @@ namespace System.Diagnostics.Internal
OpCode ReadOpCode() OpCode ReadOpCode()
{ {
byte instruction = ReadByte(); var instruction = ReadByte();
if (instruction < 254) if (instruction < 254)
return singleByteOpCode[instruction]; return singleByteOpCode[instruction];
else else
@ -71,18 +68,15 @@ namespace System.Diagnostics.Internal
return null; return null;
} }
byte ReadByte() byte ReadByte() => _cil[ptr++];
{
return _cil[ptr++];
}
int ReadInt() int ReadInt()
{ {
byte b1 = ReadByte(); var b1 = ReadByte();
byte b2 = ReadByte(); var b2 = ReadByte();
byte b3 = ReadByte(); var b3 = ReadByte();
byte b4 = ReadByte(); var b4 = ReadByte();
return (int)b1 | (((int)b2) << 8) | (((int)b3) << 16) | (((int)b4) << 24); return b1 | b2 << 8 | b3 << 16 | b4 << 24;
} }
static ILReader() static ILReader()
@ -90,11 +84,11 @@ namespace System.Diagnostics.Internal
singleByteOpCode = new OpCode[225]; singleByteOpCode = new OpCode[225];
doubleByteOpCode = new OpCode[31]; 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) if (code.OpCodeType == OpCodeType.Nternal)
continue; continue;
@ -105,9 +99,6 @@ namespace System.Diagnostics.Internal
} }
} }
static FieldInfo[] GetOpCodeFields() static FieldInfo[] GetOpCodeFields() => typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static);
{
return typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static);
}
} }
} }

View File

@ -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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic; using System.Collections.Generic;
@ -70,8 +70,7 @@ namespace System.Diagnostics.Internal
private MetadataReader GetMetadataReader(string assemblyPath) private MetadataReader GetMetadataReader(string assemblyPath)
{ {
MetadataReaderProvider provider = null; if (!_cache.TryGetValue(assemblyPath, out var provider))
if (!_cache.TryGetValue(assemblyPath, out provider))
{ {
var pdbPath = GetPdbPath(assemblyPath); var pdbPath = GetPdbPath(assemblyPath);

View File

@ -14,7 +14,7 @@ namespace Demystify
public void ProducesReadableFrames() public void ProducesReadableFrames()
{ {
// Arrange // Arrange
Exception exception = GetMixedStackException(); var exception = GetMixedStackException();
// Act // Act
var methodNames = new EnhancedStackTrace(exception) var methodNames = new EnhancedStackTrace(exception)
@ -91,16 +91,10 @@ namespace Demystify
} }
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static async Task<string> MethodAsync<TValue>(TValue value) static async Task<string> MethodAsync<TValue>(TValue value) => await MethodAsync(1);
{
return await MethodAsync(1);
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static (string val, bool) Method(string value) static (string val, bool) Method(string value) => (MethodAsync(value).GetAwaiter().GetResult(), true);
{
return (MethodAsync(value).GetAwaiter().GetResult(), true);
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static ref string RefMethod(string value) static ref string RefMethod(string value)
@ -110,10 +104,7 @@ namespace Demystify
} }
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
static void Start((string val, bool) param) static void Start((string val, bool) param) => s_action.Invoke(param.val, param.Item2);
{
s_action.Invoke(param.val, param.Item2);
}
static Action<string, bool> s_action = (string s, bool b) => s_func(s, b); static Action<string, bool> s_action = (string s, bool b) => s_func(s, b);
static Func<string, bool, (string val, bool)> s_func = (string s, bool b) => (RefMethod(s), b); static Func<string, bool, (string val, bool)> s_func = (string s, bool b) => (RefMethod(s), b);

View File

@ -24,7 +24,7 @@ namespace Demystify
} }
// Act // Act
Exception demystifiedException = new Exception(innerException.Message, innerException).Demystify(); var demystifiedException = new Exception(innerException.Message, innerException).Demystify();
// Assert // Assert
var stackTrace = demystifiedException.ToString(); var stackTrace = demystifiedException.ToString();