How to set default values to all wrong or null parameters of method?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-04-30T13:30:23Z Indexed on 2010/04/30 13:47 UTC
Read the original article Hit count: 253

Filed under:
|
|
|
|

At the moment I have this code (and I don't like it): private RenderedImage

private RenderedImage getChartImage (GanttChartModel model, String title,
                                     Integer width, Integer height,
                                     String xAxisLabel, String yAxisLabel,
                                     Boolean showLegend) {
    if (title == null) {
        title = "";
    }
    if (xAxisLabel == null) {
        xAxisLabel = "";
    }
    if (yAxisLabel == null) {
        yAxisLabel = "";
    }
    if (showLegend == null) {
        showLegend = true;
    }
    if (width == null) {
        width = DEFAULT_WIDTH;
    }
    if (height == null) {
        height = DEFAULT_HEIGHT;
    }
    ...
}

How can I improve it?

I have some thoughts about introducing an object which will contain all these parameters as fields and then, maybe, it'll be possible to apply builder pattern. But still don't have clear vision how to implement that and I'm not sure that it's worth to be done. Any other ideas?

© Stack Overflow or respective owner

Related posts about java

Related posts about refactoring