2

For the example's sake let's say there is a given file math.js. Think of it as a module containing many reusable functions.

The content of file is:

export function area(shape) {
   normalize(shape)
   ... // real stuff here
}


function normalize(shape) {
   ... // real stuff here
}

export function circumference(shape) {
  normalize(shape)
  ... // real stuff here
}

imagine that there are many more functions like normalize and even more exportable functions.

The question is: is a good coding style to put not exportable functions (I would call them private functions. function normalize is such function in my code snippet) at the bottom of file?

  • 1
    This is a matter of taste, not policy. Put your private functions where they make most sense to you. – Robert Harvey Nov 16 '16 at 15:21
  • @RobertHarvey Many languages have policy to put private methods at the bottom of class definition. If you think about math.js file as a class than moving 'private' functions at the bottom makes sense – Filip Bartuzi Nov 16 '16 at 16:30
  • 1
    There you go. That's as good a guideline as any. – Robert Harvey Nov 16 '16 at 16:30
0

I'd say think about readability over just a convention for every case (remember : there is no silver bullet).

  • Util private functions used pretty much everywhere in the module, all at start or end.
  • Util functions for some localized functions, if you can, put them into the same block instead of having to scroll 250 lines to check how the private function is implemented.

Basically the 2nd option is identifying some "submodule" in your module and reapply your convention locally to that "submodule" instead of the whole module. This is because if you have a lot of private functions, putting them all at the end or bottom may be less readable (more scrolling) than grouping them with the set of function they goes along.

  • 1
    "instead of having to scroll 250 lines to check how the private function is implemented." I think this is irrelevant nowadays. All modern text editors/IDE have "jump to definition" functionality – Filip Bartuzi Nov 16 '16 at 12:47
  • @FilipBartuzi you still have to go from where you were, and since we're talking about stuff in the same file ... :-). And even if you have that, for someone else that is unfamiliar with this code and is going through the code to try to understand the big picture, it'll be harder. – Walfrat Nov 16 '16 at 12:53

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.