- Jan 2, 2006
- 10,455
- 35
- 91
This is javascript:
Here, none of the function arguments are validated and the "returnShippingCost" function calls the "returnItemWeight" function due to the benefit of scoping.
This is the simple way of making the function, but how important is it to:
1. First validate all inputs that go into the function?
2. Here I actually sent the "returnItemWeight" function as an argument into my second function. I could have relied on scoping but I'm explicitly sending the function in as an argument. Is it good practice to not rely on scoping when using a function defined outside the scope of the current function?
The second example is much more robust, but it sure is a lot more code (I haven't even done the error handling).
But I've heard that it's good practice to always first check the arguments and have functions ONLY use things that have explicitly been fed into it via the arguments (including other functions)?
Here, none of the function arguments are validated and the "returnShippingCost" function calls the "returnItemWeight" function due to the benefit of scoping.
Code:
// sample orderFormContents:
var myOrder = {
customerInfo: {
fname: "Sdfsd",
lname: "sadfsd"
},
shippingAddress: {
zipcode: 55555
},
items: [
{
sku: "343rsdf",
qty: 4
},{
sku: "dsfsad",
qty: 1
}
]
};
var returnItemWeight = function(sku){
// look up the item via sku and return the weight
};
var returnShippingCost = function(orderFormContents){
var zipcode = orderFormContents.shippingAddress.zipcode;
var shippingCost;
var weight = 0;
for(index in orderFormContents.items){
weight += returnItemWeight(orderFormContents.items[index].sku);
};
//something that calculates and returns the shipping cost from the weight and zipcode
};
This is the simple way of making the function, but how important is it to:
1. First validate all inputs that go into the function?
2. Here I actually sent the "returnItemWeight" function as an argument into my second function. I could have relied on scoping but I'm explicitly sending the function in as an argument. Is it good practice to not rely on scoping when using a function defined outside the scope of the current function?
Code:
var returnItemWeight = function(sku){
if (sku && typeof sku === "string"){
// look up the item via sku and return the weight
} else {
// something that handles the error
};
};
var returnShippingCost = function(orderFormContents, itemWeightFunction){
// validate typeof and the very existence of values and keys and nested values
if (orderFormContents && typeof orderFormContents === "object" && orderFormContents.shippingAddress && orderFormContents.shippingAddress.zipcode && orderFormContents.items)
var zipcode = orderFormContents.shippingAddress.zipcode;
var shippingCost;
var weight = 0;
for(index in orderFormContents.items){
weight += itemWeightFunction(orderFormContents.items[index].sku);
};
// do something that calculates and returns shipping cost based on weight and zipcode
} else {
// something that handles the various errors (no orderFormContents? no shippingAddress? no zipcode? no items?)
};
};
returnShippingCost(myOrder, returnItemWeight);
The second example is much more robust, but it sure is a lot more code (I haven't even done the error handling).
But I've heard that it's good practice to always first check the arguments and have functions ONLY use things that have explicitly been fed into it via the arguments (including other functions)?
Last edited:
