Hi all.

I'm reading about SOLID way of coding and am only looking at single Responsibility of objects (S).

I will be creating a string helper class and I'm wondering if I have two methods or more in the class, if I am still adhereing to SR if the methods only have one responsibility or is it that I should have two helper classes each with only on method?

for example

class String
    public string Between(string src, string start, string end)
        //some code to return a string from src

    public string Outside(string src, string target)
        //some code to return a string from src

Thanks for reading.

Edited by Suzie999

1 Year
Discussion Span
Last Post by JamesCherrill

Every rule has its exceptions, and this is one. It's very common that you end up with a bag full of assorted utility methods that are low-level things used in various different parts of your system. A literal interpretation of SOLID says you should put each one in a different class, but all that achieves is extra clutter in your name spaces.
Although I am a total believer in SOLID, I still think it's OK to have a class that holds miscellaneous utility methods that belong together because of some similarity of use and do not belong anywhere else.
SOLID exists for one reason only - to make code easier to maintain. The question to ask youself is "by deviating from the strict defintion of SOLID am I improving or harming the maintainability of my code?".


I was thinking along similar lines, thanks.

I have a WebTools class with many helper method I am going to adapt solid principles, three of them retrieve the source code of a web page but in different fashions, so I believe I can contain those 3 in same class. While they might rely on different parts of dot net, none rely on each other. So although there are 3 methods, the class is only responsible for returning a web page as a string.


That makes total sense to me. You may also find that those 3 methods contain some functionality that is common to all 3 and which can therefore be factored out into shared methods - but this only works cleanly when they are all in the same class.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.