Project

General

Profile

Cleanup #46337

mgr/dashboard: remove `or undefined` from method return types

Added by Patrick Seidensal over 3 years ago. Updated almost 3 years ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
General
Target version:
-
% Done:

0%

Tags:
frontend clean-up
Backport:
Reviewed:
Affected Versions:
Pull request ID:

Description

Since a rule for checking TypeScript was updated, many methods and functions in the code were updated to contain multiple return types, like `string | undefined`. This was done to prevent having to make changes in those methods that could potentially introduce issues. But in its current state, the code seems to invite developers to use exactly this pattern for creating new code, which kind of defeats the purpose of static typing but it at least a little bit confusing and unclean.

Returning and empty string as "false" is in most cases (maybe in all cases) not only viable but clear and concise. When a method returns two types, the return type needs to be checked before it can be processed further. Furthermore, other variations of multiple return types can be added with the same result. Instead of just returning an empty string, `undefined`, `null` or `boolean` (false) can be used, mostly interchangeably. This is not only unnecessary but also confusing and will cause additional work, if those result values are used in TypeScript (instead of Angular Templates).

I therefore recommend to stick to a single return type whenever possible, which should be possible in most cases. A string return type returns an empty string for "false" and also evaluates to `false` when tested. The same applies to `number` and `0`. This does not work for empty objects. Empty objects in JavaScript evaluate to `true`. In that case I would prefer to return `null`, which evaluates to false. I would not prefer to return `undefined`. `undefined` is the the value that is returned for attributes of an object that aren't defined. The benefit of returning `null` (but also `undefined`) is that the type `object` can still be used without having to add any other type to that declaration. When using `null` as return type it can be differentiated between an attribute that has not been defined or an object that was expected but not returned.

The frontend code needs to be adapted accordingly.

History

#1 Updated by Ernesto Puerta almost 3 years ago

  • Project changed from mgr to Dashboard
  • Category changed from 132 to General

Also available in: Atom PDF