Saturday, March 25, 2017

When a constant should be passed into a method

Have you ever had a wth moment?

Dry Air Gas Constants

So I've been working on air density equations that use the dry air constant.

Since I'm working with Java I set this up simply like this...

private static final double DRY_AIR_GAS_CONSTANT = 287.058;

Quite beautiful and simple, right?  No, wrong!

I discovered differences in the represented gas constant by trying to figure out why different air density calculators were giving different results (note: I'm not exactly sure why there are differences right now, need to research further). For example:




So with all this being said, how do I implement my code?!? I gander there are a few options...

Constant/non-constant Option 1

OPTION: I could just create constants for each known location...

private static final double DRY_AIR_GAS_CONSTANT_LOC_1 = 286.9;
private static final double DRY_AIR_GAS_CONSTANT_LOC_2 = 287.22;
private static final double DRY_AIR_GAS_CONSTANT_LOC_3 = 287.05;
private static final double DRY_AIR_GAS_CONSTANT_LOC_4 = 287.058;

PROBLEM: However, I don't think this would work as it may not be practical to keep doing coding changes for locations, and what if the values at those locations change?  (you can't dynamically change a final constant).

Constant/non-constant Option 2

OPTION: I could set the code up with getters/setters to allow for the changing of the 'constant' :

private double dryAirGasConstant = 287.058

public double getDryAirGasConstant() {
  return dryAirGasConstant;
}
public void setDryAirGasConstant(double dryAirGasConstant) {
  this.dryAirGasConstant = dryAirGasConstant;
}

PROBLEM: The desire is to have the utility available as a Java utility class, and getters/setters do not support this model.


Constant/non-constant Option 3

OPTION: perhaps I could enumerate the constants, and associate the values after the fact.

public enum DryAirGasConstant {
    REGION_1, // 286.9;
    REGION_2, // 287.22;
    REGION_3, // 287.05;
    REGION_4  // 287.058;
}

PROBLEM:  The code would still be subject to adding and modifying regional values.

Constant/non-constant Option 4

OPTION: I could utilize a web service that queries the dry air gas constant via location.  

PROBLEM: This seems to be the best approach, but such a thing does not exist


Constant/non-constant Option  5

OPTION: Since all the constants are very close in value... only provide one and let the user deal with difference in results from there desired value of which they cannot set.

PROBLEM: The result would not be accurate/precise. 

Constant/non-constant Option  6

OPTION: Allow the user to pass into the methods as an argument, the desired constant value.

PROBLEM: Uh yeah, why would someone pass a 'constant' into a method?!?  Well, if you followed the post, this is apparently the time you'd want to... and probably what makes the most sense.


Do you have other suggestions?  I posted this question to CodeRanch... let's see what those guys and gals have to say.



No comments:

Post a Comment

The source code for gliesians.com is for sale on Ebay.

I've decided to shut down gliesians.com and sell the source code. If anyone is looking to purchase the related source, you can find it...