HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Applying naming conventions to any given identifier

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
identifieranynamingapplyinggivenconventions

Problem

VSDiagnostics has an analyzer that verifies whether or not a given identifier is following naming conventions. The following conventions are supported:

  • UpperCamelCase



  • lowerCamelCase



  • _lowerCamelCase



  • ICamelCase



On top of simply verifying this convention is followed it also suggests a new name that does follow conventions. The way I have implemented the verification is simple: simply generate a new identifier that follows conventions and check whether it's the same as the existing one.

Now, the interesting work is done inside the generating of this new identifier of course.

The approach I have thought out is the following:


Any identifier is basically a group of 0, 1 or more "sections". A section is denoted by special characters (anything that isn't a letter or a number) and capital letters that are preceded by a lower-case letter. For example in My_var, MyVar and MY_VAR all identifiers have two sections: My/var, My/Var and MY/VAR. Once everything is grouped in sections we have to apply the proper naming convention. This is now made easy because everything ends with '-CamelCase' meaning we only have to apply the naming convention to the first section and all the remaining ones can have the 'Upper' convention.

Written in code that gives you the following implementation: Github

```
///
/// Removes all non-digit, non-alphabetic characters. The naming convention of the first section can be specified, all
/// others use .
/// For example:
/// input = "_allo_ello"; first section = "allo"
/// input = "IBufferMyBuffer"; first section = "IBuffer"
/// input = "MY_SNAKE_CASE"; first section = "MY"
/// This allows us to remove things like underscores and have the individual sections they denoted in a proper
/// convention as well
///
///
///
///
internal static string GetNormalizedString(string input, Func getFirstEntryConventioned)
{
var sections = new List();
var tempBuffer = new StringBuilder();

A

Solution

-
At first I didn't really like the implementation with the local Action but upon trying to re-implement it I realized it's difficult to restructure the code without making it actually more confusing - so fair enough.

-
GetNormalizedString can't deal with any of it's parameters being null. For input you could check for string.IsNullOrWhiteSpace and return or throw an ArgumentNullException. getFirstEntryConventioned should probably have an ArgumentNullExeption thrown.

Same goes for the naming convention functions: You only check for input.Length == 0. This means you're never expecting null arguments in which case you should use either use contracts or throw ArgumentNullExceptions or return empty strings.

However given that this is a plugin meant to be used in Visual Studio as you edit you probably don't want exceptions bubbling up, so dealing with null inputs might be desirable from a user perspective.

-
I think IUpper can be simplified a little bit:

internal static string IUpper(string input)
{
    if (input.Length == 0)
    {
        return string.Empty;
    }

    if (input.StartsWith("I", StringComparison.OrdinalIgnoreCase))
    {
        input = input.Substring(1);
    }
    return "I" + Upper(input);
}


Essentially, remove a leading I if there is one and then reduce it to the Upper case.

Code Snippets

internal static string IUpper(string input)
{
    if (input.Length == 0)
    {
        return string.Empty;
    }

    if (input.StartsWith("I", StringComparison.OrdinalIgnoreCase))
    {
        input = input.Substring(1);
    }
    return "I" + Upper(input);
}

Context

StackExchange Code Review Q#119551, answer score: 3

Revisions (0)

No revisions yet.