Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing specific vs generic methods

Tags:

methods

Which would be a better design for a public interface:

WriteValue(DeviceValueType.MachineName,"micks machine");

OR

WriteMachineName("Micks machine");

If you use type 1 then you just need one such generic interface and you can write to any number of DeviceValueTypes just by adding items to the enum.

The second type is more specific in that it will write only a value to specific type denoted by its method name. But these methods have a great advantage with respect to type safety.

For eg: Lets consider another type

WriteValue(DeviceValueType.SleepDelay,"25");

and

WriteSleepDelay(25);

Note that here, type 2 has an advantage because I can specify the type expected. If I were to use type 1 then I will have to cast the value to int and then throw an exception if the value is not in the expected range. If I had to pass complex types, this becomes even more difficult.

So in summary:

Type 1: Generic. The interface will need to expose only one method. No type safety for parameters. Type 2: Specific. The interface will require as many methods as there are types. Type safe.

What is the generally suggested methodology to solve this problem?

I am using C# but I believe this problem to be non language specific.

like image 886
bobbyalex Avatar asked Apr 08 '26 11:04

bobbyalex


2 Answers

I'm coming from a Python background, but I would actually pick both options. Make the WriteValue function, and let it do all the heavy lifting. Then, write the WriteMachineName and WriteSleepDelay functions as simple wrappers around WriteValue. This way, you'll be able to use the shorthand function for all functions that are used often, and the long version for everything that isn't called that often.

You might want to make the way this works explicit in the documentation though, so people who'll have to work with it after you know how and why you did this.

Also: I don't know how expensive function calls are in C#. If they are very expensive (in CPU time), you might want to always go with type 1.

like image 53
Xudonax Avatar answered Apr 10 '26 01:04

Xudonax


You should avoid what you call type 1 interfaces. Speaking from experience, these kind of interfaces tend to get messy pretty fast. Take the sleep delay example. How are you going to type check that string for a positive integer? You will end up with a huge switch statement. So at the very least encapsulate it.

Although your type 2 is better it should be noted that it tightly couples the write action with the data needed for the write. I don't know the requirements you are dealing with (write once read many?), but your DeviceValueType enum suggests you could simply declare a DeviceConfig class with properties like machine name and sleep delay and pass that to a Device or DeviceWriter class that will do the actual work. However, if machine name is config, while sleep delay is actually a method you should consider separating those two concerns as well (i.e. Device.sleep() and Device.writeConfig(DeviceConfig)).

like image 43
Lodewijk Bogaards Avatar answered Apr 10 '26 00:04

Lodewijk Bogaards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!