Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
scroll top