Flower 3: Who needs SRP anyway

flower - Paradise bird
coding flower

Single Responsibility Principle is one of the core guidelines for object-oriented programming. In short, it means one class has does one thing only. Same for methods it does exactly one thing stated in its name.
Let us look at Method from the previous test ’GetLoggedUserName.’
insert method here

Here we cannot act as mechanically we need to think what author wanted to do? (Note I have used this part as an example in my article about experiments – you can read more here)

So when the element is not displayed string text return string empty, and they want null. But why? We need to find the usage of ’GetLoggedUserName.’

[csharp]
[TestMethod]
public void TestLoginInput_username()
{
const string expectedLogin = "Jan";
//
// some code performed here
//
var loggedUserName = mainPage.GetLoggedUserName();

Assert.IsNotNull(loggedUserName);
Assert.AreEqual(expectedLogin, loggedUserName);
}
[/csharp]

Ok to be honest code looked more like this:

[csharp]
[TestMethod]
public void TestLoginInput_username_how_it_acully_looked()
{
var result = WorkflowAuth.WorkflowCheckedLoggeduserInfo(irrelevantParameter1, irrelevantParameter2,
irrelevantParameter3,
//more sensles paramiter
"Jan");
Assert.IsTrue(result);
}
[/csharp]

[csharp]
public static class WorkflowAuth
{
public static bool WorkflowCheckedLoggeduserInfo(string irrelevantParameter1,
string irrelevantParameter2,
string irrelevantParameter3,
string irrelevantParameter4,
string irrelevantParameter5,
string irrelevantParameter6,
string irrelevantParameter7,
string expectedName)
{
var uselessif = new UselessIfsPage();
//
// flow does somethinh
//

var loggedUserName = uselessif.GetLoggedUserName();
if (loggedUserName == null)
{
return false;
}

if (loggedUserName != expectedName)
{
return false;
}
return true;

}
}
[/csharp]

But it is unreadable we will talk about few things we see here but not today.

So he is first checking if an element is not displayed by checking if it is null. In short, it is sneaking assertion.
By this method, the author wants to check if an element is displayed. So when you get a letter null instead of value, he will know that element wasn’t shown.

The problem is that this will be obvious to him but not to rest of us.
Your goal always should be failure reason as readable as possible.
You don’t want to do debug to see what is broken. And here you have to.
The author should do the first assertion that element is displayed before he does test for this text.

Small fix

Then he can get the more reliable result. Something like this:

[csharp]
[TestMethod]
public void TestLoginInput_username()
{
const string expectedLogin = "Jan";
//
// some code performed here
//

Assert.IsTrue(mainPage.IsLoggedUserLabekDisplayed(), "loged user name label is not displayed);
var loggedUserName = mainPage.GetLoggedUserName();
Assert.AreEqual(expectedLogin, loggedUserName);
}
[/csharp]

Summary

Following SRP will help us avoid many nasty surprises. Again this will also make your code more readable and maintainable.
This case we looked today was much more complicated than previous ones. I wasn’t even sure if this story should be about SRP,
But still, I am glad we did tackle it.

Dodaj komentarz