by hbt · 10y ago · 0 comments
Sure, we have libraries, apis, package managers etc. but every time I read a code base, there is always a utility function reinventing the wheel.
Someone wrote it because it is still difficult to discover modular code and reuse it easily.
It's pretty nuts when you think about it. Imagine mechanical engineers having to recreate the same CAD file because they can't find a component with the same functionality (which does happen but for other reasons e.g intellectual property).
But we don't have the same intellectual property challenges and yet the best tools we have to discover and reuse code are 90s search engines that treat code as raw text with zero contextual understanding and often outdated code snippets.
PS: I love your project btw.
Let's say you want that `contains` function from the original post. You'd search for `(Eq a) => a -> [a] -> Bool`, which describes a function that takes two parameters and returns a boolean. The first result[2] is the `elem` function, which is exactly what we wanted!
This is a bit of a contrived example, but I have honestly been surprised by how effective searching by type signature is in Haskell. I wonder if it is possible for a language with a weaker type system, like JavaScript.
[1] https://www.haskell.org/hoogle/
[2] https://www.haskell.org/hoogle/?hoogle=%28Eq+a%29+%3D%3E+a+-...
map (elem x) [list1, list2, ..., list10]
or, for folds, the function is the one least likely to change, so you put it first, and the list is most likely to change, so you put it last: foldl (+) 0 [1,2,3]
map (foldl (-) 100) [[1,2],[2,3],[4,19]]
Of course, it's always debatable if you can find some situation where you wanted to curry in a different order, but generally you pick in order to reduce forced named lambda parameters. As far as I can tell, that's how it's done.Edit: if you saw the sneaky edit, you'll know that this particular convention isn't easy to follow!
If there are multiple close matches, ordering of arguments can change which is first, but the one you're looking for is almost always in the first few.
[1] https://www.haskell.org/hoogle/?hoogle=%28Eq+a%29+%3D%3E+%5B...
There probably isn't a nirvana just waiting for That One Great Tool. Or, alternatively, if there is, it probably involves having to switch to something like a dependently-typed system or the very bleeding edge of where the Haskell community is right now, which is probably still not really quite where it needs to be yet for this. And that would still involve a lot of cost for that switch.
Oh, and this is one of those places where I perhaps may be justified in pointing out again that for all the sound and fury in software development, in reality our field does not move that quickly. There's a ton of package managers out there, but broadly speaking, they're the same set of features shuffled into various combinations. Nothing wrong with that, per se. Just a lot less innovation than may initially meet the eye.
That's because those modules also have multiple (and competing) dependencies though.
If most modules in all languages depended on the same, lower level modules (with rigidly fixed APIs) that wouldn't the case.
Sometimes the wheel is invented because the existing wheel isn't built for the same purpose (ex: off-road).
ex:
I recently needed to check a buffer to see if it's contents were valid UTF-8 in nodejs. I wanted to do this without having to run it through something like StringDecoder, because it's an intermediate buffer, and I didn't want to have the overhead of actually converting it since it wasn't for my use. So I do 'npm install is-utf8'. Then:
let isUtf8 = require('is-utf8')
isUtf8(new Buffer('\u0000')) //false
isUtf8(new Buffer('\u000B')) //false
isUtf8(new Buffer('foobar')) //true
So the package is-utf8 is really more like "Is a printable UTF-8 string". Which is certainly a reasonable choice, and a possibly reasonable default, but was not what I needed.Don't get me wrong, libraries are wonderful and useful and you should be using them where appropriate, but like everything they have tradeoffs. I've seen way too many projects that try to just tie every single library they can find together without having to write any code. I know these projects because they're a nightmare to install and keep working and I end up spending way too much time trying to hack around all of the code rot to get them running again.
I'm tempted to say developers shouldn't have been able to target specific dll versions, but that would cause different problems.
In the grand scale of things that are wrong with programming that's somewhere between non-problem and minor irritation. Disk space is cheap.
I don't understand what part of this doesn't qualify as a "reusable module" to you.
- How do you search for a function other than by description or name. Assuming the developer classified properly and you even happen to use the same domain language as the writer.
- When multiple modules are returned, similar packages, how do you compare and select efficiently without wasting hours reading the code, evaluating it, checking if it is still maintained and hoping there are no hidden bugs etc.
It is easy to share code. Package manager even facilitate updating code. But it is still a nightmare to find/discover code and reuse it
It's like building a lego set. You know the building block you want but if you have to spend hours looking for it, you give up.
I believe competent developers write shitty code not because they are lazy or inexperienced but because the cost of writing great code is very high.
You were asking for modules and now you're down to the function level. That's a much easier problem to solve. Hoogle, Javadoc, documentation and good index engines solve this problem easily.
Note that type only is pretty ambiguous and most of the time useless without additional metadata. Types tell you what, they don't tell you why or how.
> - When multiple modules are returned, similar packages, how do you compare and select efficiently without wasting hours reading the code, evaluating it, checking if it is still maintained and hoping there are no hidden bugs etc.
At some point, the human brain has to step in and make a call. You can't expect an automated tool to dictate to you the code you need to write.
The problem is we don't have a safe, easy to build and package with everything else, and easy to consume (without performance penalty) language. The best we have is C, which is not enough.
It's not necessarily a problem. It actually cuts both ways.
The code is not going to benefit from updates, but the code is not also going to be harmed by updates. Think API or behavior changes. Additionally, if the use case for a dependency changes, sometimes the "new" solution doesn't match well.
Ex:
I work with jscript in ASP sometimes. A javascript library changed such that instead of doing iterative processing of nested items (pushing an item on to an array, then looping, popping off the item and processing it, then repeating), they changed to use node's nexttick with some logic like 'we want to move to nested function calls for processing, and doing that iteratively would blow the stack, so we'll just use nexttick so it won't have an increasing stack'. Well, jscript in ASP doesn't have nexttick or any equivalent timer. So while the original code itself worked flawlessly, the use case of the authors moved and we pulled that code in as an external dependency.
That's obviously an extreme case, but I can't really count the number of times that an API change in an NPM package for node has meant modifying code, without a change in functionality.
So yes, you get updates, and sometimes those are going to be security updates and real bug fixes. Other times though that update is going be adding new functionality (and possibly new attack vectors), or dropping support for your use case, etc.
Like many things in our field, it's wisest to look at the risks in all cases, evaluate them for the specific situation at hand, and then choose the appropriate one, rather than cargo-culting one 'best practice'.