Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been reading some post on how best to implement a helper class and I found that many people agree that the "helper classes" should be avoided.

"helper classes are an extraordinarily bad idea that should be avoided most of the time"

Post 1 - Are Helper Classes Evil?
Post 2 - Killing the Helper class, part two
Post 3 - How to Refactor the Helper Class

It seems that what they say makes sense, but how should I extend the functionality of a .Net class? which is the best practice to do this?.

For instance: I need to extend the functionality of the SerialPort class by adding a function to know if a port exists, with a common Helper Class I could do this:

VB
Public NotInheritable Class Helper

   Public Shared Function SerialPortExists(ByVal portNumber as Integer) As Boolean
      Dim ports As String()
      ports = SerialPort.GetPortNames()
      Return Array.Exists(Of String)(ports, Function(p) p.Equals("COM" & portNumber.ToString()))
   End Function

End Class


how can I accomplish this using best practices?
Posted

1 solution

Well, there really isn't a hard and fast rule for this.

My personal opinion is if this is the only method you have that helps SerialPort or you have a few methods related to it is to create a SerialPortHelper class and drop the method in there. This class should noly have methods in it related to SerialPort, NOTHING ELSE.

I only read the first post you linked but what it was generally referring to was creating a general purpose "catch all" class for miscellaneous methods that don't fit anywhere else. This is bad practice and I tend to agree.

I also don't feel the need to Inherit from the SerialPort class just to add a single method to it. If you've got more than that, feel free.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900