«

»

Sep 23

Try it Another Way

How many ways can you reverse a string? An issue exists where the remaining solution is the first one made that works. This is rarely the right way to write code. We write code to solve problems. Just because a method is written and works does not mean it was the best way to solve the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private string Reverse1(string value)
{
    string newValue = "";
    for (int i = value.Length-1; i >= 0; i--)
    {
        newValue += value[i];
    }
    return newValue;
}
 
private string Reverse2(string value)
{
    if (string.IsNullOrWhiteSpace(value)) return "";
    var newValue = value.Substring(0, value.Length - 1);
    return value[value.Length - 1] + Reverse2(newValue);
}
 
private string Reverse3(string value)
{
    return new string(value.Reverse().ToArray());
}

I’m using these simple methods to reverse a string to show a point. These are not the only ways to reverse a string. The point is that are other ways. When a solution is made try rewriting it. Challenge yourself for other ways to solve it. What are better ways that use different techniques and/or more efficient. Perhaps a new Dependency Injection tool should be evaluated or learned. Perhaps your solution should make use of multi-threading. Don’t just stop with the first solution to the problem but instead master it.

hublot replica | replica watches | cartier replica sale | breitling replica sale