Skip to content

Latest commit

 

History

History
131 lines (111 loc) · 2.57 KB

RULES.md

File metadata and controls

131 lines (111 loc) · 2.57 KB

Rules

Prefer chaining

  // Wrong
  const groupedOrders = _.groupBy(orders, (orderItem) => orderItem.transaction_id);
  const result = _.map(groupedOrders, (groupedOrders) => doSomething(groupedOrders));

  // Correct
  _.chain(orders)
    .groupBy((orderItem) => orderItem.transaction_id)
    .map((groupedOrders) => doSomething(groupedOrders))
    .value();

Prefer explicit methods

Use utility methods that conveys your logic rather than writing the logic imperatively.

  const names = ['a', 'b', 'c'];
  const ids = [1, 2, 3];

  // Wrong
  _.map(names, (name, index) => {
    const id = ids[index];
    ...
  });
  // Correct
  _.map(_.zip(names, ids), [name, id] => {
    ...
  });

Conflicting objects:

  // Wrong:
  const superCategory = "fire"
  const superCategoryObject = _.find(CONFIG.***, ***)

  // Correct:
  const superCategoryName = "fire"
  const superCategoryConfig = _.find(CONFIG.***, ***)

Extracting Variables:

  // Wrong:
  const inputEmail = '[email protected]'
  _.map(allowedEmails, (email) => { *** });

  // Correct
  const inputEmail = '[email protected]'
  _.map(allowedEmails, (allowedEmail) => { *** });

Random variable names

  // Wrong
  temp = get_house_price_in_usd(house_sqft, house_room_count)
  final_value = temp * usd_to_aud_conversion_rate

  // Correct
  house_price_in_usd = get_house_price_in_usd(house_sqft, house_room_count)
  house_price_in_aud = house_price_in_usd * usd_to_aud_conversion_rate
  // Wrong
  temp = m1 * x1 + m2 * (x2 ** 2)
  final = temp + b

  // Correct
  house_price = price_per_room * rooms + price_per_floor_squared * (floors ** 2)
  house_price = house_price + expected_mean_house_price

Encourage truth variable names

  // Wrong
  if (isDisabled) { ....
  if (!isDisabled) { ...

  // Correct
  if(isEnabled) { ....
  if(!isEnabled) { ....

Symmetrical Naming:

  // Wrong
  const parser = {
    parseProductCode: () => { ... },
    parseQuantity: () => { ... },
    tradingSymbolParse: () => { ... },
  }

  // Correct
  const parser = {
    parseProductCode: () => { ... },
    parseQuantity: () => { ... },
    parseTradingSymbol: () => { ... },
  }

Early Return instead of if else

  // Wrong
  const fn = () => {
    if (isValid()) {
      if (isSafe()) {
        return doWork();
      } else {
        return []
      }
    } else {
      throw new Error('Not Valid');
    }
  }

  // Correct
  const fn = () => {
    if (!isValid()) {
      throw new Error('Not Valid');
    }
    if (!isSafe()) {
      return [];
    }
    return doWork();
  }