Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring switch statement for Data to different types of data
    primarykey
    data
    text
    <p>My mission is to refactor a switch statement that was poorly written (it makes the cyclomatic complexity spike). In short, there is a class that parses a file for various values. </p> <pre><code>class foo { //a sampling of the fields. Each have their appropriate property private string _name; private short _location; private int _lineNumber; private List&lt;string&gt; _siblings; internal foo (StreamReader reader) { _siblings = new List&lt;string&gt;() while (!reader.EndofFile) { switch (reader.ReadLine()) { case "Name": _name = reader.ReadLine(); break; case "Location": _location = short.Parse(reader.ReadLine()); break; case "Line Number": _lineNumber = int.Parse(reader.ReadLine()); break; case "Brother": case "Sister": _siblings.Add(reader.ReadLine()); break; //etc } } } //Other methods and such } </code></pre> <p>I have read up on the topic and while there seems to be plenty of help, it all seems to be pointing at the Strategy design pattern, which (I believe) would overkill my problem. In my project, there are multiple classes like this, with some of them having upwards of 25 case statements (so kudos to those who can come up with an idea of and interface or abstract class)</p> <p>I have thought about using a <code>Dictionary&lt;String, TValue&gt;</code> as described by <a href="http://simpleprogrammer.com/2010/08/17/pulling-out-the-switch-its-time-for-a-whooping/" rel="nofollow">John Sonmez</a>, but then what would TValue be?</p> <p>Any help would be greatly appreciated.</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.
    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