DEV Community

Discussion on: What is the best way to organize methods/functions within a file?

Collapse
 
tiguchi profile image
Thomas Werner

Except for the following rough layout:

  • Constants
  • Variable members / fields
  • Constructors
  • Methods

I stopped worrying about rearranging and ordering by visibility, member name or getter / setter types, for the following reasons:

  • Moving a method to its "correct" place can increase the likelihood for merge conflicts (when several people happen to work on the same file at the same time)

  • IDEs can present the structure of a class or file in any way you like. You can sort by name, filter out private methods, just show fields etc.

  • IDE code generators usually generate new method stubs right under the method you're working on. Even when those methods are private, I stopped moving them to the bottom of the class. I actually prefer them now being right under the public method that uses them. It makes the code much clearer and easier to follow to me.

  • Mistakes happen. I want to worry less. I've been almost OCD for most of my life, and letting go of those things makes me happier 😁