Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.IsNullOrEmpty(myString) or string.IsNullOrWhiteSpace(myString) is not violating SRP Rule?

As the question shows,

As we are using string functions like IsNullOrEmpty or IsNullOrWhiteSpace as the name of functions shows , these are doing more than one job , is it not a violation of SRP?

rather should it not be string.isValid(Enum typeofValidation) than using strategey pattern to choose the correct strategey to validate.

or is it perfectly OK to violate SRP in utilities class or static classes.

like image 282
TalentTuner Avatar asked Mar 06 '12 15:03

TalentTuner


1 Answers

The SRP says that a function or class should have only one reason to change. What is a reason to change? A reason to change is a user who requests changes. So a class or function should have only one user who requests changes.

Now a function that does some calculations and then some formatting, has two different users that could request a change. One would request changes to the calculations and the other would request changes to the formatting. Since these users have different needs and will make their requests and different times, we'd like them to be served by different functions.

IsNullOrEmpty(String) is not likely to be serving two different users. The user who cares about null is likely the same user who cares about empty, so isNullOrEmpty does not violate the SRP.

like image 77
Robert Martin Avatar answered Nov 09 '22 06:11

Robert Martin