I want to have functions that convert numeric values into strings in English words, to pluralize nouns, to inflect, break by words and so on. I will use these throughout a big project. Normally I would put them as pure static methods in a StringHelper class with nothing but static methods. But it there a better way to organize it to avoid static methods and helpers?

  • 1
    It's not entirely clear here what you mean by "better." If you're bothered by the prefixing of your methods with StringHelper, then try using extension methods, which eliminate that prefixing. – Robert Harvey Sep 13 '17 at 15:00
  • Are you using a language wihout free functions? Which one? C#, Java, ...? – Deduplicator Sep 13 '17 at 15:08
  • 2
    I am bothered by the notions that helpers are considered bad in general. Yet I am not aware of alternatives. It may be Java, PHP or TypeScript. – Gherman Sep 13 '17 at 15:10
  • Is this question is language specific or a general one? – Boris Modylevsky Sep 13 '17 at 17:00
  • 2
    "Helper methods", "util functions", etc aren't bad per-se by themselves, the danger is when you begin to have so many that you end up unintentionally creating a hodge-podge API of virtually no sensible design or structure. I think some sort of disparaging reference to PHP would be relevant here. – whatsisname Sep 13 '17 at 18:51

As usual it depends.

If you only works with one locale and don't need any kind of state for your process, you can handle everything static and you're fine.

However if you intend to have multiple languages where rules to pluralizes, break words can change, you will probably needs :

  • A general interface exposing every function for string manipulation
  • A factory that take the Locale for your request and return the right implementation of the interface.

Otherwise instead of having all static you could remove the static and use the new. Just in case you process a lot of text at once be sure to instantiate only once, and not in a loop where the potentially very numerous instantiation would be only wasting time and memory (like a loop for every word in a text).

Also I hope you don't intend to handle that kind of stuff manually, thought it seems easy in fact it is really hard. There are stuff already done and available for that, for instance this one for Java. Though it could probably used a intermediary layer of encapsulation to only do what you need without having for all users of your function how to use that class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.