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

120
Visualizações
open closed principle in functional programming?

I have been reading some articles about the open-closed principle and all seem to be related to OOP.

Is there a way to do open-closed principle in functional programming using Node.JS for example?

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

0

There's several ways to achieve this principle, for example if you create classes, you can inherit the functions and then extend its functionality. For example:

class NumberConverter {
  isNumber(number) {
    return true;
  }

  convertBase(number, fromBase, toBase) {
    return parseInt(number, fromBase).toString(toBase);
  }
}

class DecimalToBinary extends NumberConverter {
  isDecimalNumber(number) {
    return true;
  }

  dec2bin(number) {
    return this.convertBase(number, 10, 2);
  }
}

You can see how you can extend the convertBase and extend its functionality. You can extend from multiple classes just adding a comma after each class like extends Products, Music.

But if you want to create another kind of approach, you can do composition instead of inheritance, meaning that with the previous example, you can make a new class like so:

class Calculator {
  properties;
  numberConverter;

  setDecimalConverter(numberConverter) {
    this.numberConverter = numberConverter;
  }

  convertDec2bin(number) {
    return this.numberConverter.convertBase(number, 10, 2);
  }
}

So then you can use the new class Calculator like this:

const calculator = new Calculator();
calculator.numberConverter = new NumberConverter(); // If you don't want to do this, you would have to add the NumberConverter in the Calculator constructor
calculator.convertDec2bin(5);

And all of this functionality can be done as a function. (Although it might not be exactly an open-closed principle, I think it still solves your use case). You can use it like this:

const Calculator = function(NumberConverter) {
  const numberConverter = NumberConverter;

  return {
    convertDec2bin(number) {
      return numberConverter.convertBase(number, 10, 2);
    }
  }  
}

const calculator = Calculator(NumberConverter());
calculator.convertDec2bin(5)
about 4 years ago · Juan Pablo Isaza Relatório

0

Sure, indeed FP is all about reusing functions to build new ones.

For example:

You can declare a function add :: Number -> Number -> Number

const add = (x) => (y) => x + y

And then use add to build another function like mult :: Number -> Number -> Number

const mult = (x) => (y) => y === 0 ? 0 : x + mult(x)(y - 1)

Each function could be considered atomic, as there's no need to do further changes. So, by this way you don't modify previous functions, you re-use them to form new ones.

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