Skip to content

Kommentare

Kommentieren Sie nur Dinge mit komplexer Geschäftslogik.

Kommentare sind eine Entschuldigung, keine Notwendigkeit. Guter Code dokumentiert sich weitgehend selbst.

Schlecht:

javascript
function hashIt(data) {
  // The hash
  let hash = 0;

  // Length of string
  const length = data.length;

  // Loop through every character in data
  for (let i = 0; i < length; i++) {
    // Get character code.
    const char = data.charCodeAt(i);
    // Make the hash
    hash = (hash << 5) - hash + char;
    // Convert to 32-bit integer
    hash &= hash;
  }
}

Gut:

javascript
function hashIt(data) {
  let hash = 0;
  const length = data.length;

  for (let i = 0; i < length; i++) {
    const char = data.charCodeAt(i);
    hash = (hash << 5) - hash + char;

    // Convert to 32-bit integer
    hash &= hash;
  }
}

Lassen Sie keinen auskommentierten Code in Ihrer Codebase zurück.

Versionsverwaltungssysteme gibt es aus einem Grund. Behalten Sie alten Code in Ihrer Historie.

Schlecht:

javascript
doStuff();
// doOtherStuff();
// doSomeMoreStuff();
// doSoMuchStuff();

Gut:

javascript
doStuff();

Vermeiden Sie Journalkommentare

Denken Sie daran: Nutzen Sie Versionsverwaltung! Toter Code, auskommentierter Code und insbesondere Journalkommentare sind unnötig. Verwenden Sie git log, um den Verlauf einzusehen.

Schlecht:

javascript
/**
 * 2016-12-20: Removed monads, didn't understand them (RM)
 * 2016-10-01: Improved using special monads (JP)
 * 2016-02-03: Removed type-checking (LI)
 * 2015-03-14: Added combine with type-checking (JR)
 */
function combine(a, b) {
  return a + b;
}

Gut:

javascript
function combine(a, b) {
  return a + b;
}

Vermeiden Sie Positionsmarkierungen

Sie fügen meist nur Störungen hinzu. Lassen Sie Funktionen, Variablennamen sowie korrekte Einrückung und Formatierung die visuelle Struktur Ihres Codes definieren.

Schlecht:

javascript
////////////////////////////////////////////////////////////////////////////////
// Scope Model Instantiation
////////////////////////////////////////////////////////////////////////////////
$scope.model = {
  menu: "foo",
  nav: "bar"
};

////////////////////////////////////////////////////////////////////////////////
// Action setup
////////////////////////////////////////////////////////////////////////////////
const actions = function() {
  // ...
};

Gut:

javascript
$scope.model = {
  menu: "foo",
  nav: "bar"
};

const actions = function() {
  // ...
};