When developping a project I follow the OOP principles and break my code into classes and most of the time I go for one file = one class.

Now I dont know how to organize my code into the class and even into my own functions/methods I'm still looking for ways to improve the readability.

I think the general consensus is :

  1. attributes
  2. Constructors
  3. Getters/Setters
  4. Methods

But once I get to the method part I dont know if I should organize by public/protected/private members, or if I should put related methods together ?

IE :

Methods for database operation, then methods for user input validation, then methods for event handling.

What If I put some code in a method, and then I call this method in a lots of my others class method ?

public void f1(){
    // other stuff

public void f2(){
    // other stuff

Where do I put 'mymethod' definition for an optimal readibility ? Should I declare it before f1 and f2 or after both ?

  • Possible duplicate of How would you know if you've written readable and easily maintainable code?
    – gnat
    Commented Mar 9, 2018 at 17:47
  • With modern development IDE's, the order hardly matters. People will use the dropdowns or F12 to find a method. The practice our team uses is to define the properties and methods in alphabetical order, which prevents any merge issues that could result from code moving around.
    – John Wu
    Commented Mar 9, 2018 at 18:06
  • Every shop will have their own tradition but there is literature regarding this issue: Uncle Bobs Stepdown Rule Commented Mar 11, 2018 at 14:52

3 Answers 3


Quit procrastinating. Don't waste your time with this stuff.

Modern tools facilitate jumping around to parts of the code so effectively you could order a classes functions and properties almost completely randomly and most folks wouldn't even notice.

There are tools that can do it for you if it's really that important to you. Find a decent tool that works with your development language and just use one of the defaults. Done.

  • Hundred times this. Order of stuff within class is mostly irrelevant when using modern IDE. The readability and understandability are in different areas.
    – Euphoric
    Commented Mar 9, 2018 at 18:56

If you're asking this for your own, personal projects, then @whatsisname nailed it. If you're joining a team, take a look at some of the existing code, and use their style.

  • Agreed! Consistency is valuable when working on a team. I'm not opposed to adopting an opinionated styleguide. For example, AirBnB's JavaScript styleguide specifies a very specific structure for React component classes. Commented Mar 9, 2018 at 18:54

re: public/protected/private

A good argument is to put the public methods first, cause they are the API of your class and therefore "more important". Let users see them first, to get a feel for what the class can do for them. For example, if they think they want to use this class to talk to a database, they'd expect to seem something like a connect() and a query() method.

Following this argument, private methods are "less important" and can be at the bottom of your class.

Not the answer you're looking for? Browse other questions tagged or ask your own question.