Simplifying JavaScript by Joe Morgan

Simplifying JavaScript by Joe Morgan

Author:Joe Morgan
Language: eng
Format: epub, pdf
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf


Tip 28 Create Default Parameters

In this tip, you’ll learn how to use default parameters to set values when a parameter is absent.

No matter how much planning you do, things change. This is especially true of function parameters. When you write a function, you expect a few parameters. Then, as code grows and edge cases emerge, suddenly the parameters you supplied aren’t enough.

In the next several tips, you’ll learn different techniques for handling parameters. Nearly all of these techniques can help you cope in some way with changing requirements. But to start, you’ll learn the easiest trick: setting default parameters.

Consider a basic helper function. All you want to do is convert pounds to kilograms. That seems simple. You simply need to take the weight as an input and divide the weight in pounds by 2.2 to get kilograms. (Apologies to non-Americans who don’t have to deal with this silliness. I’m sure you also get stuck converting other measurements.)

params/defaults/simple.js

​ ​function​ convertWeight(weight) {

​ ​return​ weight / 2.2;

​ }

That code seems easy enough. And you use it throughout the app. Before you know it, a ticket comes in because someone needs to be able to pass ounces. And because there are 16 ounces in a pound, you’ll need to convert that number to a decimal before adding it to the pounds.

Fine. You add in a parameter for ounces, but now you’re in a bind. Do you track down every instance of the function and add in a zero for the ounces? Or do you try to handle cases where a value wasn’t provided?

You can try the first approach and update every function, but there’s always the chance that you’ll miss one. Fortunately, in JavaScript, you don’t need to pass all the parameters to a function. They can be optional. If you’re missing a parameter, the value is set to undefined.

Knowing that, you go for the second approach and add a little bit of code to set the value if it doesn’t exist.

params/defaults/more.js

​ ​function​ convertWeight(weight, ounces) {

​ ​const​ oz = ounces ? ounces / 16 : 0;

​ ​const​ total = weight + oz;

​ ​return​ total / 2.2;

​ }

When you run convertWeight(44,11), you get 20.3125, which isn’t bad, but nearly every other conversion returns a long decimal string. convertWeight(44, 8) returns 20.22727....

Stranger still, when you run convertWeight(6.6), you expect to get 3 and instead you get 2.999999.... You can thank floating point arithmetic for that.[25]

Great—now you need to round up to handle cases where the floating point arithmetic doesn’t match user expectations. And because you’re rounding anyway, you should make the number of decimal points an option, too, with a default of two decimal places.

You add some more code to handle the missing parameter. You also add in a helper function, roundTo, to handle the rounding (see the book code for implementation details).

But there’s a complication. To make the default two decimal places, you can’t just check to see if the parameter roundTo is truthy. You can’t, for example, write const round = roundTo || 2;



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.