What do you call the constructor argument and member variables?

I don’t use the prefix to name the internal variables of the class (I know what some do, but I don’t start “why are you ...”). I just prefer it. The problem is that sometimes the same parameters are passed to the contructor, and I end up confused about what to call them. For example:

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

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

What are the names of XclassIdX and XclassNameX?

You can do the following:

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

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

Just not sure if this is a good idea or are there other more elgetic ways?

+7
c # naming-conventions
source share
8 answers

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

+17
source share

I just use the this approach; then all my variables and fields reflect their intentions. Do not do the X thing - it's just ugly; -p

+4
source share

In such cases, I do not think that any method is better than any other. But for the sake of other people who might need to maintain your code (and you yourself for that matter), I would say just choose one of the ways to do this and be consistent.

+3
source share

Short answer:

Prefix members and parameters with m_ "and" p_ "or" s_ "if the element is static.

Do not decorate properties or locals , and when you feel that you should call them the same (ignoring the case), resolve the conflict by prefixing the properties with "this". "

Explanation:

Consider that there are at least four (4) different categories of readable / assigned names that must be different: Local variables , Variable members (instance and static), Properties, and Parameters method. All four categories can be displayed in one block of code, so each of them needs clear distinctive characteristics.

A meaningful prefix can simultaneously distinguish between variables and reveal their scope, such as m_ (member), s_ (static), p_ (parameter), leaving public properties and local variables simple without prefixes and without worrying about case sensitivity. If for some reason you must specify a local object in the same way as a property, regardless of the case, then simply attach the "this" property.

Naming conflicts between Local variables and Parameters does not occur because they cannot be called the same (the compiler will catch a duplicate definition). The same applies to member variables and Properties . Parameters and members with the prefixes "p_" and "m_", respectively, will not conflict, and conflicts between unreadable locales and properties can be resolved by adding "this". to properties.

The alternatives to my suggestions are not very good: case-sensitive (a bad idea, because not all CLR languages ​​are case-sensitive), use underscores on their own (also bad because it might run into standards and the damn thing doesn't tell you) or use different names (can be time-consuming, complex and seemingly arbitrary).

+1
source share

Why not just add an underscore or single letter for your private member variables. If you do not like this idea, then what you did in your second block of code is fine.

0
source share

Put X on your options as if it were bad. The constructor is part of the open interface of your class, and this only confuses the users of your class. Some like prefix member variables with underscores, which are much better since they are only visible to the developer of your class. Otherwise, just use this as in your second example.

0
source share

m_foo for private users
Foo for public property
Foo for parameters and local variables.

I prefer to distinguish the block area from the elements of the extended scope, so I adhere to the prefix.

0
source share

You need to somehow separate them. Using "this." is a weak form of Hungarian notation, so you can bite a bullet and use underscores or "m_" characters.

I feel it too. it’s actually worse, because in extreme cases (that is, large methods) this is possible. will be left and the wrong variable will be pointed to it. I know that great methods are bad, but they are really found in real business applications with developers of diverse skills. Using a prefix for private fields will prevent this scenario.

It is tempting to claim that underscores can be forgotten just like "this." would. But this is not true, because the underscore changes the name of the variable, so the only place you could forget is the definition, not the use of the file in it. Whereas with that. prefix is ​​missing a compiler error if you forget it during use.

Whatever you choose, in this situation Hungarianism should not be avoided, whether you use it. or underline. In any case, this is not so bad, we are forced to conditionally use the Hungarian language for interface names, control names and, possibly, other places that I cannot think of now.

-one
source share

All Articles