saleor / saleor-storefront

A GraphQL-powered, NextJs-based, PWA storefront for Saleor. IMPORTANT: This project is [DEPRECATED] in favor of saleor/react-storefront soon to become our default demo and storefront starter pack.
https://demo.saleor.io/
BSD 3-Clause "New" or "Revised" License
769 stars 672 forks source link

Improve readability in the code by adding id-length rule #911

Closed HarisSpahija closed 3 years ago

HarisSpahija commented 4 years ago

What I'm trying to achieve

Right now it is almost impossible to know what some functions do due to shortcutting identifiers. Especially shortcutting definitions like the code example below:

export const maybe = <T>(exp: () => T, d?: T) => {
  try {
    const result = exp();
    return result === undefined ? d : result;
  } catch {
    return d;
  }
};

This is terrible to read and very hard for new developers to understand what this function does. The utility function is not descriptive nor do the parameters describe what they stand for.

Describe a proposed solution

I would like to add a rule to the eslint called id-length https://eslint.org/docs/rules/id-length

The end result of this function would be

export const tryExpression = <T>(expression: () => T, data?: T) => {
  try {
    const result = expression();
    return result === undefined ? data : result;
  } catch {
    return data;
  }
};

The function is descriptive and also the parameters are a lot more readable.

Alternative

An alternative would be to add JSDoc to the codebase that will help describe what functions and classes do. For now it seems redundant due to the amount of typescript already covering most of what JSDoc does.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.