Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it good form to expose derived values as properties?
    primarykey
    data
    text
    <p>I need to <strong>derive an important value given 7 potential inputs</strong>. <a href="http://en.wikipedia.org/wiki/Robert_Cecil_Martin" rel="nofollow noreferrer">Uncle Bob</a> urges me to avoid functions with that many parameters, so I've <a href="http://www.refactoring.com/catalog/extractClass.html" rel="nofollow noreferrer">extracted the class</a>. All parameters now being properties, I'm left with a calculation method with no arguments.</p> <p>“That”, I think, “could be a property, but I'm not sure if that's idiomatic C#.” </p> <p>Should I <strong>expose the final result as a property, or as a method with no arguments</strong>? Would the average C# programmer find properties confusing or offensive? What about the Alt.Net crowd? </p> <pre><code>decimal consumption = calculator.GetConsumption(); // obviously derived decimal consumption = calculator.Consumption; // not so obvious </code></pre> <p>If the latter: should I <strong>declare interim results as [private] properties</strong>, also? Thanks to heavy <a href="http://www.refactoring.com/catalog/extractMethod.html" rel="nofollow noreferrer">method extraction</a>, I have several interim results. Many of these shouldn't be part of the public API. Some of them could be interesting, though, and my expressions would look cleaner if I could access them as properties:</p> <pre><code>decimal interim2 = this.ImportantInterimValue * otherval; </code></pre> <p><strong>Happy Experiment Dept.:</strong></p> <p>While debugging my code in VS2008, I noticed that I kept hovering my mouse over the method calls that compute interim results, expecting a hover-over with their return value. After turning all methods into properties, I found that <strong>exposing interim results as properties greatly assisted debugging.</strong> I'm well pleased with that, but have lingering concerns about readability.</p> <p>The interim value declarations look messier. The expressions, however, are easier to read without the brackets. I no longer feel compelled to start the method name with a verb. To contrast: </p> <pre><code>// Clean method declaration; compulsive verby name; callers need // parenthesis despite lack of any arguments. decimal DetermineImportantInterimValue() { return this.DetermineOtherInterimValue() * this.SomeProperty; } // Messier property declaration; clean name; clean access syntax decimal ImportantInterimValue { get { return this.OtherInterimValue * this.SomeProperty; } } </code></pre> <p>I should perhaps explain that I've been coding in Python for a decade. I've been left with a tendency to spend extra time making my code easier to call than to write. I'm not sure the Python community would regard this property-oriented style as acceptably “Pythonic”, however:</p> <pre><code>def determineImportantInterimValue(self): "The usual way of doing it." return self.determineOtherInterimValue() * self.someAttribute importantInterimValue = property( lambda self =&gt; self.otherInterimValue * self.someAttribute, doc = "I'm not sure if this is Pythonic...") </code></pre>
    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.
    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