deno-docs icon indicating copy to clipboard operation
deno-docs copied to clipboard

Add abbreviation section to style guide

Open timreichen opened this issue 1 year ago • 1 comments

I think it would be beneficial to add a section about abbreviations. WDYT?

Example:

Do not use abbreviations

We prefer descriptive null names for functions, properties, variables and arguments over abbreviated names.

Good:

interface CartItem {
  price: number;
  quantity: number;
}
function calculateTotalCartPrice(cart: CartItem[]) {
  let totalPrice = 0;
  for (const item of cart) {
    totalPrice += item.price * item.quantity;
  }
  return totalPrice;
}

const shoppingCart = [
  { pr: 10, qty: 2 },
  { pr: 5, qty: 4 },
  { pr: 20, qty: 1 },
];
const totalPrice = calculateTotalCartPrice(shoppingCart);

Bad:

interface CartItem {
  price: number;
  quantity: number;
}

function calcTotal(c: CartItem[]) {
  let tot = 0;
  for (const itm of c) {
    tot += itm.pr * itm.qty;
  }
  return tot;
}

const c = [
  { pr: 10, qty: 2 },
  { pr: 5, qty: 4 },
  { pr: 20, qty: 1 },
];
const total = calcTotal(c);

timreichen avatar Jul 06 '24 08:07 timreichen

IMO this is quite a subtle needle to thread in statically typed languages, because you also shouldn't necessarily be duplicating information that's already encoded in types (or should be), in names.

frou avatar Jul 12 '24 09:07 frou

Abbreviations in variable names, function names, argument names, etc are very common across Deno projects (Deno CLI, std, etc). (For example, rid, conn, req, res, etc)

I don't think this suggestion is aligned with the existing state of our preference

kt3k avatar May 08 '25 19:05 kt3k