Handmade Hero » Forums » Code » When to use libraries
occamin
Mike
4 posts
#6219 When to use libraries
1 year, 6 months ago

This is a question mainly posed to Casey, but anyone with significant programming experience is welcome to chip in.

What is your thought process when you have to make a decision to write something yourself from scratch or to use an existing library (e.g. SDL)?

Given the recent embarrassing npm left-pad situation, we can kind of see what happens when reuse is taken to the extreme. The other side of the coin, is if everyone rewrote everything from scratch, there would also be a tremendous amount of wasted effort and time cost.

Obviously, writing things from scratch can serve as an excellent way to learn (which are why most of us are here), but I assume that in practice people have to weigh the pros and cons of each dependency somehow.
ratchetfreak
303 posts
#6229 When to use libraries
1 year, 6 months ago

The problem with leftpad is that people are using libraries for single functions that really belong in a larger string processing library
cmuratori
Casey Muratori
803 posts
1 project

Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.

#6231 When to use libraries
1 year, 6 months ago

Generally speaking, I like programming so I tend not to use libraries. I'd rather write the code myself. I rarely find any libraries that couldn't be dramatically improved in terms of their design, so most of the time I don't find it to be "wasted work" to write a better one.

In terms of the "what's the most efficient trade-off here in terms of total time taken vs. flexibility vs. bugs vs. long-term stability vs. etc., etc." I really couldn't say. Certainly it seems like currently there is too much library use, but exactly how much is too much is tough to say. Probably the current amount of library use itself wouldn't "too much" if languages and architectural understanding were more advanced and allowed more straightforward authoring of reusable code.

- Casey
Flyingsand
69 posts
#6233 When to use libraries
1 year, 6 months ago

occamin
The other side of the coin, is if everyone rewrote everything from scratch, there would also be a tremendous amount of wasted effort and time cost.


What about all the wasted time and effort that goes into dealing with bad libraries? I think far too much faith is put in the amount of time & effort libraries "save" us.

e.g. I once worked on a synth plug-in where I was asked to write some DSP filters. After pulling it from the repo, it took me 3 days to be able to just build the thing because there were so many libraries (most of which used several CMake scripts). Needless to say, I refuse to work with this person ever again. :)

Then there are bad APIs, compatibility issues, bugs you have no control over, etc. etc. that all contribute to additional wasted time and frustration in dealing with libraries. Granted, there are some good ones like SDL and the STB libraries, but I've been burned far more than the alternative.

But also like you said, in practice when budgets and scope come into the picture, it's unavoidable at times. Fortunately where I work, I have a high degree of autonomy so I can often make decisions against using libraries whenever possible, but on one project in particular it was impossible not to use a library (with an especially bad API at that!). I find it demoralizing, and it saps my productivity because it's so frustrating to work with.

All my other projects at work are more interesting and rewarding though, but it's telling that even just one project with a horrible library can drag down my day! :(