Question

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.)
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top