HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Encapsulating Complex Calculation in JavaScript

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
encapsulatingcalculationcomplexjavascript

Problem

This is code for performing a calculation that should be repeated with different levels and temperatures. I am concerned about the organization of this code as I feel it may contain too many helper functions and seems a little unnecessarily complicated. Only the getCharge() method needs to be exposed to the browser.

There is sample usage of this code after the initial definition.

```
var hprVessel = function (level, temp) {
/**
* @params {Number} level - Percentage load for HPR
* @params {Number} temp - temperature of fluid
*/
var liquidCoefficients = [
83.4485407734612,
-0.111449280945489,
0.000100022560084721,
-0.00000206240663899928
];

var vaporCoefficients = [
0.895329190066827,
0.016635773286873,
-0.0000894924196507015,
0.00000206296636296276
];

var vessel = {
// Vessel-specific Params
diameter: 1.9583 * level,
height: 0.2937 * level,
length: 15.9583 * level,

// Calculations
getLiquidVol: function () {
var r = this.diameter/2;
var sectionArea = Math.pow(r, 2) * Math.acos((r - this.height) / r);
var triangleArea = Math.sqrt(Math.pow(r, 2) - Math.pow((r-this.height), 2))*(r-this.height);

return (this.length sectionArea triangleArea);
},
getVesselVol: function () {
return (Math.PI Math.pow(this.diameter, 2) this.length);
},
getVaporVol: function () {
return (vessel.getVesselVol() - vessel.getLiquidVol());
},
getLiquidCharge: function (temp) {
return (vessel.getLiquidVol() * vessel.calcDensity(liquidCoefficients, temp));
},
getVaporCharge: function (temp) {
return (vessel.getVaporVol() * vessel.calcDensity(vaporCoefficients, temp));
},
chargeHPR: function (temp) {
return (1000 + vessel.getLiquidCharge(temp) + vessel.getVaporCh

Solution

diameter: 1.9583 * level,
height: 0.2937 * level,
length: 15.9583 * level,


It might be wise to actually put these numbers in a constant and naming them appropriately. That way, in the long run, you won't forget what these values are.

return (/* stuff */);


I notice you also use parens in your returns. Unless they're a personal preference, they're actually optional (What do you think this is? PHP? :P)

Now this code appears to benefit more using prototypal inheritance rather than a factory pattern. That's because every time you call hprVessel, it builds an object of functions. With prototypal inheritance, your instance will only hold instance properties, aka "vessel-specific data". The functions will be shared across your vessel instances via the prototype.

Here's a short example:

function HPRVessel(level, temp){

  // Instance data, aka vessel-specific data
  this.temp = temp;
  this.level = level;
  this.diameter = HPRVessel.SOME_CONSTANT * level,
  this.height = HPRVessel.ANOTHER_COSNTANT * level,
  this.length = HPRVessel.YET_ANOTHER_CONSTANT * level,

}

// pseudo-static properties
// Here we append general HPRVessel constants
HPRVessel.liquidCoefficients = [
  83.4485407734612,
  -0.111449280945489,
  0.000100022560084721,
  -0.00000206240663899928
];

HPRVessel.SOME_CONSTANT = 1.9583
HPRVessel.ANOTHER_COSNTANT = 0.2937
HPRVessel.YET_ANOTHER_CONSTANT = 15.9583

// Here, we define shared stuff. Best practice is to just put methods on it.
HPRVessel.prototype.getCharge = function(){
  // Anything on the instance is accessible via `this`
  return this._chargeHPR(this.temp);
};

// Since there's no "private", we use the convention of prefixing `_` to tell
// curious people not to call these methods directly
HPRVessel.prototype._chargeHPR= function(){
  return (1000 + this._getLiquidCharge(this.temp) + this._getVaporCharge(this.temp));
};

// Usage:
var vessel = new HPRVessel(1, -30);
var result = vessel.getCharge();

Code Snippets

diameter: 1.9583 * level,
height: 0.2937 * level,
length: 15.9583 * level,
return (/* stuff */);
function HPRVessel(level, temp){

  // Instance data, aka vessel-specific data
  this.temp = temp;
  this.level = level;
  this.diameter = HPRVessel.SOME_CONSTANT * level,
  this.height = HPRVessel.ANOTHER_COSNTANT * level,
  this.length = HPRVessel.YET_ANOTHER_CONSTANT * level,

}

// pseudo-static properties
// Here we append general HPRVessel constants
HPRVessel.liquidCoefficients = [
  83.4485407734612,
  -0.111449280945489,
  0.000100022560084721,
  -0.00000206240663899928
];

HPRVessel.SOME_CONSTANT = 1.9583
HPRVessel.ANOTHER_COSNTANT = 0.2937
HPRVessel.YET_ANOTHER_CONSTANT = 15.9583

// Here, we define shared stuff. Best practice is to just put methods on it.
HPRVessel.prototype.getCharge = function(){
  // Anything on the instance is accessible via `this`
  return this._chargeHPR(this.temp);
};

// Since there's no "private", we use the convention of prefixing `_` to tell
// curious people not to call these methods directly
HPRVessel.prototype._chargeHPR= function(){
  return (1000 + this._getLiquidCharge(this.temp) + this._getVaporCharge(this.temp));
};

// Usage:
var vessel = new HPRVessel(1, -30);
var result = vessel.getCharge();

Context

StackExchange Code Review Q#115846, answer score: 3

Revisions (0)

No revisions yet.