Question

Why do some of the example classes in the Swing Tutorials (sorry, I don't remember which ones; I could be wrong, but I know I saw this somewhere) use private methods like this?:

public MyClass{
    public MyClass(){
        .
        .
        .   
        initializeComponents();
        .
        .
        .
    }
    private void initializeComponents(){
        //initializing
    }
}

Is that proper convention, or is this?:

public MyClass{
    public MyClass(){
        .
        .
        .
        //initializing components
          //component initialization
        .
        .
        .
    }
}

Which style of initialization is better?

I've only seen the example classes that have a large number of components to initialize use the private method, so it seems like it's for readability. Am I right? If that is so, wouldn't using comments also provide a similar level of readability?

Was it helpful?

Solution

so it seems like it's for readability. Am I right?

Yes it is better for readability as well as keeping all components initialization in a method, similar approach is used with WinForm in .Net framework. If only comments are used then imagine your constructor going to 200 lines of code just for component initialization.

If that is so, wouldn't using comments also provide a similar level of readability?

Component initialization is not just constructor call to the component class, usually it involves setting other properties like Height, Width, Position etc. Now imagine if comments and white space are used, then the constructor would be of hundreds of lines of code, just because of component initialization.

OTHER TIPS

I think they do that for readability. Separating the initialization of the GUI components from the constructor makes the constructor code clearer and more concise (otherwise you'd have huge constructor methods). As a result of this, you know exactly which blocks of code are relevant to the GUI, and which are relevant to the internal functions of the class.

Of course first one is better. Its all about better readability and maintanaibility.

The second approach is not good, as the comments are actually bad and unreliable in most of the cases. If some logic is changed the comments are usually not updated. While if the code itself is good including method with self explanatory name, it is always better. Extraction of some code which is logically related to a single responsibilty serves as a abstracted step (you dont need to go into details to understand)

If you need to initialize many fields and that too also have different types. Then only its better to put into functions and call it from constrctor, like -

public MyClass{
    public MyClass(){
         initializeComponentType1();
         initializeComponentType2();
         ...
    }
    /**
    * initializing type 1 component
    */
    private void initializeComponentType1(){
        //initializing
    }
    /**
    * initializing type 2 component
    */
    private void initializeComponentType2(){
        //initializing
    }
}

It improves readability and maintainability of code. It is always recommended write simple, readable and maintainable code.

But if there are some set of initilization, you don't need to create function.

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