I'm writing a C (c99) library that operates on strings. I'm having a design problem writing functions that will perform in different modes.
For example; the Find function can search for:
- the first, last, n-th element
- different letter case
- return position before or after the found element
- search in a substring
Those modes cause exponential function naming. My solution is a function for each combination of modes:
Find(), FindCase(), FindSubstring(), FindSubstringCase(), FindSubstringCaseEnd(), FindEnd(), ...
and so on until every combination is made. I have written the find functions in a way where each mode is just a wrapper to a generic find function that is called internally to avoid duplication. This made adding new function modes easy, the only downside is the unusual function count.
I don't think any more modes will be added, so the current function count is within double digits. Still I'm unsure if that is the best solution.
Alternate solutions I considered and didn't find worthy are:
Having one generic function with a lot of parameters. This is inferior because it is easier to remember the function name than its parameters. It is also error prone and puts an unecessary burden on the user.
Have a function that takes a string of modes( like fopen() ). Same problems as above.
Variable arguments functions are just not worth considering.
I think my solution is decent, since the user only has to remember the name, naming is consistent, so you can "guess" the name, and cannot really make a mistake. In the worst case you have to look-up the name and the name could be up to ~20 characters, which still takes less space than additional parameters.
Also note that this problem applies to other functions( insert, erase, ... ) to a lesser degree.
Still, I'm looking for better solutions if there are any.
Find(FIND_END, ...);
If you wanted to find from the end with case folding you would callFind(FIND_END|FIND_CASE, ...);
etc.