Note that there are some explanatory texts on larger screens.

plurals
  1. POFunc<> or method in test code?
    primarykey
    data
    text
    <p>I saw this loop in test code:</p> <pre><code>foreach ( StuffId Id in Result.GetIdList() ) { if ( Id.Level == 3 ) { Level3Id = Id.ToString(); } if ( Id.Level == 5 ) { Level5Id = Id.ToString(); } } </code></pre> <p>Other tests imply that either there is only one Id for each level or when there are multiples for each level then the Id will be the same.</p> <p>Being slightly obsessed with LINQ right now, I first refactored to this:</p> <pre><code>IEnumerable&lt;StuffId&gt; Ids = Result.GetIdList(); Level3Id = Ids.Where( x =&gt; x.Level == 3 ).First().Id.ToString(); Level5Id = Ids.Where( x =&gt; x.Level == 5 ).First().Id.ToString(); </code></pre> <p>Then the code repetition bothered me so I refactored to this:</p> <pre><code>IEnumerable&lt;StuffId&gt; Ids = Result.GetIdList(); Func&lt;int,string&gt; IdFromLevel = level =&gt; Ids.Where( x =&gt; x.Level == level ).First().Id.ToString(); Level3Id = IdFromLevel(3); Level5Id = IdFromLevel(5); </code></pre> <p>A colleague wondered why I didn't use a method in place of the delegate. My reasoning is a method would be slightly more 'messy' because I'd have to additionally pass in the collection and that using a delegate is no big deal for a simple test (for which terse, readable and no branching are good qualities).</p> <p>I had a look on SO, of course, and found this seemingly relevant question:</p> <p><a href="https://stackoverflow.com/questions/7172038/c-func-instead-of-methods">C#: Func&lt;> instead of methods?</a></p> <p>where the consensus seems to favour a method over a delegate. Does the same apply in my case?</p>
    singulars
    1. This table or related slice is empty.
    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