Skip to content

Replaced defaultValue with ?? #12507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 1 addition & 4 deletions Apps/Sandcastle/gallery/3D Tiles 1.1 CDB Yemen.html
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,7 @@
const propertyValue = feature.getProperty(propertyId);
const property = metadataClass.properties[propertyId];

const propertyType = Cesium.defaultValue(
property.componentType,
property.type,
);
const propertyType = property.componentType ?? property.type;
tableHtmlScratch += `<tr style='font-family: monospace;' title='${property.description}'><th>${property.name}</th><th><b>${property.id}</b></th><td>${propertyType}</td><td>${propertyValue}</td></tr>`;
}
tableHtmlScratch +=
Expand Down
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/3D Tiles Point Cloud Styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

const styles = [];
function addStyle(name, style) {
style.pointSize = Cesium.defaultValue(style.pointSize, 5.0);
style.pointSize = style.pointSize ?? 5.0;
styles.push({
name: name,
style: style,
Expand Down
4 changes: 2 additions & 2 deletions Apps/Sandcastle/gallery/Imagery Layers Manipulation.html
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@
try {
const imageryProvider = await Promise.resolve(imageryProviderPromise);
const layer = new Cesium.ImageryLayer(imageryProvider);
layer.alpha = Cesium.defaultValue(alpha, 0.5);
layer.show = Cesium.defaultValue(show, true);
layer.alpha = alpha ?? 0.5;
layer.show = show ?? true;
layer.name = name;
imageryLayers.add(layer);
Cesium.knockout.track(layer, ["alpha", "show", "name"]);
Expand Down
8 changes: 4 additions & 4 deletions Apps/Sandcastle/gallery/development/3D Models.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@
});

async function createModel(url, height, heading, pitch, roll) {
height = Cesium.defaultValue(height, 0.0);
heading = Cesium.defaultValue(heading, 0.0);
pitch = Cesium.defaultValue(pitch, 0.0);
roll = Cesium.defaultValue(roll, 0.0);
height = height ?? 0.0;
heading = heading ?? 0.0;
pitch = pitch ?? 0.0;
roll = roll ?? 0.0;
const hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);

const origin = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, height);
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

- `Camera.getPickRay` was erroneous returning a result in camera coordinates. It is now returned in world coordinates as stated in the documentation. The result can be transformed using `Camera.inverseViewMatrix` to achieve the previous behavior.

#### Deprecated :hourglass_flowing_sand:

- `defaultValue` function and `defaultValue.EMPTY_OBJECT` have been deprecated, and will be removed in 1.134. Use respectively the nullish coalescing operator [??](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing) and `Frozen.EMPTY_OBJECT` instead. A new `Frozen.EMPTY_ARRAY` frozen value has been added for the same purpose as `Frozen.EMPTY_OBJECT`. See [Coding Guide](https://github.com/CesiumGS/cesium/tree/main/Documentation/Contributors/CodingGuide#default-parameter-values).

#### Fixes :wrench:

- Fixed broken Entity Tracking [sandcastle](https://sandcastle.cesium.com/?src=Entity%20tracking.html). [#12467](https://github.com/CesiumGS/cesium/pull/12467)
Expand Down
45 changes: 17 additions & 28 deletions Documentation/Contributors/CodingGuide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ To some extent, this guide can be summarized as _make new code similar to existi

- Directory names are `PascalCase`, e.g., `Source/Scene`.
- Constructor functions are `PascalCase`, e.g., `Cartesian3`.
- Functions are `camelCase`, e.g., `defaultValue()`, `Cartesian3.equalsEpsilon()`.
- Files end in `.js` and have the same name as the JavaScript identifier, e.g., `Cartesian3.js` and `defaultValue.js`.
- Functions are `camelCase`, e.g., `binarySearch()`, `Cartesian3.equalsEpsilon()`.
- Files end in `.js` and have the same name as the JavaScript identifier, e.g., `Cartesian3.js` and `binarySearch.js`.
- Variables, including class properties, are `camelCase`, e.g.,

```javascript
Expand Down Expand Up @@ -438,39 +438,30 @@ const p = new Cartesian3(1.0, 2.0, 3.0);

### Default Parameter Values

If a _sensible_ default exists for a function parameter or class property, don't require the user to provide it. Use Cesium's `defaultValue` to assign a default value. For example, `height` defaults to zero in `Cartesian3.fromRadians`:
If a _sensible_ default exists for a function parameter or class property, don't require the user to provide it. Use the nullish coalescing operator [`??`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing) to assign a default value. For example, `height` defaults to zero in `Cartesian3.fromRadians`:

```javascript
Cartesian3.fromRadians = function (longitude, latitude, height) {
height = defaultValue(height, 0.0);
height = height ?? 0.0;
// ...
};
```

- :speedboat: Don't use `defaultValue` if it could cause an unnecessary function call or memory allocation, e.g.,
- :speedboat: `??` operator can also be used with a memory allocations in the right hand side, as it will be allocated only if the left hand side is either `null` or `undefined`, and therefore has no negative impact on performances, e.g.,

```javascript
this._mapProjection = defaultValue(
options.mapProjection,
new GeographicProjection(),
);
this._mapProjection = options.mapProjection ?? new GeographicProjection();
```

is better written as
- If an `options` parameter is optional and never modified afterwards, use `Frozen.EMPTY_OBJECT` or `Frozen.EMPTY_ARRAY`, this could prevent from unnecessary memory allocations in code critical path, e.g.,

```javascript
this._mapProjection = defined(options.mapProjection)
? options.mapProjection
: new GeographicProjection();
```

- If an `options` parameter is optional, use `defaultValue.EMPTY_OBJECT`, e.g.,
function BaseLayerPickerViewModel(options) {
options = options ?? Frozen.EMPTY_OBJECT;

```javascript
function DebugModelMatrixPrimitive(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
this.length = defaultValue(options.length, 10000000.0);
this.width = defaultValue(options.width, 2.0);
const globe = options.globe;
const imageryProviderViewModels =
options.imageryProviderViewModels ?? Frozen.EMPTY_ARRAY;
// ...
}
```
Expand Down Expand Up @@ -594,9 +585,9 @@ result = Cartesian3.add(result, v2, result);

```javascript
function Cartesian3(x, y, z) {
this.x = defaultValue(x, 0.0);
this.y = defaultValue(y, 0.0);
this.z = defaultValue(z, 0.0);
this.x = x ?? 0.0;
this.y = y ?? 0.0;
this.z = z ?? 0.0;
}
```

Expand Down Expand Up @@ -771,7 +762,7 @@ Public properties that can be read or written without extra processing can simpl

```javascript
function Model(options) {
this.show = defaultValue(options.show, true);
this.show = options.show ?? true;
}
```

Expand Down Expand Up @@ -827,9 +818,7 @@ When the overhead of getter/setter functions is prohibitive or reference-type se

```javascript
function Model(options) {
this.modelMatrix = Matrix4.clone(
defaultValue(options.modelMatrix, Matrix4.IDENTITY),
);
this.modelMatrix = Matrix4.clone(options.modelMatrix ?? Matrix4.IDENTITY);
this._modelMatrix = Matrix4.clone(this.modelMatrix);
}

Expand Down
2 changes: 1 addition & 1 deletion Documentation/Contributors/DocumentationGuide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function Cartesian3(x, y) {
* @type {number}
* @default 0.0
*/
this.x = defaultValue(x, 0.0);
this.x = x ?? 0.0;

// ...
```
Expand Down
96 changes: 41 additions & 55 deletions Specs/Cesium3DTilesTester.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Cartesian3,
Color,
defaultValue,
Frozen,
defined,
JulianDate,
ImageBasedLighting,
Expand Down Expand Up @@ -120,15 +120,9 @@ const defaultIbl = new ImageBasedLighting({
});

Cesium3DTilesTester.loadTileset = async function (scene, url, options) {
options = defaultValue(options, {});
options.cullRequestsWhileMoving = defaultValue(
options.cullRequestsWhileMoving,
false,
);
options.imageBasedLighting = defaultValue(
options.imageBasedLighting,
defaultIbl,
);
options = options ?? {};
options.cullRequestsWhileMoving = options.cullRequestsWhileMoving ?? false;
options.imageBasedLighting = options.imageBasedLighting ?? defaultIbl;
options.environmentMapOptions = {
enabled: false, // disable other diffuse lighting by default
...options.environmentMapOptions,
Expand Down Expand Up @@ -170,10 +164,10 @@ Cesium3DTilesTester.tileDestroys = function (scene, url, options) {

Cesium3DTilesTester.generateBatchedTileBuffer = function (options) {
// Procedurally generate the tile array buffer for testing purposes
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const magic = defaultValue(options.magic, [98, 51, 100, 109]);
const version = defaultValue(options.version, 1);
const featuresLength = defaultValue(options.featuresLength, 1);
options = options ?? Frozen.EMPTY_OBJECT;
const magic = options.magic ?? [98, 51, 100, 109];
const version = options.version ?? 1;
const featuresLength = options.featuresLength ?? 1;
const featureTableJson = {
BATCH_LENGTH: featuresLength,
};
Expand Down Expand Up @@ -207,12 +201,12 @@ Cesium3DTilesTester.generateBatchedTileBuffer = function (options) {

Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
// Procedurally generate the tile array buffer for testing purposes
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const magic = defaultValue(options.magic, [105, 51, 100, 109]);
const version = defaultValue(options.version, 1);
options = options ?? Frozen.EMPTY_OBJECT;
const magic = options.magic ?? [105, 51, 100, 109];
const version = options.version ?? 1;

const gltfFormat = defaultValue(options.gltfFormat, 1);
const gltfUri = defaultValue(options.gltfUri, "model.gltf");
const gltfFormat = options.gltfFormat ?? 1;
const gltfUri = options.gltfUri ?? "model.gltf";
const gltfUriByteLength = gltfUri.length;

const featureTableJson = options.featureTableJson;
Expand All @@ -222,7 +216,7 @@ Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
featureTableJsonString = JSON.stringify(featureTableJson);
}
} else {
const featuresLength = defaultValue(options.featuresLength, 1);
const featuresLength = options.featuresLength ?? 1;
featureTableJsonString = JSON.stringify({
INSTANCES_LENGTH: featuresLength,
POSITION: new Array(featuresLength * 3).fill(0),
Expand All @@ -231,10 +225,7 @@ Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
featureTableJsonString = padStringToByteAlignment(featureTableJsonString, 8);
const featureTableJsonByteLength = featureTableJsonString.length;

const featureTableBinary = defaultValue(
options.featureTableBinary,
new Uint8Array(0),
);
const featureTableBinary = options.featureTableBinary ?? new Uint8Array(0);
const featureTableBinaryByteLength = featureTableBinary.length;

const batchTableJson = options.batchTableJson;
Expand All @@ -245,10 +236,7 @@ Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {
batchTableJsonString = padStringToByteAlignment(batchTableJsonString, 8);
const batchTableJsonByteLength = batchTableJsonString.length;

const batchTableBinary = defaultValue(
options.batchTableBinary,
new Uint8Array(0),
);
const batchTableBinary = options.batchTableBinary ?? new Uint8Array(0);
const batchTableBinaryByteLength = batchTableBinary.length;

const headerByteLength = 32;
Expand Down Expand Up @@ -300,9 +288,9 @@ Cesium3DTilesTester.generateInstancedTileBuffer = function (options) {

Cesium3DTilesTester.generatePointCloudTileBuffer = function (options) {
// Procedurally generate the tile array buffer for testing purposes
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const magic = defaultValue(options.magic, [112, 110, 116, 115]);
const version = defaultValue(options.version, 1);
options = options ?? Frozen.EMPTY_OBJECT;
const magic = options.magic ?? [112, 110, 116, 115];
const version = options.version ?? 1;
let featureTableJson = options.featureTableJson;
if (!defined(featureTableJson)) {
featureTableJson = {
Expand All @@ -315,10 +303,8 @@ Cesium3DTilesTester.generatePointCloudTileBuffer = function (options) {

let featureTableJsonString = JSON.stringify(featureTableJson);
featureTableJsonString = padStringToByteAlignment(featureTableJsonString, 4);
const featureTableJsonByteLength = defaultValue(
options.featureTableJsonByteLength,
featureTableJsonString.length,
);
const featureTableJsonByteLength =
options.featureTableJsonByteLength ?? featureTableJsonString.length;

const featureTableBinary = new ArrayBuffer(12); // Enough space to hold 3 floats
const featureTableBinaryByteLength = featureTableBinary.byteLength;
Expand Down Expand Up @@ -356,10 +342,10 @@ Cesium3DTilesTester.generatePointCloudTileBuffer = function (options) {

Cesium3DTilesTester.generateCompositeTileBuffer = function (options) {
// Procedurally generate the tile array buffer for testing purposes
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const magic = defaultValue(options.magic, [99, 109, 112, 116]);
const version = defaultValue(options.version, 1);
const tiles = defaultValue(options.tiles, []);
options = options ?? Frozen.EMPTY_OBJECT;
const magic = options.magic ?? [99, 109, 112, 116];
const version = options.version ?? 1;
const tiles = options.tiles ?? Frozen.EMPTY_ARRAY;
const tilesLength = tiles.length;

let i;
Expand Down Expand Up @@ -393,20 +379,20 @@ Cesium3DTilesTester.generateCompositeTileBuffer = function (options) {

Cesium3DTilesTester.generateVectorTileBuffer = function (options) {
// Procedurally generate the tile array buffer for testing purposes
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const magic = defaultValue(options.magic, [118, 99, 116, 114]);
const version = defaultValue(options.version, 1);
options = options ?? Frozen.EMPTY_OBJECT;
const magic = options.magic ?? [118, 99, 116, 114];
const version = options.version ?? 1;

let featureTableJsonString;
let featureTableJsonByteLength = 0;
const defineFeatureTable = defaultValue(options.defineFeatureTable, true);
const defineFeatureTable = options.defineFeatureTable ?? true;
if (defineFeatureTable) {
const defineRegion = defaultValue(options.defineRegion, true);
const defineRegion = options.defineRegion ?? true;
const featureTableJson = {
REGION: defineRegion ? [-1.0, -1.0, 1.0, 1.0, -1.0, 1.0] : undefined,
POLYGONS_LENGTH: defaultValue(options.polygonsLength, 0),
POLYLINES_LENGTH: defaultValue(options.polylinesLength, 0),
POINTS_LENGTH: defaultValue(options.pointsLength, 0),
POLYGONS_LENGTH: options.polygonsLength ?? 0,
POLYLINES_LENGTH: options.polylinesLength ?? 0,
POINTS_LENGTH: options.pointsLength ?? 0,
POLYGON_BATCH_IDS: options.polygonBatchIds,
POLYLINE_BATCH_IDS: options.polylineBatchIds,
POINT_BATCH_IDS: options.pointBatchIds,
Expand Down Expand Up @@ -446,19 +432,19 @@ Cesium3DTilesTester.generateVectorTileBuffer = function (options) {

Cesium3DTilesTester.generateGeometryTileBuffer = function (options) {
// Procedurally generate the tile array buffer for testing purposes
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
const magic = defaultValue(options.magic, [103, 101, 111, 109]);
const version = defaultValue(options.version, 1);
options = options ?? Frozen.EMPTY_OBJECT;
const magic = options.magic ?? [103, 101, 111, 109];
const version = options.version ?? 1;

let featureTableJsonString;
let featureTableJsonByteLength = 0;
const defineFeatureTable = defaultValue(options.defineFeatureTable, true);
const defineFeatureTable = options.defineFeatureTable ?? true;
if (defineFeatureTable) {
const featureTableJson = {
BOXES_LENGTH: defaultValue(options.boxesLength, 0),
CYLINDERS_LENGTH: defaultValue(options.cylindersLength, 0),
ELLIPSOIDS_LENGTH: defaultValue(options.ellipsoidsLength, 0),
SPHERES_LENGTH: defaultValue(options.spheresLength, 0),
BOXES_LENGTH: options.boxesLength ?? 0,
CYLINDERS_LENGTH: options.cylindersLength ?? 0,
ELLIPSOIDS_LENGTH: options.ellipsoidsLength ?? 0,
SPHERES_LENGTH: options.spheresLength ?? 0,
BOX_BATCH_IDS: options.boxBatchIds,
CYLINDER_BATCH_IDS: options.cylinderBatchIds,
ELLIPSOID_BATCH_IDS: options.ellipsoidBatchIds,
Expand Down
Loading