Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the idiomatic naming convention for local functions in C# 7

Normal class methods, whether instance or static, have an idiomatic naming convention with regards to their casing. It's not clear that there is a convention for local functions, introduced in C# 7.

Should a local function be named in camelCase?

public static int Factorial(int n) {     return calcFactorial(n);      int calcFactorial(int number) => (number < 2)         ? 1         : number * calcFactorial(number - 1); } 

Or PascalCase?

public static int Factorial(int n) {     return CalcFactorial(n);      int CalcFactorial(int number) => (number < 2)         ? 1         : number * CalcFactorial(number - 1); } 
like image 624
Joseph Daigle Avatar asked May 04 '17 15:05

Joseph Daigle


People also ask

What is the naming convention in C?

The first character of the name should be a letter and all characters (except the period) should be lower-case letters and numbers. The base name should be eight or fewer characters and the suffix should be three or fewer characters (four, if you include the period).

What is the naming convention for functions?

Naming Convention for Functions So, similar to variables, the camel case approach is the recommended way to declare function names. In addition to that, you should use descriptive nouns and verbs as prefixes. For example, if we declare a function to retrieve a name, the function name should be getName.

Does C use camelCase or Snakecase?

Classic C doesn't use camel-case; I've written code in camel-case in C, and it looks weird (so I don't do it like that any more). That said, it isn't wrong - and consistency is more important than which convention is used.

What is the naming convention for variables and functions?

Function and Class Naming conventions An important aspect of naming is to ensure your classes, functions, and variables can be distinguished from each other. For example, one could use Camelcase and Pascalcase for functions and classes respectively, while reserving Snakecase or Hungarian notation for variable names.

What are naming conventions in C programming?

In C programming, naming conventions are set of rules for choosing the valid name to be used for variables and functions in a C program. It should begin with an alphabet. There may be more than one alphabet, but without any spaces between them.

What naming convention should be followed for the function/APIs/interfaces module?

Below naming convention shall be followed for the Function/APIs/Interfaces Module: Name of the module to which the interface/function belongs. For the global functions, the module name shall be capital and for the local function, it shall be small. The global function prototype (declaration) shall be in its respective .h file.

What is the naming convention for global identifiers?

All the identifiers shall follow the below-naming convention. The naming convention consists of three fields separated by underscores (_) as shown below. Identifier names should not exceed 32 chars with the first 24 characters being unique. It is recommended to prefix the global identifiers with its module name.

What is the naming convention for ENUM in C?

C# naming conventions. ... Use a plural type name for an enumeration with bit fields as values, also called flags enum. Do not use an "Enum" suffix in enum type names. Do not use "Flag" or "Flags" suffixes in enum type names. Do not use a prefix on enumeration value names.


1 Answers

My standard is always PascalCase, also spell out the full word. I don't like abbreviations as they can have multiple meanings.

So, in your PascalCase scenario, I would spell out the 'Calc' word to be the following:

public static int Factorial(int n) {     return CalculateFactorial(n);      int CalculateFactorial(int number) => (number < 2)         ? 1         : number * CalculateFactorial(number - 1); } 

Compilers have come along ways, and a few extra bytes to make it clear what the method does is worth the few extra keystrokes.

like image 109
Chad Avatar answered Oct 05 '22 22:10

Chad