Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to prevent naming pollution by helper classes in C#?

I often come across the pattern that I have a main class and several smaller helper classes or structs.

I'd like to keep the names of thoses structs as clean as possible. So when I have a class that's called CarFinder that heavily makes use of some special Key object that is only (or mainly) used internally, I'd like to call that object Key instead of CarFinderKey.

Everything to remove all the extra fuzz that distracts me from when I try to understand the class while reading it.

Of course I don't want to pollute the rest of the code with a small helper class that is called Key - it most likely will clash and confuse.

In a perfect world I would have liked to have a keyword like internal to this namespace, but as that does not exist that leaves me the following options that I can think of:


  1. Use internal and put the class in a different project.

Advantage: Perfect encapsulation.

Disadvantage: A lot of organisational overhead and unnecessary complicated dependencies.

Note: I'm not talking about really large self contained systems that undoubtedly deserve their own assembly.


  1. Put it in a different child namespace, like CarFinding.Internal

Advantage: Easy to implement.

Disadvantage: Still can pollute when the namespace is accidently imported.


  1. Put the helper class as a child class within CarFinder.

Advantage Doesn't pollute internally and can even be promoted as a public helper struct that is exposed to the outer world with CarFinder.Key

Disadvantage Have to put the helper class within the same file, or encapsulate it in an external file with public partial class around it. The first one makes a file unneccesary long, the second just feels really ugly.


  1. Anyway call it CarFinderKey

Advantage Easy to implement.

Disadvantage Adds in my opinion too much fuzz to CarFinder. Still unncessary pollutes the naming, just with a name that is not likely to clash.


What is the recommended guideline?

like image 506
Dirk Boer Avatar asked Oct 19 '15 09:10

Dirk Boer


People also ask

Is a helper class a code smell?

A Helper class is a lesser known code smell where a coder has identified some miscellaneous, commonly used operations and attempted to make them reusable by lumping them together in an unnatural grouping.

What is a helper in programming?

In object-oriented programming, a helper class is used to assist in providing some functionality, which isn't the main goal of the application or class in which it is used. An instance of a helper class is called a helper object (for example, in the delegation pattern).

What is helper CSS?

What is a CSS Helper Class? CSS helper classes, otherwise known as utility classes, are a CSS methodology that allows us to write less repetitive, modular code. This is done by creating a set of abstract classes that are responsible for doing one thing and one thing only.


2 Answers

Personally, I don't mind the extra "fuzz" caused by CarFinderKey, and here is why: Once worked on a very large project where we tried to use namespaces to disambiguate names.

So as you expand your system, you can very easily end up with 10 tabs open in your code editor, all named "Key.cs". That was seriously not fun.

like image 183
JMarsch Avatar answered Sep 27 '22 22:09

JMarsch


It's opinion based. Anyway, I would:

  1. try to make it a private nested class of CarFinder, which usually fails because the Key needs to be passed over to CarManager, you know what I mean. Public nested classes are discouraged.

  2. I would put it into a sub-namespace called Core, a common name for internal stuff. For me, Core is "namespace internal" by naming convention.

  3. The larger the project, the longer names you need. CarFinderKey is still a valid option.

I would never create additional assemblies just for this. It just doesn't feel right.

like image 23
Stefan Steinegger Avatar answered Sep 27 '22 22:09

Stefan Steinegger