Question

I often experience difficulties in designation of my methods. For instance, now I have a static method which compares two hashes, and I stuck with its name. HashesEqual(string h1, string h2)? AreHashesEqual(string h1, string h2)? Your better version? This is generic question -- I have a lot of such stuff. Is there any authoritative source where I can read about naming conventions?

Was it helpful?

Solution

From Names of Type Members on MSDN:

Do give methods names that are verbs or verb phrases. Typically methods act on data, so using a verb to describe the action of the method makes it easier for developers to understand what the method does. When defining the action performed by the method, be careful to select a name that provides clarity from the developer's perspective. Do not select a verb that describes how the method does what it does; in other words, do not use implementation details for your method name.

Do name Boolean properties with an affirmative phrase (CanSeek instead of CantSeek). Optionally, you can also prefix Boolean properties with Is, Can, or Has, but only where it adds value.

I think others will agree that any naming convention is good as long as it makes sense and you use it consistently.


Alternatively, consider creating an extension method for the string class that provides this functionality. Then you can simply do:

var equal = h1.EqualsHash(h2); // or similar, based on the naming you choose

Or write a custom Hash class that keeps the hashed values internally, and override/overload its Equals method and ==/!= operator(s), giving way to this:

var h1 = new Hash("string1");
var h2 = new Hash("string2");

var equal = h1 == h2;
// or
var equal = h1.Equals(h2);

Or make your utility class stand alone (e.g. HashUtil or something), and keep the word "hash" out of its method(s) entirely:

var equal = HashUtil.AreEqual(h1, h2);

Also see: Guidelines for Names

OTHER TIPS

I'd suggest all developers read the .NET Design Gudelines (linked is the specific section on type members). In your instance, because the return type is also a boolean value, I'd recommend you try something like:

 IsHashEqual(string testHash)

The only method name suggestion provided by the .Net Framework Guidelines is that methods should be

DO give methods names that are verbs or verb phrases

I think that Are do qualifies as "verb phrases" and would work here.

In this case though you aren't defining something new but rather a specific form of a well established pattern: Equals. Given that my inclination would be to prefix the function with Equals so that it shows up close to Equals in features like Intellisense, Search, etc ...

EqualHashes(string p1, string p2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top