About formatting
Here are two scenarios in which the details of the formatting are described.
How should in the clean code way, this be formatted

Scenario 1: Dependent functions of dependent functions

function main () {
  firstFunction();
  secondFunction();
  thirdFunction();
}

function firstFunction () { 
  fourthFunction()
}
function fourthFunction() {}

function secondFunction() {
  functionFifth() 
}

function functionFifth() {}

function thirdFunction() {}


OR

function main () {
  firstFunction();
  secondFunction();
  thirdFunction();
}

function firstFunction () { 
  fourthFunction()
}

function secondFunction() {
  functionFifth() 
}

function thirdFunction() {}

function fourthFunction() {}

function functionFifth() {}

Scenario 2: Multiple functions call the same function

function firstFunction() {
  mainFunction()
}

function mainFunction() {}

function secondFunction() {
  mainFunction()
}

OR

function firstFunction() {
  mainFunction()
}

function secondFunction() {
  mainFunction()
}

function mainFunction() {}

For me personally, i would in scenario 1 choose the first way and in scenario 2 the second.

Question: How should the formatting be in this two scenarios ?

2

TL;DR – Whatever is more readable.

I Sometimes:

  • Order methods based on the order of execution.
  • Group similar methods (on different types together).
  • Create groups of methods that act on the same type.
  • Move utility methods to the end of the file.
  • Sometimes I will extract (private) methods for readability.
  • Sometimes I will inline (private) methods.

The “rules” around formatting and to some extent maintaining levels of abstraction are GUIDELINES, generally speaking any kind of logical pattern will be better than randomly “splatting” the methods into a file (assuming its a consistent/known pattern followed through the code base).

If you find it difficult to follow the logic of a file (regardless of how you order the methods), consider if there is a concept that can be extracted / moved to a different file.

With respect to the first scenario, the method order is roughly analogous to “Depth-First” and “Breadth-First”. My gut feel is that the breadth first approach only really clears up the details for the first level – after that you will need to jump around the file to follow the logic, so I would be tempted to avoid it … in most cases.

There are so many possible variants of the second scenario, that I am simply going to default to my guiding principal: whatever is more readable.