Menu Close

Riddle about low-level code quality

Question: what does the code below do?

export function compact(array: any[]) {
    var index = -1,
        length = array === null ? 0 : array.length,
        resIndex = 0,
        result = [];

    while (++index < length) {
        var value = array[index];
        if (value) {
            result[resIndex++] = value;
    return result;

Answer: same thing that this code does:

export const compact = (array: any[]) => array.filter(x => x);

Code in the question is about 3 weeks old, written in green-field project, has no documentation or tests (rightfully so, functionality is too trivial to deserve them). Time needed to understand the implementation from the answer is at least order of magnitude shorter then time needed to understand original code. Same goes for actually writing it. Avoidable with simple code-review… .

Code quality matters as it’s influence on performance/delivery schedule is cumulative in it’s nature.

Leave a Reply

Your email address will not be published. Required fields are marked *