Default parameters should be last
12
13 private _params: Params;
14
15 constructor(id: string = ComponentAPI.generateID(), macro:string, geometricOperation: GeometricOperationType, layer: Layer, params: { [k: string]: any } = {}) {16 this._id = id;
17 this._macro = macro;
18 this._geometricOperation = geometricOperation;
Description
Placing default parameters before non-default ones waste its benefits since they must be provided to get to the non-default parameters.
Default parameter values allow callers to specify as many or as few arguments as they want while getting the same functionality and minimizing boilerplate, wrapper code.
Please ensure all the function parameters with default values are declared after the function parameters without default values. Otherwise, it makes it impossible for callers to take advantage of defaults since they must re-specify the defaulted values or pass undefined in order to get to the non-default parameters.
Bad Practice
function f(a = 0, b: number) {}
function f(a: number, b = 0, c: number) {}
function f(a: number, b?: number, c: number) {}
function f(c?: number,a: number, b = 0) {}
class defaultParamLast {
constructor(public a = 10, private b: number) {}
}
class defaultParamLast {
constructor(public a?: number, private b: number) {}
}
Recommended
function f(b: number, a = 0) {}
function f(a: number, c: number, b = 0) {}
function f(a: number, c: number, b?: number,) {}
function f(a: number, b = 0, c?: number) {}
class defaultParamLast {
constructor(private b: number, public a = 10) {}
}
class defaultParamLast {
constructor( private b: number, public a?: number) {}
}