Today I’d like to share with you bit of refactoring.
Class name: ExternalAccessService
methods to refactor:
1. GenerateTokenForApply
2. GenerateTokenForResults
3. GenerateTokenForClient
We don’t need all details of these methods but what is interesting about them is that every one of them is generating token. And the way they do that is very similar:
var result = _repository.GenerateTokenIdForApplyUrn(agentToken, urn);
if (!string.IsNullOrEmpty(result))
{
return new ResultStatus() { Success = true, StringValue = result};
}
return UnableToGenerateToken();
var result = _repository.GenerateTokenIdForResults(agentToken, requestId, clientId);
if (!string.IsNullOrEmpty(result))
{
return new ResultStatus() { Success = true, StringValue = result };
}
return UnableToGenerateToken();
var result = _repository.GenerateTokenForClient(agentToken, clientId);
if (!string.IsNullOrEmpty(result))
{
return new ResultStatus() { Success = true, StringValue = result };
}
return UnableToGenerateToken();
Lots of rows of code that does same thing with different parameters. So I thought that it is great opportunity for refactoring.
And my result is here:
private ResultStatus GenerateToken(Func<string> func)
{
string result = func.Invoke();
if (!string.IsNullOrEmpty(result))
{
return new ResultStatus() { Success = true, StringValue = result };
}
return UnableToGenerateToken();
}
And from 6 lines of code in every method we have only one:
return GenerateToken(() => _repository.GenerateTokenIdForApplyUrn(agentToken, urn));
return GenerateToken(() => _repository.GenerateTokenIdForResults(agentToken, requestId, clientId));
3.method
return GenerateToken(() => _repository.GenerateTokenForClient(agentToken, clientId));
However don’t be mistaken, refactoring is not just about reducing number of lines of code. It is also about re-thinking usage of your code. Re-using your existing code etc…
Another example of refactoring is ResultStatus class
Class name: ResultStatus
property to refactor: Value
Before refactoring the class was used to return status from service layer of application.
Example of class:
public class ResultStatus
{
private List errors = new List();
public bool Success { get; set; }
public string stringValue { get; set; }
public int intValue { get; set; }
public List Errors {
get { return errors; }
set { errors = value; }
}
}
Evidently for every different type of value property we would have to add another property with that type. For example public decimal decimalValue etc….
Solution to this problem is refactioring and using c# Generics.
After refactoring:
public class ResultStatus<T>
{
private List<string> errors = new List<string>();
public bool Success { get; set; }
public T Value { get; set; }
public List<string> Errors {
get { return errors; }
set { errors = value; }
}
}
Now we don’t have to use different properties to get value we will just initialize our class with type that we expect:
return new ResultStatus<string>() { Success = true, Value = result };
That’s it. Hope you liked my examples and I hope you will also refactor your applications and make them better and more readable for yourself and your colleagues.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.