1

I have a MineSweeper program, where I am facing a scenario similar to.,

Where is my problem?

 class operation{

    <Data's needed for all operations> 
    /*(Each operation needs all the datas.)*/

   public  mainOperation(){ // user can call this mainOperation whenever they want to do some work.
       operation_ABC();
       operation_LMN();
    }

    private  void operation_ABC(){
    helper_DEF();  //This helper does partial work of ABC()
    helper_IJK();  //This helper does remaining work of ABC()     
    }

    private void DEF(){..//access and work on data..}
    private void IJK(){..//access and work on data..}       

    private void operation_LMN(){
    helper_OPQ();  
    < some work on data >
    helper_RST();  
    }

    private void helper_OPQ(){..//access and work on data...}
    private void helper_RST(){..//access and work on data...}    
    }

What I need?

What I need is some way to group the methods and reduce the visibility of helper_DEF and helper_IJK within operation_ABC. Here , I named it clearly operation_ or helper_ but the real scenario in my program is....

private void backEndActionTaker(){
      <type: private void > initializeMinePlacer(); 
      <type: private void > mineValueAssigner();    
}

Readability Problem: Developers who needs to read/extend this code will surely find difficult to segregate operation and helper as everything seems like an operation at one glance.

What I worked? I planned of creating inner classes without any data, but I felt 2 uneasy-things.

 class operation_ABC{
    exec_ABC(){
      helper_1();
      helper_2();
    }
    private helper_1(){......//Access outer class data...}
    private helper_2() {.........}
  }
  1. creating a class without any data-member.
  2. whenever I have to call that operation, I have to create an object and call that.(static inner classes methods willn't work because the helper methods access outer class data.)
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77

1 Answers1

2

Having private methods call other private methods is OK. If you want to improve readability, don't use prefixes like "helper_" or "operation_", which are non standard, violate naming conventions, and don't tell much about the method anyway. Just choose clear method names, and document them using javadoc comments.

Any maintenance developer should know how to analyze code, and should be able to use an IDE to know where a method is called if it isn't obvious from the documentation and name of the method.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • what about my inner classes. Are they are right way to do like that? – Muthu Ganapathy Nathan Dec 11 '11 at 09:20
  • In this case, I think they just add complexity without any benefit. – JB Nizet Dec 11 '11 at 09:22
  • Ya. The uneasy thing I feel is **both the OPERATION and HELPER methods are private and they are in same scope (No distinguishion btw both)**. (Any way to improve the readability) by restricting the scope of helper method within operation method? and I cant get the statement "The separation in private calling other private methods is OK." – Muthu Ganapathy Nathan Dec 11 '11 at 09:31
  • I rephrased the sentence. Don't worry about that. Having them in the same scope is normal and common. Don't over-engineer. – JB Nizet Dec 11 '11 at 09:38
  • Assuming the private operations can be grouped by similar functionality, adding private nested classes (with appropriate names) could be used to improve the organization of the class. As these themselves become more complex, consider refactoring them to top-level, package-private classes. Really, it is up to you. However, as indicated above, it is common to have methods in a class invoke other methods. – Saish Dec 29 '11 at 17:17