Naming Things in Program
Tags: programming, computerscience, cleancode, codequality
Published: 2023-01-15 13:36:36

#### _There are only two hard things in Computer Science: cache invalidation and naming things._  
_-- Phil Karlton_
<br/>

Whether Phil said this quote or not. In my opinion, I totally agree with the naming of things, especially since English ain't my mother language. 

Good naming would let your objects proud to ask: "Say my name", and speak loudly like Mr. White in Breaking Bad. So how to name things? There are some concepts that come below.
<br/>
<br/>

## 1. Don't abbreviate names

There is a classic example, you shouldn't name variables with a single letter. That won't tell you anything about the variables, for instance.

:x: int y = 3;
:white_check_mark: int seconds = 3;
<br/>

For class or function naming, abbreviating will easily make your team member or other developers who can't realize it at first sight, even yourself(After a few days you are back to check your code). See these instances below.

:x: abbreviate 
```cs
Public void MoveOnPg()
```
:white_check_mark: not abbreviate 
```cs
Public void MoveOnPage()
```
<br>
<br>
 

## 2. Don't put types in variable names


Many books before usually recommend putting the type in the prefix of the variable, but now with statically type languages, we don't need to do that.

:x: type variable

```cs
bool bIsValid;
int32_t iCount;
uint32_t uNumUsers;
```

:white_check_mark: variable
```cs
bool IsValid;
int32_t Count;
uint32_t NumUsers;
```
<br>
<br>

## 3. Add units to variables unless the type tells you

The parameters of the function, adding units are clear to whom to use it.

:x: not type variable
```cs
void progress(int overdue)
```

:white_check_mark: type variable
```cs
void progress(int overdueSeconds)
```


Further, we can add the type instead for this case in C#, and here getting seconds.

```cs
void progress(TimeSpan overdue) {
     var seconds = overdue.TotalSeconds;
}
```
<br>
<br>
## 4. Don't put types in your types


There is very common to see people to put types in the types, in particular the pattern in C#, for instance, adding the prefix in interface with "I".

```cs
public interface IService
{
    void Configure(IConfiguration configuration);
}
```

Most of the time people don't care about whether it's an interface, class, or abstract class, they just want to know what they can use, Absolutely, for C# developers it quite makes sense to follow the pattern, since everyone does that, but for other languages, we shall avoid to do it.

Another example is AbstractX, BaseX

```cs
class Car: BaseCar
{
    private const double power = 225;
    private const double acceleration= 6.1;

    public Car(): base(power, acceleration)
    {
       ...
    }
}
```
It's good to rename the "BaseCar" to "Vehicle", and   we can also make the child vehicle become a especific vehicle: Sedan.

```cs
class Sedan: Vehicle
{
    private const double power = 225;
    private const double acceleration= 6.1;

    public Sedan(): base(power, acceleration) 
    {
       ...
    }
}
```
<br>
<br>
## 5. Refactor if you find yourself naming code "Utils" or "Utilities"

The class "Utils" or "Utilities", isn't a good name to make customers understand what this class can do, until they dig inside, see below.

```cs
public class Utils
{
    int number;

    public Utils()
    {
    }

    public int sum(int number1, int number2)
    {
        number = number1 + number2;
    }
    return number;
}
```

Actually, we don't need this "Utils" class, for the functional we can move this out, and create a sum class instead.

```cs
public class Sum
{
    private int result;

    public int Result {
       get {
           return result;
       }
    }

    public Sum(int firstNumber) {
        result = firstNumber;
    }

    public void Add(int number) {
        result += number;
    }
}
```

When you find there is class naming like this, rename it, or refactor it, hence, don't name code "Utils".
<br/>

After reference these concepts, I'd like to say :sunglasses:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9msbwhuf2skzlys1mwd.gif)
<br/>

Thanks for reading, and happy coding:metal:!
<br/>
<br/>
###### Reference: https://youtu.be/-J3wNP6u5YU