Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Which do you like better? Class #1:

public class SearchClass
{
    public SearchClass (string ProgramName)
    { /* Searches LocalFile objects, handles exceptions, and puts results into m_Results. */ }

    DateTime TimeExecuted;
    bool OperationSuccessful;

    protected List<LocalFile> m_Results;
    public ReadOnlyCollection<LocalFile> Results
    {
        get { return new ReadOnlyCollection<LocalFile>(m_Results); }
    }

    #region Results Filters
    public DateTime OldestFileModified
    {
        get { /* Does what it says. */ }
    }

    public ReadOnlyCollection<LocalFile> ResultsWithoutProcessFiles()
    {
        return new ReadOnlyCollection<LocalFile> ((from x in m_Results
                                                   where x.FileTypeID != FileTypeIDs.ProcessFile
                                                   select x).ToList());
    }
    #endregion
}

Or class #2:

public class SearchClass
{
    public SearchClass (string ProgramName)
    { /* Searches LocalFile objects, handles exceptions, and puts results into m_Results. */ }

    DateTime TimeExecuted;
    bool OperationSuccessful;

    protected List<LocalFile> m_Results;
    public ReadOnlyCollection<LocalFile> Results
    {
        get { return new ReadOnlyCollection<LocalFile>(m_Results); }
    }

    public class SearchResults : ReadOnlyCollection<LocalFile>
        {
        public SearchResults(IList<LocalFile> iList) : base(iList) { }

        #region Results Filters
        public DateTime OldestFileModified
        {
            get { /* Does what it says. */ }
        }

        public ReadOnlyCollection<LocalFile> ResultsWithoutProcessFiles()
        {
            return new ReadOnlyCollection<LocalFile> ((from x in this
                                                       where x.FileTypeID != FileTypeIDs.ProcessFile
                                                       select x).ToList());
        }
        #endregion
    }
}

...with the implication that OperationSuccessful is accompanied by a number of more interesting properties on how the operation went, and OldestFileModified and ResultsWithoutProcessFiles() also have several more siblings in the Results Filters section.

share|improve this question
    
Better over at SO? –  John Mar 7 '11 at 21:03
    
I thought it seemed like a fairly subjective question. :) Edited wording to make solicitation of preference more explicit. –  Calvin Fisher Mar 8 '11 at 14:28
add comment

2 Answers

up vote 1 down vote accepted

As always, these things are a matter of taste. In this case, I'm not crazy about the second solution for a few reasons:

  1. There's always a small intellectual burden when defining classes and their relationships. It's not clear what benefit the nested class provides aside from partitioning code. The one class approach is simple and clear.
  2. I'm not crazy about nested public classes. This implies I should be able to instantiate it outside of the SearchClass context which doesn't make a lot of sense.
  3. Inheriting generic collections isn't my favorite. When I run into this class, it's not immediately clear that it's just a plain old ReadOnlyCollection<LocalFile>. I don't like the extra step of remembering what's behind the curtain.

Additionally, you may want some sort of static method to create your SearchClass. In your example, the class runs processing in the constructor which doesn't allow it to gracefully fail. The only option is to throw an exception. If you use a static method, you have the option of returning null on failure (or some other alternative failure scheme).

share|improve this answer
add comment

I follow the KISS principle (Keep it simple, stupid) So the first one to me is the more preferable of the two. I don't like nested classes at all, plus I know having alot of methods in one class can get pretty long sometimes, but to me it's easier searching for those methods than looking in multiple sub-classes.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.