←back to thread

498 points azhenley | 1 comments | | HN request time: 0s | source
Show context
QuadrupleA ◴[] No.45773004[source]
Love Carmack, but hard disagree on this and a lot of similar functional programming dogma. I find this type of thing very helpful:

    classList = ['highlighted', 'primary']
    if discount:
        classList.append('on-sale')
    classList = ' '.join(classList)
And not having to think about e.g. `const` vs `let` frees up needless cognitive load, which is why I think python (rightly) chose to not make it an option.
replies(3): >>45774119 #>>45776812 #>>45776933 #
1. ColeShepherd ◴[] No.45774119[source]
Some potential alternatives to consider:

1.

    classList = ['highlighted', 'primary']
        .concatif(discount, 'on-sale')
        .join(' ')
2.

    classList = ' '.join(['highlighted', 'primary'] + (['on-sale'] if discount else []))
3.

    mut classList = ['highlighted', 'primary']
    if discount:
        classList.append('on-sale')
    classList = ' '.join(classList)

    freeze classList

4.

    def get_class_list(discount):
        mut classList = ['highlighted', 'primary']
        if discount:
            classList.append('on-sale')
        classList = ' '.join(classList)
        return classList

    classList = get_class_list(discount)