C# method readability

In C# is there a way to combine methods into groups? I have a class to communicate with a device. For each command I implemented, I made a method. This results in a few 100 methods. And for readability I would like to be able to combine these methods into sub groups so I could access them as this

DSP myDsp = new DSP(); 
uint32 serial = myDsp.MasterModule.GetSerialNr();  
string descriptor = myDsp.Eeprom.ReadDescriptor();

If I do this with a subclass then I lost all reference to the masterclass field and properties. For instance masterclass DSP holds a field address, if I make a subclass MasterModule then this class has no access to the address field.

Answers


use Fa├žade design pattern to group the functionality.


There is nothing built-in, but the following workarounds are commonly used:

  1. Prefix your method names, e.g. EepromReadDescriptor, so that IntelliSense puts them close together, and put them in partial classes to structure your source code.

  2. The subclass approach works as well, but you need to pass a reference to the required information (such as the address) to the constructor of the "subclass".


"This results in a few 100 methods" sounds to me like your class is doing too much so yes I agree you need to split it up. You'll most likely find there are certain functions that can be grouped (as you've mentioned) and that in reality you only need to pass in a few of the members of the main class for those functions to work. i.e. this data could be grouped into a separate class that could be passed into constructors. Its impossible to say any more without looking at your code in more detail, but in my view a little careful thought and design would result in a better structure for your code.


The answers make the point that you should split your class, but perhaps that is not possible. Instead you can group the methods using several interfaces:

 // Methods grouped by interface
 public interface IMasterModule
 {
     uint32 GetSerialNr();
 }

 public interface IEprom
 {
     string ReadDescriptor();
 }

 // Now for your huge class
 public class DSP : IMasterModule, IEprom
 {
     // Properties which expose the groups
     public IMasterModule MasterModule { get { return this; } }
     public IEprom        Eprom        { get { return this; } }

     // --------------- IMasterModule -------------

     // Now your methods, which have access to the main class
     public uint32 GetSerialNr()
     {
         ...
     }

     // ----------------- IEprom -----------------

     public string ReadDescriptor()
     {
        ...
     }

 }

Visually separating the code into groups is important. You could use #region instead, but comments with hypens are more visible.

Now to use those groups:

   DSP myDsp = new DSP(); 

   uint32 serial = myDsp.MasterModule.GetSerialNr();  
   string descriptor = myDsp.Eeprom.ReadDescriptor();

Need Your Help

Querying MySQL with Node.JS and display results in webpage

javascript php jquery mysql node.js

My goal is to query MySQL with Node.JS for better interactivity. My query is a simple SELECT with JOINs. I managed to construct a script that displays results in the console but I'm kind of stuck w...

Form validation and captcha

php html forms validation captcha

I have two issues with a contact form I have created. I was previously hit hard by spam.