Naming conventions in C# - Time & Space Complexity
We look at how following naming rules affects the speed of writing and reading code.
Does using good names change how fast the program runs?
Analyze the time complexity of the following code snippet.
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void PrintFullName()
{
Console.WriteLine($"{FirstName} {LastName}");
}
}
This code defines a class with properties and a method using standard naming styles.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated operations here.
- How many times: The code runs each statement once when called.
Since there are no loops or repeated steps, the time to run does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 simple steps |
| 100 | Still about 10 simple steps |
| 1000 | Still about 10 simple steps |
Pattern observation: The work stays the same no matter how big the input is.
Time Complexity: O(1)
This means the program runs in constant time, unaffected by input size.
[X] Wrong: "Using longer or more descriptive names slows down the program's running speed."
[OK] Correct: Naming only affects how humans read and write code, not how fast the computer runs it.
Understanding naming conventions shows you care about clear code, which helps teams work better together.
"What if we added a loop to create many Person objects? How would the time complexity change?"
Practice
Solution
Step 1: Understand class naming convention in C#
Classes in C# use PascalCase, where each word starts with a capital letter and no underscores are used.Step 2: Compare options with PascalCase
MyClass uses PascalCase correctly. my_class uses snake_case, myClass uses camelCase, and MYCLASS uses all uppercase (incorrect for classes).Final Answer:
MyClass -> Option AQuick Check:
Class names use PascalCase = MyClass [OK]
- Using camelCase for class names
- Using underscores in class names
- Using all uppercase for class names
Solution
Step 1: Recall local variable naming rules
Local variables in C# use camelCase, starting with a lowercase letter and no underscores.Step 2: Match options to camelCase
localVariable uses camelCase correctly. LocalVariable uses PascalCase, LOCALVARIABLE uses all uppercase, and local_variable uses underscores.Final Answer:
localVariable -> Option AQuick Check:
Local variables use camelCase = localVariable [OK]
- Starting local variables with uppercase letters
- Using underscores in local variable names
- Using all uppercase for local variables
const int MAX_COUNT = 10; int maxCount = 5; Console.WriteLine(MAX_COUNT); Console.WriteLine(maxCount);
Solution
Step 1: Identify constant and variable values
MAX_COUNT is a constant with value 10, maxCount is a variable with value 5.Step 2: Understand Console.WriteLine outputs
Console.WriteLine(MAX_COUNT) prints 10, Console.WriteLine(maxCount) prints 5.Final Answer:
10 and 5 -> Option BQuick Check:
Constants print their value = 10, variables print their value = 5 [OK]
- Confusing constant and variable values
- Expecting names to print instead of values
- Thinking naming affects output values
public class exampleClass {
public int MyProperty { get; set; }
private int myField;
}Solution
Step 1: Check class naming convention
Class names must use PascalCase, but 'exampleClass' starts with lowercase.Step 2: Check property and field naming
Property 'MyProperty' uses PascalCase correctly; field 'myField' uses camelCase correctly.Final Answer:
Class name should be PascalCase -> Option CQuick Check:
Class names = PascalCase, exampleClass is incorrect [OK]
- Using lowercase for class names
- Confusing property and field naming rules
- Thinking camelCase is okay for classes
public const int max_users = 100;
public void UpdateUserCount(int CurrentUserCount) { }Solution
Step 1: Naming for constants
Constants use PascalCase, so MaxUsers is correct, max_users is incorrect.Step 2: Naming for method parameters
Method parameters use camelCase, so currentUserCount is correct, CurrentUserCount is incorrect.Final Answer:
MaxUsers and currentUserCount -> Option DQuick Check:
Constants PascalCase, parameters camelCase = MaxUsers, currentUserCount [OK]
- Using camelCase for constants
- Starting parameters with uppercase letters
- Using underscores in parameters
Ready to prove it?
Pass the test and this pattern moves unknown
