主要内容

Check for Certain Naming Conventions in Your Code

You can configure the Code Analyzer to check that the names of variables, functions, and other types of MATLAB® identifiers follow certain naming conventions that you set. If a name does not match the specified naming conventions, then the Code Analyzer displays a message. You can configure naming conventions for these types of MATLAB identifiers:

  • "variable"

  • "function"

  • "localFunction"

  • "nestedFunction"

  • "class"

  • "property"

  • "method"

  • "event"

  • "enumeration"

To set naming conventions, create a file named codeAnalyzerConfiguration.json and add a "naming" property. In the "naming" property, define naming conventions for different types of MATLAB identifiers by specifying the identifier types and their naming convention properties. Each configured identifier can have one or more naming convention properties. For more information on setting up codeAnalyzerConfiguration.json, see Customize Code Analyzer Checks Using Configuration File.

Check Name Lengths

You can configure a minimum or maximum name length for identifiers using the following properties. The Code Analyzer displays a message if an identifier name has fewer characters than the minimum or more characters than the maximum.

Property NameDescriptionExample
"maxLength"Specify the maximum number of characters for an identifier name.
"maxLength" : 12
"minLength"Specify the minimum number of characters for an identifier name.
"minLength" : 4

For example, require that the names of local and nested functions have a minimum length of 4 by specifying this code in the codeAnalyzerConfiguration.json file.

    "naming": {
        "localFunction" : {
            "minLength" : 4
        },
        "nestedFunction" : {
            "minLength" : 4
        }
    }

Check for Prefixes, Suffixes, and Phrases

You can configure required or disallowed prefixes and suffixes for identifiers using the following properties. You can also configure disallowed phrases, and the Code Analyzer displays a message if it finds the phrases anywhere in an identifier name.

Property NameDescriptionExample
"requiredPrefix"Specify the prefix or prefixes that identifier names must begin with, as a string scalar or string array.
"requiredPrefix" : ["prop","property"]
"disallowedPrefix"Specify the prefix or prefixes that identifier names must not begin with, as a string scalar or string array.
"disallowedPrefix" : ["get","set"]
"requiredSuffix"Specify the suffix or suffixes that identifier names must end with, as a string scalar or string array.
"requiredSuffix" : "orgABC"​
"disallowedSuffix"Specify the suffix or suffixes that identifier names must not end with, as a string scalar or string array.
"disallowedSuffix" : ["event","EVENT"]​
"disallowedPhrase"Specify the phrase or phrases that identifier names must not contain, as a string scalar or string array.
"disallowedPhrase" : ["enum","ENUM","class","_day"]

For example, require that the names of nested functions use certain prefixes and have a minimum length of 5 by specifying this code in the codeAnalyzerConfiguration.json file.

    "naming": {
        "nestedFunction": {
            "requiredPrefix" : ["nest","sub"],
            "minLength": 5
        }
    }

Check for Casing

You can check that identifiers conform to one or more casing standards. Specify a "casing" property containing a string scalar or string array where each element is a casing standard. If you specify more than one casing standard, then identifiers must satisfy at least one of them.

For example, this check specifies that function names must be uppercase with underscores or upper camel case.

    "naming": {
        "function": {
            "casing": ["UPPERCASE_WITH_UNDERSCORES","UpperCamelCase"]
        }
    }

Prefixes that match the requiredPrefix property and suffixes that match the requiredSuffix property are not included in casing checks. For example, this check specifies a required prefix as well as upper camel case for variable names. The check ignores the prefix when checking if the name adheres to the upper camel case rule, so a name like isEnabled is accepted.

    "naming": {
        "variable": {
            "casing": "UpperCamelCase",
            "requiredPrefix": ["init","pre","is"]
        }
    }

This table shows the values you can specify when setting casing standards.

CasingRequirementsMatching ExamplesNon-Matching Examples
"lowercase"
  • Contain all lowercase characters.

  • Contain only characters and digits.

example, examplename, example123EXAMPLE, exampleName, example_123
"UPPERCASE"
  • Contain all uppercase characters.

  • Contain only characters and digits.

EXAMPLE, EXAMPLENAME, EXAMPLE123Example, ExampleName, EXAMPLE_123
"lowerCamelCase"
  • First letter must be lowercase.

  • Subsequent words start with uppercase letter.

  • Two consecutive letters can be uppercase.

  • Contain only characters and digits.

example, exampleName, example123, exampleNAMEExample, EXAMPLE, ExampleName, exampleName_123
"UpperCamelCase"
  • First letter must be uppercase.

  • Subsequent words start with uppercase letter.

  • Two consecutive letters can be uppercase.

  • Contain only characters and digits.

Example, ExampleName, Example123, ExampleNAmeexample, eXAMPLE, exampleName, ExampleName_123
"lowercase_with_underscores"
  • All characters must be either lowercase letters, digits, or underscores.

  • No consecutive underscores.

example, examplename, example_name, example_123, example_name_EXAMPLE, exampleName, example__123
"UPPERCASE_WITH_UNDERSCORES"
  • All characters must be either uppercase letters, digits, or underscores.

  • No consecutive underscores.

EXAMPLE, EXAMPLENAME, EXAMPLE_NAME, EXAMPLE_123, EXAMPLE_NAME_Example, EXAMPLEnAME, EXAMPLE__123

Check for Custom Naming Conventions Using Regular Expressions

You can create a custom naming convention check by specifying the "regularExpression" property. The Code Analyzer displays a message if the identifier name does not fully match the specified regular expression. Required prefixes and suffixes are not excluded from this check. Each identifier type can have only one regular expression check.

For example, require that event names use only uppercase characters and start with the prefix "E" by specifying this code in the codeAnalyzerConfiguration.json file.

    "naming": {
        "event": {
            "regularExpression": "^([A-Z]+)$",
            "requiredPrefix": "E"
        }
    }

For more information on constructing regular expressions, see Steps for Building Expressions.

This table shows regular expressions for the casing options listed in Check for Casing.

Regular ExpressionCasingMatching ExamplesNon-Matching Examples
"^[a-z0-9]+$"
"lowercase"example, examplename, example123EXAMPLE, exampleName, example_123
"^[A-Z0-9]+$"
"UPPERCASE"EXAMPLE, EXAMPLENAME, EXAMPLE123Example, ExampleName, EXAMPLE_123
"^[a-z][a-zA-Z0-9]*$"
"lowerCamelCase"example, exampleName, example123, exampleNAMEExample, EXAMPLE, ExampleName, exampleName_123
"^[A-Z][a-zA-Z0-9]*$"
"UpperCamelCase"Example, ExampleName, Example123, ExampleNAmeexample, eXAMPLE, exampleName, ExampleName_123
"^(?!.*__)[a-z0-9_]+$"
"lowercase_with_underscores"example, examplename, example_name, example_123, example_name_EXAMPLE, exampleName, example__123
"^(?!.*__)[A-Z0-9_]+$"
"UPPERCASE_WITH_UNDERSCORES"EXAMPLE, EXAMPLENAME, EXAMPLE_NAME, EXAMPLE_123, EXAMPLE_NAME_Example, EXAMPLEnAME, EXAMPLE__123

This table shows additional regular expression examples for naming conventions.

Regular ExpressionDescriptionMatching ExamplesNon-Matching Examples
"^[A-Z][a-z0-9]*$"
Match strings starting with an uppercase letter, followed by zero or more lowercase letters or digits. Underscores and other special characters are not allowed. (Leading uppercase)Example, Example123, A, Binverse, C1, Examplenameexample, EXAMPLE, B_inverse, Example_name, Examplename_123, eXample, EXample, ExampleName, exampleName, exampleName123
"(^[a-z][a-zA-Z0-9]*$)|(^[A-Z][a-z0-9]*$)"
Match strings with either the lower camel case format or the leading uppercase format. example, exampleName, example123, exampleNAME, Example, Example123, A, Binverse, C1, ExamplenameEXAMPLE, B_inverse, Example_name, Examplename_123, EXample, ExampleName
"^[a-z](?:[a-z0-9]+[A-Z]?[a-z0-9]*)*$"
Match strings starting with a lowercase letter. No two consecutive characters are uppercase.example, exampleName, example123Example, EXAMPLE, ExampleName, exampleNAME, exampleName_123
"^[A-Z](?:[a-z0-9]+[A-Z]?[a-z0-9]*)*$"
Match strings starting with an uppercase letter. No two consecutive characters are uppercase.Example, ExampleName, Example123example, eXAMPLE, exampleName, ExampleName_123, ExampleNAme
"^(?!.*__)[a-z0-9_]+(?<!_)$"
Match strings composed of lowercase letters, digits, and underscores, ensuring that the string does not end with an underscore and contains no consecutive underscores.example, examplename, example_name, example_123EXAMPLE, exampleName, example__123, example_name_
"^(?!.*__)[A-Z0-9_]+(?<!_)$"
Match strings composed of uppercase letters, digits, and underscores, ensuring that the string does not end with an underscore and contains no consecutive underscores.EXAMPLE, EXAMPLENAME, EXAMPLE_NAMEExample, EXAMPLEnAME, EXAMPLE__123, EXAMPLE_NAME_

See Also

Apps

Objects

Functions

Topics