Skip to main content
added 19 characters in body
Source Link
Billal Begueradj
  • 3.4k
  • 10
  • 25
  • 34

It is inefficient and overengineered because the code

  1. loops through the transactions several times when once would be enough, both by the multiple filters and the two separate loops.

  2. uses more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. calls moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in the team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex. (See also any article on KISS)

You are overthinking this with filter and reduce and consuming more memory and cpu than needed in the process. If I got this code I would think that the interviewee was trying to impress me that they know about filter and reduce but the code ends up way longer and more complex than needed.

A simpler version would be

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also https://stackoverflow.com/questions/45691907/is-using-several-filter-calls-on-a-big-array-bad-for-performance-in-javascripthis.

It is inefficient and overengineered because the code

  1. loops through the transactions several times when once would be enough, both by the multiple filters and the two separate loops.

  2. uses more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. calls moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in the team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex. (See also any article on KISS)

You are overthinking this with filter and reduce and consuming more memory and cpu than needed in the process. If I got this code I would think that the interviewee was trying to impress me that they know about filter and reduce but the code ends up way longer and more complex than needed.

A simpler version would be

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also https://stackoverflow.com/questions/45691907/is-using-several-filter-calls-on-a-big-array-bad-for-performance-in-javascrip

It is inefficient and overengineered because the code

  1. loops through the transactions several times when once would be enough, both by the multiple filters and the two separate loops.

  2. uses more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. calls moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in the team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex. (See also any article on KISS)

You are overthinking this with filter and reduce and consuming more memory and cpu than needed in the process. If I got this code I would think that the interviewee was trying to impress me that they know about filter and reduce but the code ends up way longer and more complex than needed.

A simpler version would be

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also this.

depersonalised some more, improving the tone
Source Link
user985366
  • 1.7k
  • 9
  • 13

It is inefficient and overengineered because yourthe code is

  1. loopingloops through the transactions several times when once would be enough, both by the multiple filters and the two separate loops.

  2. usinguses more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. callingcalls moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in yourthe team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex. (See also any article on KISS)

You are overthinking this with filter and reduce and consuming more memory and cpu than you needneeded in the process. If I got this code I would think that you werethe interviewee was trying to impress me that youthey know about filter and reduce but the code you wrote isends up way longer and more complex than needed.

What you could do is sort ofA simpler version would be

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also https://stackoverflow.com/questions/45691907/is-using-several-filter-calls-on-a-big-array-bad-for-performance-in-javascrip

It is inefficient and overengineered because your code is

  1. looping through the transactions several times when once would be enough, both by the multiple filters and the two separate loops.

  2. using more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. calling moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in your team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex. (See also any article on KISS)

You are overthinking this with filter and reduce and consuming more memory and cpu than you need in the process. If I got this code I would think that you were trying to impress me that you know about filter and reduce but the code you wrote is way longer than needed.

What you could do is sort of

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also https://stackoverflow.com/questions/45691907/is-using-several-filter-calls-on-a-big-array-bad-for-performance-in-javascrip

It is inefficient and overengineered because the code

  1. loops through the transactions several times when once would be enough, both by the multiple filters and the two separate loops.

  2. uses more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. calls moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in the team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex. (See also any article on KISS)

You are overthinking this with filter and reduce and consuming more memory and cpu than needed in the process. If I got this code I would think that the interviewee was trying to impress me that they know about filter and reduce but the code ends up way longer and more complex than needed.

A simpler version would be

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also https://stackoverflow.com/questions/45691907/is-using-several-filter-calls-on-a-big-array-bad-for-performance-in-javascrip

depersonalised by discussing the code rather than the person
Source Link
user985366
  • 1.7k
  • 9
  • 13

It is inefficient and overengineered because you areyour code is

  1. looping through the transactions several times when once would be enough, both by yourthe multiple filters and yourthe two separate loops.

  2. using more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. You are calling moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in your team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex. (See also any article on KISS)

You are overthinking this with filter and reduce and consuming more memory and cpu than you need in the process. If I got this code I would think that you were trying to impress me that you know about filter and reduce but the code you wrote is way longer than needed.

What you could do is sort of

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also https://stackoverflow.com/questions/45691907/is-using-several-filter-calls-on-a-big-array-bad-for-performance-in-javascrip

It is inefficient and overengineered because you are

  1. looping through the transactions several times when once would be enough, both by your multiple filters and your two separate loops.

  2. using more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. You are calling moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in your team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex.

You are overthinking this with filter and reduce and consuming more memory and cpu than you need in the process. If I got this code I would think that you were trying to impress me that you know about filter and reduce but the code you wrote is way longer than needed.

What you could do is sort of

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also https://stackoverflow.com/questions/45691907/is-using-several-filter-calls-on-a-big-array-bad-for-performance-in-javascrip

It is inefficient and overengineered because your code is

  1. looping through the transactions several times when once would be enough, both by the multiple filters and the two separate loops.

  2. using more memory than needed, partly due to the above and partly due to the creation of the array "filteredTransactions"

  3. calling moment() up to twice to convert the timestamp, when once would suffice.

It is less maintainable / understandable by others in your team because

  1. some may not commonly use map/filter and most may not commonly use reduce which is particularly complex. (See also any article on KISS)

You are overthinking this with filter and reduce and consuming more memory and cpu than you need in the process. If I got this code I would think that you were trying to impress me that you know about filter and reduce but the code you wrote is way longer than needed.

What you could do is sort of

sum = 0
transactions.forEach(t => {
    if (t.category == category) {
        let d = moment(transactionDate)
        if (d.isSameOrAfter(start) && d.isBefore(end)) {
            sum += d.amount
        }
    }
})
return sum

This code is half the lines and almost no intermediate variables.

It does not require the next guy to understand filter, map or reduce.

It does not look at any transaction more than once.

It does not create 3 intermediate lists of transactions / numbers.

See also https://stackoverflow.com/questions/45691907/is-using-several-filter-calls-on-a-big-array-bad-for-performance-in-javascrip

added 101 characters in body
Source Link
user985366
  • 1.7k
  • 9
  • 13
Loading
Source Link
user985366
  • 1.7k
  • 9
  • 13
Loading