Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The SRP applies to classes, not methods. Still, it's a good idea to have methods that do one thing only. But you can't take that to extremes. For example, a console application would be fairly useless if its Main method could contain only one statement (and, if the statement is a method call, that method could also contain only one statement, etc., recursively).</p> <p>Think about the implementation of IsNullOrEmpty:</p> <pre><code>static bool IsNullOrEmpty(string s) { return ReferenceEquals(s, null) || Equals(s, string.Empty); } </code></pre> <p>So, yes, it's doing two things, but they're done in a single expression. If you go to the level of expressions, <em>any</em> boolean expression involving binary boolean operators could be said to be "doing more than one thing" because it is evaluating the truth of more than one condition.</p> <p>If the names of the methods bother you because they imply too much activity for a single method, wrap them in your own methods with names that imply the evaluation of a single condition. For example:</p> <pre><code>static bool HasNoVisibleCharacters(string s) { return string.IsNullOrWhitespace(s); } static bool HasNoCharacters(string s) { return string.IsNullOrEmpty(s); } </code></pre> <p>In response to your comment:</p> <blockquote> <p>say I wrote the function like SerilizeAndValidate(ObjectToSerilizeAndValidate) , clearly this method / class , is doing 2 things , Serialize and Validation, clearly a violation , some time methods in a class leads to maintenance nightmare like above example of serialize and validation </p> </blockquote> <p>Yes, you are right to be concerned about this, but again, you cannot literally have methods that do one thing only. Remember that different methods will deal with different levels of abstraction. You might have a very high-level method that calls <code>SerializeAndValidate</code> as part of a long sequence of actions. At that level of abstraction, it might be very reasonable to think of <code>SerializeAndValidate</code> as a single action.</p> <p>Imagine writing a set of step-by-step instructions for an experienced user to open a file's "properties" dialogue:</p> <ul> <li>Right-click the file</li> <li>Choose "Properties"</li> </ul> <p>Now imagine writing the same instructions for someone who's never used a mouse before:</p> <ul> <li>Position the mouse pointer over the file's icon</li> <li>Press and release the right mouse button</li> <li>A menu appears. Position the mouse pointer over the word "Properties"</li> <li>Press and release the left mouse button</li> </ul> <p>When we write computer programs, we need to operate at <em>both</em> levels of abstraction. Or, rather, at any given time, we're operating at one level of abstraction or another, so as not to confuse ourselves. Furthermore, we rely on library code that operates at lower levels of abstraction still.</p> <p>Methods also allow you to comply with the "do not repeat yourself" principle (often known as "DRY"). If you need to both serialize and validate objects in many parts of your application, you'd want to have a <code>SerializeAndValidate</code> method to reduce duplicative code. You'd be very well advised to implement the method as a simple convenience method:</p> <pre><code>void SerializeAndValidate(SomeClass obj) { Serialize(obj); Validate(obj); } </code></pre> <p>This allows you the convenience of calling one method, while preserving the separation of serialization logic from validation logic, which should make the program easier to maintain.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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