开发者

When my method is a Helper, Utility or should be turned into a Extension?

开发者 https://www.devze.com 2023-04-08 03:46 出处:网络
For example this encryption function could be called a Utility ? public static string HashText(string text)

For example this encryption function could be called a Utility ?

public static string HashText(string text)
{
    byte[] encodedBytes;
    using (var md5 = new MD5CryptoServiceProvider())
    {
        var originalBytes = Encoding.UTF8.GetBytes(text);
        encodedBytes = md5.ComputeHash(originalBytes);
    }
    return Encode(encodedBytes);
}

While this other function would be a Helper ?

public static string Encode(byte[] byteArray)
{
    if (byteArray == null || byteArray.Length == 0)
        return "";

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < byteArray.Length; i++)
    {
        sb.Append(byteArray[i].ToString("x2"));
    }
    return sb.ToString();
}

How about a Extension could you p开发者_运维问答rovide any simple examples of when I should turn something into a extension of a method ?


When to make a method a static helper method or an extension method is a subjective decision. If you need help deciding whether or not to make a method an extension method, then ask yourself whether it makes sense to include that method in the other class as a normal instance method.

For example, you might have a method like this:

public class Customers
{
    public static Customer GetCustomer(string name)
    {
        // implementation
        return customer;
    }
}

Does this make sense as an instance method of the String class?

public class String
{
    public Customer GetCustomer()
    {
        // implementation
        return customer;
    }

    // other methods
}

Design-wise this polutes the String class with a completely unrelated class. It makes the String class a "kitchen sink" that collects anything and everything remotely related to strings.

On the other hand, suppose you have method like this:

public class StringHelper
{
    public static string[] Split(string s, string separator)
    {
            // implementation
            return array;
    }
}

Does this make sense as an instance method of the String class?

public class String
{
    public string[] Split(string separator)
    {
            // implementation
            return array;
    }

    // other methods
}

Absolutely! This an natural extension to the existing Split functionality. It could have been included in the original implementation of String.

Everything in between can be judged on a case-by-case basis.


I'm not sure if it's entirely correct but I've often heard the terms helper and utility used interchangeably. They both strike me as fairly informal terms since they refer to an organizational approach rather than anything specific about how the language handles them. Still, there are some connotations that may clear up some confusion for you.

A utility method will typically be one that is used often but is not specific to your application. Cosine and Sine methods are good examples of this.

A helper class or helper method is typically used often but is specific to your application. Encrypt is a good example because encryption methods employed vary from application to application.

With your regard to when you should write a utility method the answer is it's not really a "should" or "should not" kind of situation. There is negligible (if any) difference when it comes to performance and whether you use an extension method or pass in a parameter to a helper/utility method is purely a matter of preference. While an extension method may have a cleaner feel to it there are many languages that do not support extension methods so neither approach is wrong and will have a negligible impact on readability of your source code.


Regarding the extension methods, they shouldn't be overused.

Extension methods are best when they seem to extend the existing functionality of an object. For example, you might extend StringBuilder with a method AppendHex.

However, if the functionality seems unrelated to the object or to its existing functionality, using a "utility" class is a better practice. It will be easier to read, write, and understand these methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号