-1

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(){
    mymethod();
    // other stuff
}

public void f2(){
    mymethod();
    // other stuff
}

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

Maxime
  • 153
  • 5
  • Possible duplicate of [How would you know if you've written readable and easily maintainable code?](https://softwareengineering.stackexchange.com/questions/141005/how-would-you-know-if-youve-written-readable-and-easily-maintainable-code) – gnat Mar 09 '18 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 Mar 09 '18 at 18:06
  • Every shop will have their own tradition but there is literature regarding this issue: [Uncle Bobs Stepdown Rule](https://grysz.com/2015/11/13/reformat-java-code-in-intellij-according-to-uncle-bobs-stepdown-rule/) – candied_orange Mar 11 '18 at 14:52

3 Answers3

5

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.

whatsisname
  • 27,463
  • 14
  • 73
  • 93
  • Hundred times this. Order of stuff within class is mostly irrelevant when using modern IDE. The readability and understandability are in different areas. – Euphoric Mar 09 '18 at 18:56
2

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.

Phil N DeBlanc
  • 175
  • 1
  • 5
  • 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](https://github.com/airbnb/javascript/tree/master/react#ordering) for React component classes. – Brad Buchanan Mar 09 '18 at 18:54
0

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.

user949300
  • 8,679
  • 2
  • 26
  • 35