Code Smell - Badly Name Variables

Code Smell - Badly Name Variables

It can be easy to overlook the importance of having a good variable or method name. After all, it doesn't affect performance as the compiler will convert it to machine code. And it can sometimes be challenging to think of a good name. But without a good naming convention, it can be even harder for another engineer (or yourself in a few months) to read and understand what the code is doing. Self-explanatory methods and variables make code more intuitive, as developers can generally understand the code without going through it in detail.

Here's an example of badly named methods and variables in C#:

public class Product
{
    private string nm;
    private string mfg;
    private int pID;

    public Product(string name, string manufacturer, int productID)
    {
        nm = name;
        mfg = manufacturer;
        pID = productID;
    }

    public string gtnm()
    {
        return nm;
    }

    public string getmfg()
    {
        return mfg;
    }

    public int getID()
    {
        return pID;
    }

    public void updtmfg(string newMfg)
    {
        mfg = newMfg;
    }

    public void updtID(int newID)
    {
        pID = newID;
    }

    // ...many more badly named methods for various product-related tasks
}
Vague method names 

In this example, the variables nm, mfg, and pID are poorly named, and the methods gtnm(), getmfg(), getID(), updtmfg(), and updtID() do not follow the convention of using PascalCase for method names.

A better approach would be to use descriptive names for variables and follow naming conventions for methods. For example:

public class Product
{
    private string name;
    private string manufacturer;
    private int productId;

    public Product(string name, string manufacturer, int productId)
    {
        this.name = name;
        this.manufacturer = manufacturer;
        this.productId = productId;
    }

    public string GetName()
    {
        return name;
    }

    public string GetManufacturer()
    {
        return manufacturer;
    }

    public int GetProductId()
    {
        return productId;
    }

    public void UpdateManufacturer(string newManufacturer)
    {
        manufacturer = newManufacturer;
    }

    public void UpdateProductId(int newProductId)
    {
        productId = newProductId;
    }

    // ...other methods with appropriately named variables and methods
}
Clear method names 

In this revised example, variable names use descriptive names, and the method names follow the convention of using PascalCase for methods.