[ACCEPTED]-What's your naming convention for helper functions?-naming-conventions
You can call the helper function anything 4 you want, and it won't matter as long as 3 you don't put the helper function in the 2 "global" namespace. Simply adding a "prime" seems 1 a common practice. :) E.g., in Haskell,
reverse :: [a] -> [a]
reverse = reverse' []
where reverse' :: [a] -> [a] -> [a]
reverse' result [] = result
reverse' result (x:xs) = reverse' (x:result) xs
I always use do_, like "do_compute" with 5 "compute". I find it quite descriptive as 4 it is effectively the part of the function 3 that performs the action, whereas the "compute" that 2 gets called needs to have a simple descriptive 1 name for the outside world.
I agree with ShreevatsaR, if you don't make 14 the helper function top-level (or worse, put 13 it in the export list), than it doesn't 12 matter what its name is.
I tend to call 11 helper functions f
and g
.
reverse :: [a] -> [a]
reverse = f []
where
f ys [] = xs
f ys (x:xs) = f (x:ys) xs
I just use this naming 10 scheme for small functions (otherwise I 9 don't know what the f
refers to). Then again, why 8 would you ever write big functions?
However, if 7 you do want to export your 'helper' function 6 because it might be useful to others, I 5 would call it:
reverseAccumulator
Like Haskell's zip
and zipWith
.
But 4 I wouldn't call those 'helper' functions, zipWith
is 3 just a generic function and zip
is the default 2 implementation (probably the one thats used 1 the most).
I tend to add "_recurse" to the end. So 10 "reverse_recurse". Not sure where I got 9 that from. I like leaving the base case 8 function simple as you have in your example. It 7 tends to be the "public" function and the 6 fact that it uses a helper function to perform 5 the iteration is irrelevant to the caller. In 4 javascript I sometimes go so far as to hide 3 the iterative function via a closure, to 2 make it really clear it is not to be called 1 directly.
I use aux
or foo_aux
(for main function foo
), and nest 1 the definition so it's not externally visible.
I also agree with ShreevatsaR, in this example 11 I would make the helper a private function.
For 10 other cases where I need helper functions 9 to be visible in the whole module, but not 8 exported, I tend to prefix functions with 7 '_'. Sure, there is the explicit exports 6 statement but during development I tend 5 to export all functions to ease interactive 4 exploration, e.g. in ghci. Later on I add 3 the list of exported functions and the underbar 2 makes it easy to remember whether I intended 1 a function to be local or not.
setup and execute
example:
function whateverSetup() { ... }
function whateverExecute() { ... }
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.