dsriseah / ursys

an opinionated javascript library for prototyping realtime web apps
1 stars 2 forks source link

Codestyle: Return undefined or empty array for null collection operations? #8

Open dsriseah opened 1 month ago

dsriseah commented 1 month ago

How to handle the case where a function returns a collection?

const ADDR_MAP = new Map();
function GetAddressList() {
  const values = [...ADDR_MAP.values()];
  return values;
}

Alternatively, we could do something like:

const ADDR_MAP = new Map();
function GetAddresses() {
  if (ADDR_MAP.size===0) return undefined;
  const values = [...ADDR_MAP.values()];
  return values;
}

In this case, undefined is returned if there are no addresses. I think the first case is probably preferable...we can assume that a GetList operation always returns an array, as well as for Find and Match operations.

However, in the case of a GetItem, we would want to return undefined if it doesn't exist.