Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
Is it good practice to surround code with braces?

Is it okay to surround code with braces { } for making code in IDE's compress/collapse?

For example:

{
    function foo(a,b) {
        return a*b+b;
    }

    function bar(a,b,c) {
        return a*b+c;
    }
}

This would collapse in my IDE to the following:

{ ...
}

Are these extra braces allowed, or would it be syntactically incorrect? (I know it works because the code has run without any errors regarding syntax.)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You've already answered your question:

Are these extra braces allowed, or would it be syntactically incorrect?

with

(I know it works because the code has run without any errors regarding syntax)

It is syntactically permitted, as of ES2015.

But, these sorts of plain blocks have some issues:

  • Function declarations inside them work very strangely (so using them is likely to confuse people in some circumstances)
  • They're quite unusual to see - generally, script-writers do not use them, and do not expect them, so using such a block is likely to confuse some

If you need the ability to "compress" code - to group parts of related code without cluttering up a script - consider using modules instead. For example

// fooAndBar.js
export function foo(a, b) {
  return a*b+b;
}
export function bar(a, b, c) {
  return a*b+c;
}

Then just import the functions when needed.

It's a lot easier to maintain and debug 8 files that are 50 lines each than it is to do the same for one file that's 400 lines each. If you have to collapse blocks to make something easier to read, it's probably time to refactor to another file.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda