Readability of the code
-
Hi, there are two functions below that do the same thing. And the question is which one of them is more readable? Which one would you prefere? And why? Thanks in advance, Vitaliy version 1: __________________________________
private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario) { string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile; if (string.IsNullOrEmpty(profileKey)) { return null; } string parameterValue = scenario.IPHelper.Profile[profileKey]; if (string.IsNullOrEmpty(parameterValue)) { return null; } ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID); ContentParameterDetail pd = parameter.GetDetailbySPGWCode(parameterValue); return pd; }
version 2: __________________________________
private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario) { ContentParameterDetail pd = null; string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile; if(!string.IsNullOrEmpty(profileKey)) { string parameterValue = scenario.IPHelper.Profile[profileKey]; if (!string.IsNullOrEmpty(parameterValue)) { ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID); pd = parameter.GetDetailbySPGWCode(parameterValue); } } return pd; }
-
Hi, there are two functions below that do the same thing. And the question is which one of them is more readable? Which one would you prefere? And why? Thanks in advance, Vitaliy version 1: __________________________________
private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario) { string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile; if (string.IsNullOrEmpty(profileKey)) { return null; } string parameterValue = scenario.IPHelper.Profile[profileKey]; if (string.IsNullOrEmpty(parameterValue)) { return null; } ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID); ContentParameterDetail pd = parameter.GetDetailbySPGWCode(parameterValue); return pd; }
version 2: __________________________________
private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario) { ContentParameterDetail pd = null; string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile; if(!string.IsNullOrEmpty(profileKey)) { string parameterValue = scenario.IPHelper.Profile[profileKey]; if (!string.IsNullOrEmpty(parameterValue)) { ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID); pd = parameter.GetDetailbySPGWCode(parameterValue); } } return pd; }
I prefer version 1 cause there it's perfectly clear that null gets returned if
string.IsNullOrEmpty
is true. In version 2 I have to take a look at the whole method to see thatpd
isn't changed anywhere and therefor null will be returned.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hi, there are two functions below that do the same thing. And the question is which one of them is more readable? Which one would you prefere? And why? Thanks in advance, Vitaliy version 1: __________________________________
private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario) { string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile; if (string.IsNullOrEmpty(profileKey)) { return null; } string parameterValue = scenario.IPHelper.Profile[profileKey]; if (string.IsNullOrEmpty(parameterValue)) { return null; } ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID); ContentParameterDetail pd = parameter.GetDetailbySPGWCode(parameterValue); return pd; }
version 2: __________________________________
private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario) { ContentParameterDetail pd = null; string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile; if(!string.IsNullOrEmpty(profileKey)) { string parameterValue = scenario.IPHelper.Profile[profileKey]; if (!string.IsNullOrEmpty(parameterValue)) { ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID); pd = parameter.GetDetailbySPGWCode(parameterValue); } } return pd; }
I would go with version 1... You can actually see when it returns null, in version 2 you really have to read all the code to know when it returns null...
Don't you also love the code?
-
I prefer version 1 cause there it's perfectly clear that null gets returned if
string.IsNullOrEmpty
is true. In version 2 I have to take a look at the whole method to see thatpd
isn't changed anywhere and therefor null will be returned.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
Yes, good point. I just wonder if those multiple return statements are bad. Otherwise I defenitely go with version 1. Vitaliy
-
I would go with version 1... You can actually see when it returns null, in version 2 you really have to read all the code to know when it returns null...
Don't you also love the code?
Yes, Stefan in above reply also pointed that out. OK, I go with version 1. I LOVE THE CODE! :-D Vitaliy
-
Yes, good point. I just wonder if those multiple return statements are bad. Otherwise I defenitely go with version 1. Vitaliy
Vitaliy Tsvayer wrote:
if those multiple return statements are bad
This is a controversial topic and you can find many comments on this when googling for "multiple returns" e.g. this one[^] by Bruce Eckel.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook