4 ms·
> The second relies on two external functions and is one line long. > console.log(sum(range(1, 10))); > Which one is more likely to contain a bug? I'd say th
by amelius 12y ago
> The second relies on two external functions and is one line long.
> console.log(sum(range(1, 10)));
> Which one is more likely to contain a bug?
I'd say the second one. Because the first one is totally transparent. The second one relies on functions I can't see. Especially the "range" function may cause problems because here it generates the full range, including both delimiters, unlike the range function in e.g. python.
EDIT: functional programming is extremely useful, but I don't think this example really highlights the most important advantages.
- netcraft 12y agobut sum and range are able to be tested in isolation and will be reused across the application, making a bug in them more visible - rather than repeating the logic of the loop in every instance where you need a loop. Also, when maintaining the code later, the signal to noise ratio is much higher, you can see exactly what is being attempted, not the implementation details of it. There are certainly times when you need to see the implementation details or that your needs don't fit the prescribed and available functions (eg, range being exclusive vs inclusive), but then if that is the exception rather than the rule, it will serve as another indicator when you come across that code that `this is doing something different than usual`, so pay attention to the details.
- deleted 12y ago[deleted]
- iopq 12y agoand then you forget range(1, 10) is a half-open interval because it's meant to be used with `length` as in `range(0, foo.length)`
- serrimo 12y agoThat part has always annoyed me. But then Python beat me into submission with its seductive collection comprehension... Now I see it as a standard convention, like indexing should start with 0.
- deleted 12y ago[deleted]
- mannykannot 12y agoThis apparently wasn't a popular post, but it makes an important point: for abstractions to be useful, the user must know or be able to look up their precise semantics, and that requires more than a name, as illustrated by the range function here. This is generally well-understood by the people who write software for making software, but not so much in applications-level programming.
- cheatsheet 12y agoOr something like a public forum or google exists, and lots and lots of people continue to hack and talk about things in order to fuzzily define what they think the author meant by a certain term, in the case that it's meaning was lost, relabeled, mistranslated, or otherwise changed somewhere along the way of technological development. If you understand mostly what everything else is in a system, then people basically swarm around the behavior of something undefined in order to fuzzily define it. It sticks and it does not stick - it depends on whether programmers rewrite the system or the word is forgotten about on some super secure layer of abstraction, or whether it's the word that everyone is teaching and talking about and naming things after. I don't think words can really express this process, it's pervasive and paradoxical to it's description. You have to assume you understand everything else, before you can label something truly new. Otherwise it's just the same amorphous blob, and the labels keep swapping, and no concepts are really applicable, because they are only observable from a macroscopic perspective of abstraction, except when they are being used, right there in front of the code. It's always the same balance of 'i know some stuff' and 'i don't know some stuff'.
- lostcolony 12y ago-Is- it well demonstrated here? I think everyone would understand what 'range' means pretty quickly, just by executing it. I can see someone saying "I don't get 'foldl'" (or even worse, foldl vs foldr), and really -meaning- it, but regardless of whether range(1, 2) gives me [1] or [1, 2], I'm pretty sure people would 'get' it just by trying a few examples in the console/shell.
- mannykannot 12y ago
- lostcolony 12y agoNot Invented Here, amirite? Implement everything yourself, -it's the only way to be sure-.