Skip to content

Commit

Permalink
commitlint: honor our own WorkflowGuidelines (#172)
Browse files Browse the repository at this point in the history
The workflow guidelines state that we should avoid the keyword
"undefined". This is also extra good because it seems that the
`?`-based guards against both null and undefined, at least
according to this example:

conventional-changelog/commitlint@7ab4bab
  • Loading branch information
knocte authored Aug 31, 2024
1 parent ca5ef48 commit 61d1263
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
7 changes: 2 additions & 5 deletions commitlint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ export abstract class Helpers {
potentialString: any,
paramName: string
): string | null {
if (potentialString === null || potentialString === undefined) {
// otherwise, String(null) might give us the stupid string "null"
return null;
}
return String(potentialString);
// this null/undefined check is required, otherwise, String(null) might give us the stupid string "null"
return potentialString ? String(potentialString) : null;
}

public static assertNotNull(
Expand Down
19 changes: 18 additions & 1 deletion docs/WorkflowGuidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,26 @@
make use of the type system whenever you can, for example:

* Do not use edge-values to denote absence of value. Example: use null (`Nullable<DateTime>`) instead of `DateTime.MinValue`.
* Use Option types instead of Nullable ones if your language provides it (e.g. if you're using F# instead of C#).
* Do not use `undefined` which is a pitfall from JavaScript (the fact that it has two kinds of null values is a defect in
its design). As we're using TypeScript we should be able to avoid the uglyness of JavaScript.
* Use Option types instead of Nullable ones if your language provides it (e.g. if you're using F# instead of C#).

Example (with bad practice):
```typescript
if (foo === undefined || foo === null)
{
return 0;
}
else
{
return 1;
}
```

Improved code:
```typescript
return foo ? 1 : 0;
```

* Abusing obscure operators or the excessive multi-facetedness of basic ones:

Expand Down

0 comments on commit 61d1263

Please sign in to comment.