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

325
Visualizações
Return from function or pass into function?

What would be the better way to process this data? Is this just a matter of personal preference? Guessing 2 is less prone to accidently modifying the monday...friday arrays and there is less code at this layer. Is there a best practices approach? Is there a performance difference?

  1. Like this?
        // Arrays to hold what stock price belongs to what day of the week - Note these arrays are used some way down the line
        var monday = [], tuesday = [], wednesday = [], thursday = [], friday = []

        // Sort each stock date into a day of the week
        FindDayFromDate(historyData, monday, tuesday, wednesday, thursday, friday);

        // Find the average price for each day of the week
        const avgPrices = FindAveragePricePerDay(monday, tuesday, wednesday, thursday, friday);
  1. Like this?
        // Sort each stock date into a day of the week - sortedDays contains arrays of monday...friday
        const sortedDays = FindDayFromDate(historyData);

        // Find the average price for each day of the week - Expand sortedDays inside function to access monday...friday
        const avgPrices = FindAveragePricePerDay(sortedDays);
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

A good function should always be free from side-effects. It should be pure and just do one specific job at a time. As a software programmer/developer, we must write source code that actually produce the output based on input. Such kind of strong pure function actually avoid all kind of side effects and code smell. Let's look at few examples to understand this.

Assume we have a function named as Greeting like below

function Greeting(name) {
  return `Hello ${name}`;
}

It is pure because you will always get the output Hello {name} for the name passed via parameter. Let's have a look at another version of same function with many side-effects.

var greeting = "Hey";

function Greeting(name) {
  return `${greeting} ${name}`;
}

Now, this function is not pure. Guess why ? Because we are not sure what output we will receive because function is actually dependent on the outer variable named as greeting . So if someone change the value of variable greeting to something else let's say Hello then obviously the output of function will change. So the function is now tightly coupled with the outer variable. That's why it is not pure function anymore.

So to conclude, you must have to write your function in such a way that there is no dependency and no side-effect and it will always do one job.

That is the best practice :)

Happy Coding :)

about 4 years ago · Santiago Gelvez Relatório

0

I believe the second Function "FindAveragePricePerDay" calling can be adjusted to act on single day, so that it can be reused on other places

    const avgPrices = sortedDays.map(day => FindAveragePricePerDay(day));
    /* you can also simplify the FindAveragePricePerDay using the reduce method
     following this parameters array.reduce(function(total, currentValue, currentIndex, arr), initialValue) 
so all the second line would be */
    const avgPrices = day.reduce((t,c,i,arr) => t + c/arr.length, 0);

for more info about array.reduce check this

about 4 years ago · Santiago Gelvez 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