1

I was wondering if there is a convention regarding where in code to locate private methods for a class. Should they be listed after public methods to make the distinction clear, before them, or is it considered OK to intersperse them? For example

public class example{

   public example()...

   public int some_method()...

   public int another_method()...

   private boolean helper_method()...
}

versus

 public class example{

      public example()...

      public int some_method()...

      private boolean helper_method()...

      public int another_method()...
 }

When coding in C/C++ I generally group functions based on dependencies, but in that case the API is made clear by the header file. For Java, I would gravitate toward listing all the public methods first to make it clear which methods are publicly accessible, but I want to make sure this isn't bad practice.

Cam
  • 66
  • 5
  • This is a matter of debate. Some say it's better to organise by access modifier, some say it's better to organise by function. – Reinstate Monica Jan 26 '14 at 23:27
  • 2
    This may help : http://programmers.stackexchange.com/questions/186418/in-java-should-private-helpers-go-above-or-below-public-methods – user2336315 Jan 26 '14 at 23:28
  • 2
    I suspect most people do the same, subject to putting private helper methods near the method they help if it's only one, but since the advent of IDEs that show you all your methods in alpha order and let you navigate to one with a mouse-click these are minor concerns. More time is wasted debating code cosmetics in this industry than I care to contemplate. – user207421 Jan 26 '14 at 23:30

1 Answers1

0

it has absolutely no importance in which order you put your access modifiers, but there one unwritten rule to help yourself or anybody else reading your code, just group them together public with public, and private with private. And if you really want an opinion, I put private first and then protected, public and default

cipster
  • 1
  • 4
  • Even that unwritten rule is a matter of taste - I prefer seeing a private method close to where it is called... – assylias Jan 26 '14 at 23:32