Class Design -- Multiple Calls from One Method or One Call from Multiple Methods?

Posted by Andrew on Programmers See other posts from Programmers or by Andrew
Published on 2012-05-02T15:14:56Z Indexed on 2013/10/21 22:03 UTC
Read the original article Hit count: 181

Filed under:
|

I've been working on some code recently that interfaces with a CMS we use and it's presented me with a question on class design that I think is applicable in a number of situations. Essentially, what I am doing is extracting information from the CMS and transforming this information into objects that I can use programatically for other purposes. This consists of two steps:

  1. Retrieve the data from the CMS (we have a DAL that I use, so this is essentially just specifying what data from the CMS I want--no connection logic or anything like that)
  2. Map the parsed data to my own [C#] objects

There are basically two ways I can approach this:

One call from multiple methods

public void MainMethodWhereIDoStuff()
{
    IEnumerable<MyObject> myObjects = GetMyObjects();
    // Do other stuff with myObjects
}

private static IEnumerable<MyObject> GetMyObjects()
{
    IEnumerable<CmsDataItem> cmsDataItems = GetCmsDataItems();
    List<MyObject> mappedObjects = new List<MyObject>();
    // do stuff to map the CmsDataItems to MyObjects
    return mappedObjects;
}

private static IEnumerable<CmsDataItem> GetCmsDataItems()
{
    List<CmsDataItem> cmsDataItems = new List<CmsDataItem>();
    // do stuff to get the CmsDataItems I want
    return cmsDataItems;
}

Multiple calls from one method

public void MainMethodWhereIDoStuff()
{
    IEnumerable<CmsDataItem> cmsDataItems = GetCmsDataItems();
    IEnumerable<MyObject> myObjects = GetMyObjects(cmsDataItems);
    // do stuff with myObjects
}

private static IEnumerable<MyObject> GetMyObjects(IEnumerable<CmsDataItem> itemsToMap)
{
    // ...
}

private static IEnumerable<CmsDataItem> GetCmsDataItems()
{
    // ...
}

I am tempted to say that the latter is better than the former, as GetMyObjects does not depend on GetCmsDataItems, and it is explicit in the calling method the steps that are executed to retrieve the objects (I'm concerned that the first approach is kind of an object-oriented version of spaghetti code).

On the other hand, the two helper methods are never going to be used outside of the class, so I'm not sure if it really matters whether one depends on the other. Furthermore, I like the fact that in the first approach the objects can be retrieved from one line-- most likely anyone working with the main method doesn't care how the objects are retrieved, they just need to retrieve the objects, and the "daisy chained" helper methods hide the exact steps needed to retrieve them (in practice, I actually have a few more methods but am still able to retrieve the object collection I want in one line).

Is one of these methods right and the other wrong? Or is it simply a matter of preference or context dependent?

© Programmers or respective owner

Related posts about c#

Related posts about class-design