Visualstudio-docs: Microsoft Naming Conventions .editorconfig

Created on 24 Jan 2018  Â·  12Comments  Â·  Source: MicrosoftDocs/visualstudio-docs

The styles .editorconfig (https://github.com/dotnet/roslyn/blob/master/.editorconfig) is linked from documentation (https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference). It would be great to have the same as it applies to this article and related to Naming Guidelines (https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines) for C#.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

doc-bug unspecifiesvc

Most helpful comment

@kuhlenh added a small naming conventions section to the example EditorConfig at https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#example-editorconfig-file. But it sounds like the request is for an official, complete naming conventions EditorConfig sample based on https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines?

All 12 comments

@alarimer thanks, I'll add a link to the example file from the naming conventions topic too.

@alarimer I added a link to the See also section. Thanks for the feedback!

"See Also" in the closing comment is a link to the "Devenv command line switches" article.

I apologize for not being more clear. I was hoping for an .editorconfig for the Naming Guidelines.

dotnet_naming_rule.public_members_must_be_capitalized.symbols   = public_symbols
dotnet_naming_symbols.public_symbols.applicable_kinds           = property,method,field,event,delegate
dotnet_naming_symbols.public_symbols.applicable_accessibilities = public
dotnet_naming_symbols.public_symbols.required_modifiers         = readonly
dotnet_naming_rule.public_members_must_be_capitalized.style    = first_word_upper_case_style
dotnet_naming_style.first_word_upper_case_style.capitalization = first_word_upper
dotnet_naming_rule.public_members_must_be_capitalized.severity = suggestion

HTH

@alarimer ahh, okay, that makes more sense. Also I fixed the See also link; sorry about that.

No worries. I appreciate it.

The .editorconfig file from @RehanSaeed is the best one I found that follows the Naming Guidelines, but I agree with @alarimer, an official and full sample linked in the documentation would be great!

@RehanSaeed example is incomplete, at least as to fields. Let me give some details....

Foundational background

Protected fields should be treated as public, according to .NET design guidelines.

The same .NET design guidelines drastically limit allowed public fields, by allowing only two exceptions.

The naming guidelines explicitly _exclude_ internal and private fields, so even if allowed, no single .editorconfig would apply.

.net source code has historically shown at least the following two methods for internal fields:

  1. camelCasing of the field
  2. prefix the field with an underscore and use PascalCasing

Resulting split for fields

  1. Allowed, but no specified naming convention
  2. all private fields
  3. all internal fields
  4. all protected_internal fields
  5. Allowed with specified naming convention:
  6. public const fields
  7. public static readonly fields
  8. protected const fields
  9. protected static readonly fields
  10. Not allowed:
  11. any other public or protected field

Let me see if I can whip up a sample that covers fields more appropriately...

OK, this should be a reasonable sample that more appropriately covers fields...

######################################################################
# Start by defining the naming symbols (groups) for fields...
######################################################################
# allowed by design guidelines, but naming is not specified by naming guidelines
dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private, internal, protected_internal

# allowed by design guidelines, and naming guidelines indicate PascalCasing
dotnet_naming_symbols.public_constant_fields.applicable_kinds = field
dotnet_naming_symbols.public_constant_fields.required_modifiers = const
dotnet_naming_symbols.public_constant_fields.applicable_accessibilities = public, protected

dotnet_naming_symbols.public_static_readonly_fields.applicable_kinds = field
dotnet_naming_symbols.public_static_readonly_fields.required_modifiers = static, readonly
dotnet_naming_symbols.public_static_readonly_fields.applicable_accessibilities = public, protected

# not allowed by design guidelines -- all fields unless match one of the above three naming symbols
dotnet_naming_symbols.fields.applicable_kinds = field

######################################################################
# Now define the styles that will be applied to those naming symbols...
######################################################################
# camel_case - Define the camelCase style
dotnet_naming_style.camel_case.capitalization = camel_case
# pascal_case - Define the Pascal_case style
dotnet_naming_style.pascal_case.capitalization = pascal_case
# prefix_with_underscore_pascal_case
dotnet_naming_style.prefix_interface_interface_with_i.capitalization = pascal_case
dotnet_naming_style.prefix_interface_interface_with_i.required_prefix = _
# invalid_style -- used for when the naming symbols is not allowed by design guidelines
dotnet_naming_style.invalid_style.capitalization = pascal_case
dotnet_naming_style.invalid_style.required_prefix = ____invalid____
dotnet_naming_style.invalid_style.required_suffix = ____invalid____

######################################################################
# Naming Rules are matched in the order listed, and only the first match is applied
# Use this to match allowed field types, then match all other field types with the invalid style
# Explicitly mark the field type that is user-preference, to allow simple changing to camelCase
# or other settings...
######################################################################
# Fields that are private can be formatted entirely by user preference
dotnet_naming_rule.private_fields_rule.symbols  = private_fields
dotnet_naming_rule.private_fields_rule.style    = prefix_with_underscore_pascal_case
dotnet_naming_rule.private_fields_rule.severity = warning

# Fields that are public constants must be PascalCase
dotnet_naming_rule.public_constant_fields_rule.symbols  = public_constant_fields
dotnet_naming_rule.public_constant_fields_rule.style    = pascal_case
dotnet_naming_rule.public_constant_fields_rule.severity = warning

# Fields that are public static readonly must be PascalCase
dotnet_naming_rule.public_static_readonly_fields_rule.symbols  = public_static_readonly_fields
dotnet_naming_rule.public_static_readonly_fields_rule.style    = pascal_case
dotnet_naming_rule.public_static_readonly_fields_rule.severity = warning

# No other types of fields are allowed by the .NET design guidelines
dotnet_naming_rule.match_all_fields_rule.symbols  = match_all_fields
dotnet_naming_rule.match_all_fields_rule.style    = invalid_style
dotnet_naming_rule.match_all_fields_rule.severity = error

That should work, or at least be close enough to get you started.

@henrygab My https://github.com/RehanSaeed/EditorConfig is based on the default StyleCop rules which is almost the same as the naming guidelines you listed with the biggest difference being the use of this. instead of _.

I'm not too clear on the difference in your rules and mine but if there is something off about the rules, feel free to submit a PR. I'm always interested in adding more useful rules.

EDIT: I've just seen the PR you've submitted to one of my projects. I actually have a GitHub repo at https://github.com/RehanSaeed/EditorConfig specifically for an .editorconfig file.

@kuhlenh added a small naming conventions section to the example EditorConfig at https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#example-editorconfig-file. But it sounds like the request is for an official, complete naming conventions EditorConfig sample based on https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines?

Naming guidelines were added to the Roslyn .EditorConfig file last July: https://github.com/dotnet/roslyn/blob/master/.editorconfig#L63. I'll add a direct link to this in the See Also section.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gewarren picture gewarren  Â·  3Comments

Crono1981 picture Crono1981  Â·  3Comments

jnpwly picture jnpwly  Â·  3Comments

ChrisMaddock picture ChrisMaddock  Â·  3Comments

JeepNL picture JeepNL  Â·  3Comments