VS中代码格式及样式的统一处理

介绍

最近一直在回顾公司项目的代码,包括整个架构,命名和样式等等。因为项目不知道中间有几个人写过,所以代码风格的多样性也是不可避免的,命名更是随心所欲的,更有私有变量用首字母大写的单词命名的。感觉上从开始学习编程,就在强调代码可读性、可维护性等等问题,但是由于很多的原因,像变量命名这样最简单的事情,好像一直也没有一个相对统一的规则,只是有一些不成文的规定。但就这要的约定,对于绝大部分的程序员来说,可能都是不知道的吧。

现在最新版本的vs2019vs2022都支持在项目中加入一个.editorconfig的文件来统一处理代码的样式风格,命名风格等问题。也许对于软件开发的公司来说,可以方便的制定一个自己公司的编码风格,至少在一定程度上有利于代码的管理吧。

具体说明

EditorConfig文件的添加

在新建的项目中右击=》添加,选择【新建EditorConfig】后,在项目的根目录下会自动创建一个.editorconfig的文件。

在这里插入图片描述

默认打开的不是文本编辑的样子,而是一个可操作的界面,方便大家对默认样式的选择,而不需要记者具体的命令。当然,你可以通过vs code等编辑器打开.editorconfig文件进行编辑(推荐),也可以在vs中右击此文件,选择【打开方式(N)…】,如下图所示,选择具体的编辑器就行编辑,但是个人不建议这么做。

在这里插入图片描述

vs2019vs2022.editorconfig文件默认打开是有区别的,这里只展示vs2022的打开效果。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

vs2019中是没有【命名样式】整个tab中,同样的在【分析器】中也是没有内容显示的。当然,具体的样式也是有区别的,可参考官方网站:https://docs.microsoft.com/zh-cn/dotnet/fundamentals/code-analysis/style-rules/

代码样式选项

这里我们主要展示的是官方给出的默认文本展示,即通过命令的展示效果。

###############################
# Core EditorConfig Options   #
###############################
root = true
# All files
[*]
indent_style = space

# XML project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2

# XML config files
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
indent_size = 2

# Code files
[*.{cs,csx,vb,vbx}]
indent_size = 4
insert_final_newline = true
charset = utf-8-bom
###############################
# .NET Coding Conventions     #
###############################
[*.{cs,vb}]
# Organize usings
dotnet_sort_system_directives_first = true
# this. preferences
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_property = false:silent
dotnet_style_qualification_for_method = false:silent
dotnet_style_qualification_for_event = false:silent
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_predefined_type_for_member_access = true:silent
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
###############################
# Naming Conventions          #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization             = pascal_case
# Use PascalCase for constant fields  
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols  = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style    = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds            = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities  = *
dotnet_naming_symbols.constant_fields.required_modifiers          = const
###############################
# C# Coding Conventions       #
###############################
[*.cs]
# var preferences
csharp_style_var_for_built_in_types = true:silent
csharp_style_var_when_type_is_apparent = true:silent
csharp_style_var_elsewhere = true:silent
# Expression-bodied members
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
# Pattern matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:silent
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules         #
###############################
# New line preferences
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 preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true
###############################
# VB Coding Conventions       #
###############################
[*.vb]
# Modifier preferences
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:suggestion

官方网址:https://docs.microsoft.com/zh-cn/dotnet/fundamentals/code-analysis/code-style-rule-options?view=vs-2022。此网址可以在添加的.editorconfig文件(文本编辑的情况,如vs code中打开的时候)的顶部看到。

代码样式命名规则

此命名规则的一般语法如下:

<kind>.<title>.<propertyName> = <propertyValue>

< kind>

< kind> 指定要定义的元素类型(命名规则、符号组或命名样式)并且必须是下列元素之一:

设置属性<使用种类>值示例
命名规则dotnet_naming_ruledotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
符号组dotnet_naming_symbolsdotnet_naming_symbols.interface.applicable_kinds = interface
命名样式dotnet_naming_styledotnet_naming_style.pascal_case.capitalization = pascal_case

< title>

< title> 是选择的描述性名称,用于将多个属性设置关联到单个定义中。 例如,以下属性生成两个符号组定义:interfacetypes,并为每一个定义都设置了两个属性。

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum, delegate
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected

命名规则属性

要使规则生效,必须具有所有命名规则属性。

属性说明
symbols符号组的标题;命名规则将应用于此组中的符号
style应与此规则关联的命名样式的标题
severity设置用于强制执行命名规则的严重性。 将关联的值设置为任一可用的严重性级别

符号组属性

你可以为符号组设置以下属性,以限制组中包含的符号。 若要为单个属性指定多个值,请用逗号分隔这些值。

属性说明允许的值必选
applicable_kinds组中符号的种类*(使用此值可指定所有符号)
namespace
class
struct
interface
enum
property
method
field
event
delegate
parameter
type_parameter
local
local_function
applicable_accessibilities组中符号的可访问性级别*(使用此值可指定所有可访问性级别)
public
internalfriend
private
protected
protected_internalprotected_friend
private_protected
local(用于方法内定义的符号)
required_modifiers仅将符号与所有指定的修饰符进行匹配abstractmust_inherit
async
const
readonly
staticshared

注意:

1、目前 applicable_kinds 不支持元组成员。
2、这意味着符号的修饰符不会影响是否应用此规则。
3、如果组的 required_modifiers 属性包含 static 或 shared,那么组还将包括 const 符号,因为它们是隐式 static/Shared。 不过,如果你不希望将 static 命名规则应用于 const 符号,可以使用 const 符号组创建新的命名规则。
4、class 包括 C# 记录。

命名样式属性

命名样式定义要通过规则强制执行的约定。 例如:

  • 采用 PascalCase 大写形式
  • 以 m_ 开头
  • 以 _g 结尾
  • 用 __ 分隔单词

可以为命名样式设置以下属性:

属性说明允许的值必选
capitalization符号内的单词的大写样式pascal_case
camel_case
first_word_upper
all_upper
all_lower
required_prefix必须以这些字符开头
required_suffix必须以这些字符结尾
word_separator符号内的单词必须用此字符分隔

注意:必须在命名样式中指定大写样式,否则会忽略命名样式。

默认命名样式

如果不指定任何自定义命名规则,系统将使用下列默认样式:

  • 对于具有任意辅助功能的类、结构、枚举、属性、方法以及事件,默认的命名样式为帕斯卡拼写法。

  • 对于具有任意辅助功能的接口,默认的命名样式为帕斯卡拼写法并必须附加 l 前缀。

EditorConfig 文件中定义命名规则的顺序并不重要。 命名规则根据规则本身的定义自动排序。要想了解更多内容,可以点击此处

自定义命名规则

通过上一部分的学习,我们是可以进行自定义一些自己习惯的编码风格的。如下,给出了常量和静态只读字段必须大写(多个单词用 _ 分割)私有字段使用 _ 作为前缀 的示例说明。

# Naming rules
dotnet_naming_rule.const_should_be_all_upper.severity = suggestion
dotnet_naming_rule.const_should_be_all_upper.symbols = const
dotnet_naming_rule.const_should_be_all_upper.style = all_upper

dotnet_naming_rule.static_readonly_field_should_be_all_upper.severity = suggestion
dotnet_naming_rule.static_readonly_field_should_be_all_upper.symbols = static_readonly_field
dotnet_naming_rule.static_readonly_field_should_be_all_upper.style = all_upper

dotnet_naming_rule.private_or_internal_field_should_be_private_field.severity = suggestion
dotnet_naming_rule.private_or_internal_field_should_be_private_field.symbols = private_or_internal_field
dotnet_naming_rule.private_or_internal_field_should_be_private_field.style = private_field

# Symbol specifications
dotnet_naming_symbols.const.applicable_kinds = field
dotnet_naming_symbols.const.applicable_accessibilities = *
dotnet_naming_symbols.const.required_modifiers = const

dotnet_naming_symbols.static_readonly_field.applicable_kinds = field
dotnet_naming_symbols.static_readonly_field.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.static_readonly_field.required_modifiers = readonly, static

dotnet_naming_symbols.private_or_internal_field.applicable_kinds = field
dotnet_naming_symbols.private_or_internal_field.applicable_accessibilities = internal, private, private_protected
dotnet_naming_symbols.private_or_internal_field.required_modifiers = 

# Naming styles
dotnet_naming_style.private_field.required_prefix = _ 
dotnet_naming_style.private_field.capitalization = camel_case


dotnet_naming_style.all_upper.word_separator = _
dotnet_naming_style.all_upper.capitalization = all_upper

通过上面的定义,在VS编辑器中的效果如下。

在这里插入图片描述

在这里插入图片描述

ps:以上操作基本使用的都是vs2022中的效果

总结

通过上面内容的学习,我们就可以通过.editorconfig文件来规范、定制自己的代码风格了。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值