Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you name constructor argument and member variables?

I don't use prefix while naming internal variables of a class (I know some do but I am not starting "why do you..." debate). I just prefer it that way. The problem is that sometimes same parameters are passed in contructor and I end up being confused how to name them. For example:

public class SampleClass
{
    private int classId;
    private string className;

    public SampleClass (int XclassIdX, string XclassNameX) {
        classId = XclassIdX;
        className = XclassNameX;
    }
}

How to name XclassIdX and XclassNameX?

One thing that can probably be done is:

public class SampleClass
{
    private int classId;
    private string className;

    public SampleClass (int classId, string className) {
        this.classId = classId;
        this.className = className;
    }
}

Just not sure whether this is a good idea or there are other more elgant ways?

like image 764
Hemant Avatar asked Mar 20 '09 12:03

Hemant


People also ask

How do you name a constructor parameter?

Parameter NamesThe name of a parameter must be unique in its scope. It cannot be the same as the name of another parameter for the same method or constructor, and it cannot be the name of a local variable within the method or constructor. A parameter can have the same name as one of the class's fields.

How do you initialize a data member in a constructor?

Const member variables must be initialized. A member initialization list can also be used to initialize members that are classes. When variable b is constructed, the B(int) constructor is called with value 5. Before the body of the constructor executes, m_a is initialized, calling the A(int) constructor with value 4.

Where are the member variables of a class initialized?

Member variables are always initialized in the order they are declared in the class definition.


4 Answers

I think the solution you're describing, where the constructor parameters are identically named and you prefix the class members with this, is just fine. It's clear, it's concise, and there is no confusion about what you meant.

like image 97
John Feminella Avatar answered Oct 14 '22 07:10

John Feminella


I simply use the this approach; then all my variables and fields reflect their intent. Don't do the X thing - that is just ugly ;-p

like image 41
Marc Gravell Avatar answered Oct 14 '22 07:10

Marc Gravell


Short answer:

Prefix members and parameters with "m_" and "p_", or "s_" if member is static.

Don't decorate properties or locals, and when you feel you must name them the same (ignoring case), resolve the conflict by prefixing properties with "this.".

Explanation:

Consider that there are at least FOUR(4) different categories of readable/assignable names that need distinguishing: Local variables, Member variables (instance and static), Properties, and method Parameters. All four categories may appear in a single code block, and therefore they each need clear distinguishing characteristics.

A meaningful prefix can simultaneously distinguish variables and reveal their scope such as m_(member), s_(static), p_(parameter), leaving public properties and local variables to remain simple without prefixes and without worrying about case sensitivity. If for some reason you must name a local the same as a property without regard to case, then simply prefix the property with "this."

Naming conflicts between Local variables and Parameters don't occur, because they cannot be named the same thing (compiler will catch the duplicate definition). The same goes for Member variables and Properties. Parameters and members prefixed with "p_" and "m_" respectively will not conflict, and conflicts between non-prefixed locals and properties can be resolved by adding "this." to the properties.

The alternatives to my suggestions are not pretty: use case sensitivity (bad idea, as not all CLR languages are case sensitive), use underscores by themselves (also bad, as it may collide with standards and doesn't tell you a damn thing), or use different names altogether (can be time consuming, difficult, and appear arbitrary).

like image 38
Triynko Avatar answered Oct 14 '22 07:10

Triynko


I cases like this, I don't think any one way is better than any other. But for the sake of other people who might need to maintain your code (and yourself for that matter) I'd say just pick one way of doing it and be consistent about it.

like image 43
Eric Petroelje Avatar answered Oct 14 '22 06:10

Eric Petroelje