Thursday 15 March 2012

java - Avoid if/else in constructor - NavigableMap? -



java - Avoid if/else in constructor - NavigableMap? -

i have next code, based on input constructor need initialise values wrapped in simple pojo.. these values constant can see.

however don't if/else build , had read navigablemap alternative holding range values.. thoughts on how improve/clean below or indeed utilize construct?

thanks

private calibrated calibratedvalues; public calibrationproperties(long odnumber) { setcalibratedvalues(odnumber); } private void setcalibratedvalues(long odnumber) { if (odnumber < 762) { this.calibratedvalues = new calibrated(k0, h0, k0_inv, h0_inv, f0); } else if (odnumber < 866) { this.calibratedvalues = new calibrated(k1, h1, k0_inv, h0_inv, f1); } else if (odnumber < 1011) { this.calibratedvalues = new calibrated(k2, h2, k2_inv, h2_inv, f2); } else { this.calibratedvalues = new calibrated(k3, h3, k3_inv, h3_inv, f3); } //two exceptions if (odnumber == 858){ this.calibratedvalues = new calibrated(k2, h2, k2_inv, h2_inv, f2); } if (odnumber == 1005){ this.calibratedvalues = new calibrated(k3, h3, k3_inv, h3_inv, f3); } } public calibrated getcalibratedvalues() { homecoming calibratedvalues; } /** convenience class used hold calibrated values */ static class calibrated { private double[] k; private double[] h; private double[] kinv; private double[] hinv; private double f; calibrated(double[] k, double[] h, double[] kinv, double[] hinv, double f) { this.k = k; this.h = h; this.kinv = kinv; this.hinv = hinv; this.f = f; } public double[] getk() { homecoming k; } public double[] geth() { homecoming h; } ...

you not using if/else in constructors here, have created mill method.

use more meaningful names, if read code after year not know mean - mean constructor parameters , class properties your constructor has big argument list. create within of class mill methods

example:

public static calibrated createsmallcaribration(){ homecoming new calibrated(k0, h0, k0_inv, h0_inv, f0); }

if can create contructor bundle visible. , know each constructor configuration ment be,

java

No comments:

Post a Comment