Click here to Skip to main content
15,888,454 members
Articles / All Topics

Refactoring real world application

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
23 Jul 2015CPOL2 min read 9.7K   8  
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 the
This is an old version of the currently published technical blog.

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:

//1. method
var result = _repository.GenerateTokenIdForApplyUrn(agentToken, urn);
if (!string.IsNullOrEmpty(result))
{
    return new ResultStatus() { Success = true, StringValue = result};
}
return UnableToGenerateToken();

//2.method
var result = _repository.GenerateTokenIdForResults(agentToken, requestId, clientId);
if (!string.IsNullOrEmpty(result))
{
    return new ResultStatus() { Success = true, StringValue = result };
}
return UnableToGenerateToken();


//3.method
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:

//1. method
return GenerateToken(() => _repository.GenerateTokenIdForApplyUrn(agentToken, urn));
//2. method
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.