As with most things in programming, the answer is "it depends".
At one level, your C code absolutely depends on code in the standard library - you aren't going to re-implement printf
, for example.
But on a higher level, things get murkier. Using third-party solutions can make life a lot easier for some tasks, but adding a third-party dependency can cause problems as well.
For example, hand-hacking your own XML and JSON parsers is a pain in the ass1, and there are existing third-party tools that do that for you. Using those tools saves you time in development and testing and allows you to focus on the actual problem at hand.
At the same time, reliance on those third-party tools introduces some problems of its own. You may run into licensing issues, there may be security issues (such as the Heartbleed bug that was in OpenSSL), there may be versioning issues, incompatibilities with other tools, etc. You have to be careful how you design the code that uses the third party tool - it's often a good idea to create your own abstraction layer such that if you decide to use a different tool in the future, those changes are isolated from your application code.
Sometimes third-party tools are simply too "heavyweight" (use too many resources) and you only need a small slice of the functionality they offer - in that case, rolling your own solution may indeed be the better decision.
Summing up, leverage existing code where it makes sense to do so - re-implementing the same code over and over again is generally a waste of resources. Just be aware that there are times when you will want to roll your own solutions2.
- I speak from experience.
- Make absolutely sure that's the right answer, though - it's easy to fool yourself into believing something's easy (like a fully-featured XML/JSON parser) when it's really a lot of work. Crypto is one area where correctly implementing your own solution is hard and almost never a good idea.
main
function, and some otherdoSomething
function. Now I wantmain
to do the same something thatdoSomething
does. Shouldmain
calldoSomething
or should implement the same functionality again? If the answer always was to implement the same functionality again, we would never make functions. And thus functions have not stuck as a language feature. Since functions are an acceptable language feature, it is acceptable for a function to depend on another. QED. Addendum: the question should be when is it ok.