2017-12-13

Use .editorconfig File to Enforce Coding Conventions

As you may know, I literally wrote the book on C# coding conventions. You can get my book, The Reddick C# Style Guide on Amazon. Since the book was published, C# and Visual Studio have changed a little, as they have added new features to both. There is nothing that I would change in the book, but a few of the new features they added to version 7.x of C# that are not mentioned, such as tuples and pattern matching. Until I can get around to updating the book, there is a nifty feature in Visual Studio 2017 that you can use to enforce what I consider to be the proper coding style for C#.

In the root of your code, add a text file called .editorconfig. The basic format for this file is defined at http://EditorConfig.org. There are specific entries that are understood in Visual Studio 2017, starting with version 15.3, that can be found at https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference. This is the file that I use, that uses the I consider to be the right style. Even if you don't agree, feel free to use it as a template for your own style.


# http://EditorConfig.org
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference

root=true

[*]
indent_style=tab
indent_size=tab
tab_width=4
end_of_line=crlf
charset=utf-8
trim_trailing_whitespace=true
insert_final_newline=false
max_line_length=140

[*.{cs,vb}]

# "This." and "Me." qualifiers
dotnet_style_qualification_for_field=true:warning
dotnet_style_qualification_for_property=true:warning
dotnet_style_qualification_for_method=true:warning
dotnet_style_qualification_for_event=true:warning

# Language keywords instead of framework type names for type references
dotnet_style_predefined_type_for_locals_parameters_members=true:warning
dotnet_style_predefined_type_for_member_access=true:warning

# Modifier preferences
dotnet_style_require_accessibility_modifiers=always:warning
csharp_preferred_modifier_order=public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async:warning
visual_basic_preferred_modifier_order=Partial, Default, Private, Protected, Public, Friend, NotOverridable, Overridable, MustOverride, Overloads, Overrides, MustInherit, NotInheritable, Static, Shared, Shadows, ReadOnly, WriteOnly, Dim, Const,WithEvents, Widening, Narrowing, Custom, Async:nonewarning

# Expression-level preferences
dotnet_style_object_initializer=true:warning
dotnet_style_collection_initializer=true:warning
dotnet_style_explicit_tuple_names=true:warning
dotnet_style_coalesce_expression=true:warning
dotnet_style_null_propagation=true:warning

# Implicit and explicit types
csharp_style_var_for_built_in_types=false:warning
csharp_style_var_when_type_is_apparent=false:warningn
csharp_style_var_elsewhere=false:warning

# Expression-bodied members
csharp_style_expression_bodied_methods=false:warning
csharp_style_expression_bodied_constructors=false:warning
csharp_style_expression_bodied_operators=false:warning
csharp_style_expression_bodied_properties=false:warning
csharp_style_expression_bodied_indexers=false:warning
csharp_style_expression_bodied_accessors=false:warning

# Inlined variable declarations
csharp_style_inlined_variable_declaration=true:warning

# Pattern matching
csharp_style_pattern_matching_over_is_with_cast_check=true:warning
csharp_style_pattern_matching_over_as_with_null_check=true:warning

# Expression-level preferences
csharp_prefer_simple_default_expression=true:warning
csharp_style_deconstructed_variable_declaration=true:warning
csharp_style_pattern_local_over_anonymous_function=true:warning

# "Null" checking preferences
csharp_style_throw_expression=false:warning
csharp_style_conditional_delegate_call=true:warning

# Code block preferences
csharp_prefer_braces=true:warning

# Organize Usings
dotnet_sort_system_directives_first=true

# Newline Options
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
csharp_new_line_between_query_expression_clauses=true

# Indentation Options
csharp_indent_case_contents=true
csharp_indent_switch_labels=true
csharp_indent_labels=flush_left

# Spacing Options
csharp_space_after_cast=false
csharp_space_after_keywords_in_control_flow_statements=true
csharp_space_between_method_declaration_parameter_list_parentheses=false
#csharp_space_between_parentheses=

# Wrapping Options
csharp_preserve_single_line_statements=false
csharp_preserve_single_line_blocks=false

1 comment :

Note: Only a member of this blog may post a comment.