question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adopt `options` object for optional parameters

See original GitHub issue

Formalizing @DenisCarriere’s elegant proposal, which I really like. 😄 I would actually include all the optional parameters in the options object, making it more consistent throughout the library.

/**
 * @param {Object} [options={}] Optional parameters
 * @param {string} [options.units="kilometers"] can be degrees, radians, miles, kilometers, ...
 * @param {number} [options.steps=64] number of steps on a rounded corner
 */
module.exports = function (feature, distance, options) {
    options = options || {};
    var units = options.units;
    var steps = options.steps;

As suggested, we might formally adopt this for new modules and refactor the existing modules in the new major release.

cc: @rowanwins

Single optional parameter (Backwards compatible with v4.0)

/**
 * @param {Feature<LineString>} line input line
 * @param {number} distance distance along the line
 * @param {Object} [options] Optional parameters
 * @param {string} [options.units="kilometers"] can be degrees, radians, miles, or kilometers
 */
export default function (line, distance, options) {
    // Backwards compatible with v4.0
    var units = (typeof options === 'object') ? options.units : options;

index.d.ts

interface Options {
    units?: Units;
}
export default function along(line: LineString, distance: number, options?: Options): Point;

Multiple optional parameters (❗️ breaking change)

/**
 * @param {Feature<LineString>} line input LineString
 * @param {Object} [options] Optional parameters
 * @param {number} [options.resolution=10000] time in milliseconds between points
 * @param {number} [options.sharpness=0.85] a measure of how curvy the path should be between splines
*/
export default function (line, options) {
    // Optional params
    options = options || {};
    var resolution = options.resolution || 10000;
    var sharpness = options.sharpness || 0.85;

    // validation
    if (typeof options !== 'object') throw new Error('options must be an object');
    if (typeof resolution !== 'number') throw new Error('options must be an number');
    if (typeof sharpness !== 'number') throw new Error('sharpness must be an number');

index.d.ts

interface Options {
    resolution?: number;
    sharpness?: number;
}
export default function bezier(line: LineString, options?: Options): LineString;

Apply to the following modules:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
DenisCarrierecommented, Oct 4, 2017

Done

0reactions
DenisCarrierecommented, Oct 4, 2017

🎉 All done… now just need to review the remaining errors that were caused by the refactoring.

https://github.com/Turfjs/turf/pull/993

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why should Java 8's Optional not be used in arguments
When a method can accept optional parameters, it's preferable to adopt the well-proven approach and design such case using method overloading.
Read more >
How to use optional parameters in JSON.stringify and JSON ...
This object provides two methods for generating and parsing JSON format, the JSON.stringify() method and the JSON.parse() method. Without ...
Read more >
Named Parameters using Destructured Objects
Since JavaScript doesn't natively support named parameters. Here's a solution using object. No more worrying about the specific order of inserting your ...
Read more >
Optional Function Parameters With Default Values Via ...
Optional parameters are a thing of the past. Use the object spread syntax for easy parameter overriding.
Read more >
Opinions on using Optional<> as parameter : r/java - Reddit
So by using an Optional parameter you have added complexity, added extra code, and added an extra null-risk, with no actual benefit.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found