When to use a static helper class vs extension methods

When adding a static class, what criteria do you use to determine if it should be a helper class or an extension class? For example, I have a recursive hierarchical data structure (has Children list of more of itself) and I want to write methods to help searching and iterating through it. I could have a helper class like:
public static class TreeNavigation {
public static bool NodeExists(Root root, Node node){...}
}
public static class TreeNavigation {
public static bool NodeExists(Root root, Node node){...}
}
or a extension class like:
public static class TreeExtensions {
public static bool NodeExists(this Root root, Node node){...}
}
public static class TreeExtensions {
public static bool NodeExists(this Root root, Node node){...}
}
7 Replies
ero
ero5w ago
Or neither: have the method on the Root type directly.
Saiyanslayer
SaiyanslayerOP5w ago
Root is from an external library
Daniel
Daniel5w ago
Performance wise, I don't know if there's much of a difference. Maybe you can create a small side project to determine operating speeds. For me personally, I would go for extension methods. I like how intellesense detects them. I've always found Helper classes/methods like 'bandaids' to poorly planned code structure.
ero
ero5w ago
utility classes like that aren't uncommon. think Console, File, Path, etc. or even better examples; MemoryMarshal, CollectionsMarshal, BinaryPrimitives it depends on the usage and the goal of your library. is this class really meant to be public? if it's only for your own utility, i would make the class internal and an extension class. if you're providing this utility to your library's users, then i would probably consider making it a utility class. just my opinion
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
ero
ero4w ago
Not just near
Saiyanslayer
SaiyanslayerOP4w ago
Awesome, thanks everyone!
Want results from more Discord servers?
Add your server