GoogleChromeLabs / react-adaptive-hooks

Deliver experiences best suited to a user's device and network constraints
Apache License 2.0
5.1k stars 113 forks source link

Code styling #37

Open wingleung opened 4 years ago

wingleung commented 4 years ago

As of now, the code is very verbose and easy to read but I'm wondering if we can optimize readability by tweaking some code styles and namings to prevent double negatives.

For example, the following code...

let unsupported;

const useSaveData = (initialSaveDataStatus = null) => {
  if ('connection' in navigator && 'saveData' in navigator.connection) {
    unsupported = false;
  } else {
    unsupported = true;
  }

  return {
    unsupported,
    saveData: unsupported
      ? initialSaveDataStatus
      : navigator.connection.saveData === true
  };
};

export { useSaveData };

could become...

const useSaveData = (initialSaveDataStatus = null) => {
  const supported = ('connection' in navigator && 'saveData' in navigator.connection)

  return {
    unsupported: !supported,
    saveData: supported
        ? navigator.connection.saveData === true
        : initialSaveDataStatus
  };
};

export { useSaveData };

maybe even use a supported property in the return value, I'm not sure if it's deliberate that we use negative naming for this.

anton-karlovskiy commented 4 years ago

@wingleung cc @addyosmani

I think we can use supported. At the beginning I named like so, but found that it could be better if we used supported.