codeasy .net
or Sign Up
C# Elementary > 2 The Mission > C# naming convention

Calligraphy

As soon as I had finished the tasks, Ritchie introduced me to Jessica. She was tall and thin, and looked very strict. Her room was on the second floor of the underground resistance base. When she spoke she sounded surprisingly pleasant.
"Hi, Teo! Ritchie told me you are learning the new material quickly. Do you like it, or are you just doing whatever you need to survive in the future?" Jessica asked unexpectedly.
"The second one. I’m doing everything to survive, whether I like it or not. This is not my time and not my war."
"I understand you. However, here and now let’s act according to reality. We'd better talk about naming things in our programs. When you choose a name for a variable, how do you do it? What do you think about?"
"I just choose something that is easy to type. a, s and so on," I mumbled. I explained, "To be honest, I don’t think about variable names very much. I know that almost anything is allowed as a variable name and my program is going to work."
"That approach, to name all variables with one letter, may be the best if your priority is to save code writing time and computer memory," Jessica responded, "however, there is a problem with it. Take a look at this code snippet:"

int c = 20;
int m = 12;
int a = c * m;

"What is this code about?" my teacher asked.
"I have absolutely no idea. There are two int variables that you multiply, and then output the result as the "a" variable.
"There's the problem: this code gives you no idea what it's about. It's just some simple math. What do you think about this rewritten version?"

int customersPerMonth = 20;
int months = 12;
int annualCustomers = customersPerMonth * months;

"Now it looks like this code is calculating the annual number of customers," I answered. "This code does the same math operations as the previous one, but it is not abstract anymore because of the names."
"Exactly! Can you guess why you would take the time to choose longer variable names like 'customersPerMonth'? Why not stick with names like ‘c’, ‘m’ and ‘a’?" Jessica asked.
"It would be much easier for someone else to understand my code if I use representative names."
"Obviously! But that's not all. Any other guesses?" Jessica smiled.
It took me some time to come up with an answer to this question.
"Hm… If I read my code in a year or so, with proper naming it will also be much easier for me to remember what I meant to do with it."
"That's right. Now that we've agreed to use variable names that describe what the variables represent, let’s choose what is called the coding convention. It is a set of rules about naming variables and formatting the code. There are a lot of different naming conventions among programmers. Here are some ways of naming the variable that represents the number of customers per month:"

int customersPerMonth = 20;
int customers_per_month = 20;
int CustomersPerMonth = 20;
int _customersPerMonth = 20;
int CUSTOMERS_PER_MONTH = 20;

Variables naming

The Rules

"Right now I’m going to give you a set of rules that you should use in all your C# programs. Rule #1 is just common sense, avoiding something that is invalid and should never be used in any program. Most of the others you will follow so that all our programs are consistent, clear, easy to read, and easy to understand.

  1. You cannot create two or more variables with the same names!

    Obviously this is not going to work:

    int apples = 5;
    int apples = 10;
    

    It doesn't work even if they are different kinds of variables:

    int people = 5;
    string people = "People";
    
  2. Your local variable names will start with lower case, and each extra word in the name will start with an uppercase letter, with no spaces or other characters. Like this: customersPerMonth. This is called lower camel case.
  3. Choose clear and readable names.
  4. Write only one statement per line.

    Avoid writing:

    int months = 12; Console.WriteLine(months);
    

    Instead use one statement per line:

    int months = 12;
    Console.WriteLine(months);
    
  5. Use 4 spaces indentation.

    As soon as a new block {} starts, use 4 spaces:

    public static void Main()
    {
        int result = 0;
        int addition = 12;
        Console.WriteLine(result + addition);
    }
    

    And this is how you SHOULD NOT write code:

    public static void Main()
    {
    int result = 0;
          int addition = 12;
     Console.WriteLine(result+ addition);
    }
    
  6. The first character of the variable name should be any letter character or an underscore “_” and nothing else:

    Valid:

    int _a = 12;
    int big = 10000;
    int bIG = 10000;
    

    Invalid:

    int 1a = 12;
    int 756b = 10000;
    int !@#dominator = 666;
    

    Try to understand the difference between valid and readable variable names. A valid variable name can work in a program but still not be what we call readable (it may give no useful information). We are aiming to have names that are both valid and readable:

    int _a = 12;        // Valid but not readable - "_a" could be anything
    int catsCount = 12; // Valid and readable
    
  7. You can’t use a keyword as a variable name. Keywords are reserved words for types and other language structures. For example:
    string int = "23";
    

    is invalid because int is a keyword in C#. You have to follow this rule or the program won't work at all.

  • If you want more information on good coding practices, complete C# code convention guides are available here (Coding conventions) and here (more about naming conventions). If you notice any difference, stick to our rules while you are here and coding for us.
  • As soon as you learn new C# elements, I will tell you our code standards regarding them, so that your code remains beautiful," Jessica finished her monologue.
    "Wow, that’s lots of rules to remember!" I said.
    "Don’t worry; usually, you don’t need to bother about naming restrictions of C# because to name variables with English letters, using lower camel case, is usually enough. You don't need to create variables with names like !@#dominator, right?"
    "Right! What’s next?" I asked.
    "Practice, of course!"

    Variables naming

    Create five int variables. Name them with a lower camel case representation of these strings:
    Number of cars
    Money amount
    Plane speed
    Hole depth
    Age

    Example:
    int numberOfCars;
    etc...



    Make the program compile (work). It should output "The hardest is naming. I can try 100 names and still not find a good one!"



    Change any invalid variable names to valid ones. Make other corrections needed so the program will compile (work) and output: "It is now 2113. We are having a war with machines. I actually don't care about the current situation. I just want to go home."



    Change names in the program to correspond to C# naming conventions for local variables. Don't change anything else.

    C# Elementary > 2 The Mission > C# naming convention

    This site uses cookies. By continuing to browse this site you are agreeing to our use of cookies.

    Got it!