Forem

Cover image for Comentários (Código Limpo: Que Bruxaria é Essa?!?! - Parte Final)
ananopaisdojavascript
ananopaisdojavascript

Posted on

3 2

Comentários (Código Limpo: Que Bruxaria é Essa?!?! - Parte Final)

Comente apenas códigos com lógica de negócio complexa

Comentários são um pedido de desculpas, não uma necessidade. Um bom código se documenta por si só na maioria das vezes.

Não é recomendável:

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;
  }
}
Enter fullscreen mode Exit fullscreen mode

É recomendável:

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;
  }
}
Enter fullscreen mode Exit fullscreen mode

Não deixe código comentado na sua base de códigos

Controle de versão existe por um motivo. Deixe o código antigo no seu histórico.

Não é recomendável:

doStuff();
// doOtherStuff();
// doSomeMoreStuff();
// doSoMuchStuff();
Enter fullscreen mode Exit fullscreen mode

É recomendável:

doStuff();
Enter fullscreen mode Exit fullscreen mode

Não tenha diários de comentários

Lembre-se, use controle de versão! Não há necessidade de código morto, código comentado e muito menos diário de comentários. Use git log para acessar o histórico!

Não é recomendável:

/**
 * 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;
}
Enter fullscreen mode Exit fullscreen mode

É recomendável:

function combine(a, b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Evite marcadores de posição

Marcadores de posição só deixam sujeira. Deixe as funções e nomes de variáveis com o alinhamento e formatação adequados, dando estrutura visual ao seu código.

Não é recomendável:

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

////////////////////////////////////////////////////////////////////////////////
// Action setup
////////////////////////////////////////////////////////////////////////////////
const actions = function() {
  // ...
};
Enter fullscreen mode Exit fullscreen mode

É recomendável:

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

const actions = function() {
  // ...
};
Enter fullscreen mode Exit fullscreen mode

E aí? Gostaram? Até a próxima anotação! 😊

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay