I've got a class with some functions which realistically are just 'helper' methods that client code could do itself with other public accessor properties/methods, and I'm undecided as to whether I should define these as properties with a getter, instance methods, or static methods that take the instance as a parameter. In addition, I've also got an interface extracted from the class that is used pretty much everywhere except on construction, to allow my code to use any class implemented against the interface.
The question is, which is best from a design point of view? For example, as a means of getting the initial from this class:
class Person : IPerson {
private string name;
public string Name { get { return this.name; } }
// Property with getter
public string Initial { get { return this.name.Substring(0,1); } }
// Instance method
public string GetInitial { return this.name.Substring(0,1); }
// Static method
public static string GetInitial(IPerson person) {
return person.Name.Substring(0,1);
}
}
The property lends itself to shorter, more readable client code, but would require anyone implementing against IPerson to write their own implementation, as would the instance method.
The static method would mean implementing classes wouldn't need to write their own, and my code can guarantee how the initial is determined based on the name, but it means it can't be on the interface, and client code's a little more verbose.
Does it just come down to whether not it's a good idea to allow implementing classes to specify how helper methods are calculated?
EDIT: Minor aside, why won't SO let me add the best-practices tag?
A helper function is a function (usually supplied by the writer of a class) that does not need direct access to the representation of the class, yet is seen as part of the useful interface to the class.
A helper method is just a method that helps you do something else. For example, if you had to find the square root of a number multiple times within a method, you wouldn't write out the code to find the root each time you needed it, you'd separate it out into a method like Math.Sqrt.
In such case, you should declare the function at the top of the file calling the function. While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task. When a program calls a function, the program control is transferred to the called function.
The C standard library provides numerous built-in functions that your program can call. For example, strcat () to concatenate two strings, memcpy () to copy one memory location to another location, and many more functions.
How about an extension method?
namespace IPersonExtensions
{
public static class IPersonExtensionClass
{
public static string Initial(this IPerson @this)
{
return @this.name.Substring(0, 1);
}
}
}
Use it like this:
string initial = person.Initial();
This way, you can share implementation without having to inherit or rewrite code. Using the separate namespace makes it possible for users to choose whether they want to use this code or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With