Refactoring Tip

When you have something like

// connect to database
code
code
code

This might be an indicator that you can extract the code under the comment into a separate method to make things clearer and more modular.

For example:

public Connection connectToDatabase(necessary parameters) {
    code
    code
    code
}

This is nice because it is just as readable, and it’s harder for the description of the code block to get out of sync with what the code actually does.

Between when I thought of this and when I actually published it, I read a similar blog post that confirmed this thought pattern. Fowler also talks about this in Refactoring.

Categories: development

« Open Source for Retired People So what have I been doing the past few months? »

Comments