Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would you name these related Property, Class, Parameter and Field in .NET?
    primarykey
    data
    text
    <p>I often find I want to write code something like this in C#, but I am uncomfortable with the identifier names:</p> <pre><code>public class Car { private Engine engine; public Engine Engine { get { return engine; } set { engine = value; } } public Car(Engine engine) { this.engine = engine; } } </code></pre> <p>Here we have four different things called "engine":</p> <ul> <li><code>Engine</code> the class. Engine seems like a good, natural name.</li> <li><code>Engine</code> the public property. Seems silly to call it MyEngine or TheCarsEngine.</li> <li><code>engine</code> the private field backing the property. Some naming schemes will recommend <code>m_engine</code> or <code>_engine</code>, but others say that all prefixes should be avoided.</li> <li><code>engine</code> the parameter name on the constructor. I've seen naming schemes that recommend prefixing an underscore on all parameters, e.g., <code>_engine</code>. I really dislike this, since the parameter is visible to callers via Intellisense.</li> </ul> <p>The particular things I don't like about the code as written are that:</p> <ul> <li>If you change the parameter name in the constructor but miss a use of it in the constructor body, you get a subtle bug that the compiler probably won't be able to spot.</li> <li>Intellisense has a bad habit of autocompleting the wrong thing for you, and sometimes you won't notice it's changed the case. You will again get a subtle bug if the constructor body accidentally ends up <code>this.engine = Engine;</code></li> </ul> <p>It seems that each name is appropriate in isolation, but together they are bad. Something has to yield, but what? I prefer to change the private field, since it's not visible to users, so I'll usually end up with <code>m_engine</code>, which solves some problems, but introduces a prefix and doesn't stop Intellisense from changing <code>engine</code> to <code>Engine</code>.</p> <p>How would you rename these four items? Why?</p> <p>(Note: I realise the property in this example could be an automatic property. I just didn't want to make the example overcomplicated.)</p> <p>See also: <a href="https://stackoverflow.com/questions/461231/am-i-immoral-for-using-a-variable-name-that-differs-from-its-type-only-by-case">Am I immoral for using a variable name that differs from its type only by case?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload