Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

As I've always liked doing things myself, I chose to code in C++ rather than Visual Basic at university because I could see what was going on and how it worked.

However, sometimes I feel like I'm wasting my time though, as there are so many 3rd party libraries out there.

Here's an example of what I mean.

A while ago I created my own collision detection function based on the x,y coordinates of the mouse and the dimensions of the sprite looping through each line checking for visibility of pixels. About a fortnight later I found out that there is already an identical function built into DirectX.

Doing it your self helps you understand how it does it but you cant possibly understand everything about programming, there's to much and it keeps changing.

Being young and relatively new to the commercial world I was wondering what experienced professionals opinion was on when to code something yourself and when to just used what's out there?

share|improve this question
3  
Is this better suited for the Programmers stack exchange? –  Tim Apr 21 '11 at 13:55
add comment

migrated from stackoverflow.com Apr 21 '11 at 13:57

This question came from our site for professional and enthusiast programmers.

8 Answers

up vote 4 down vote accepted

Well, this is very subjective. I think every programmer have wondered this at some time.

Also, it depends a lot. There's a golden rule - the more people use one code, the better it gets - it's tested a lot, it's optimized a lot, the bugs are minimal, etc.

So, in my opinion, there're few criteria:

  • if you need something for a common use (such as ordinary containers - just like std::vector or std::list, or smart pointers (boost) ) and you don't need that much performance - use the existing one, which is "out there". This will save you much time coding, testing, finding bugs. And, as I said - the more developers use it, the more bugs are fast found and fixed, the better the lib becomes.
  • if you need something very specific:
    • if you're not looking for high performance (for example for real-time systems, which should process, lets say, 10 000 messages per second) and if there's something, that can help you - I'd suggest the same, use the existing one
    • the things are different if you really need high performance and THE MOST IMPORTANT THING - YOU'RE SURE, THAT YOU'LL ACHIEVE IT - then develop it by yourself. I can give you a real example from my experience - there are a lot of IPC libs, but most of them have a lot of features, that we (me and my co-workers from the company) don't need. So, this slows down the things, as we develop software for real-time systems, that process many messages per second and each operation, each millisecond is important to us. Then we developed our own IPC lib - it took a long time, a lot of tests, a lot of bugs, etc, but it was the best solution for us.
      Other example - some containers, such as std::list or std::vector have really great functionality and a lot of useful things. But if you're sure, that you'r not gonna need them and you really need HIGH performance, and, again, you're sure, that you can achieve this - you could implement your own container.

You know, it's really subjective. Programmer's time is much more expensive and valuable that CPU's time or something. So, if you can use something existing and it satisfies (as performance, interface ) you - use it.

About this: "Doing it your self helps you understand how it does it but you cant possibly understand everything about programming, there's to much and it keeps changing." - you're completely right. To learn something, you do need to practise it. When you write something by your own, you'll learn a lot - much more that reading about it (from book, internet, article, whatever). The best teacher is practice.

But practising could be done at home, this will make you better developer. Most companies will pay you to write quality code, and do it faster, with less bugs. Noone will let you implement map, for example - you know, it's rather difficult and hard to implement (you need to know some advanced data structures there - like AVL tree or something similar). (and I mean map for some casual use, not something very, very special, because if it's something specific - it's different then).

I'm not sure if I confused you more or helped, but there's no correct answer for this :)

share|improve this answer
add comment

I'll answer this as a build-vs-buy decision. Using third-party libraries can accelerate development but you are also bringing in a lot code might not apply to the job at hand. Extra code means more moving parts, which has performance and reliability implications.

You are also forced to use the developer's "pattern" of approaching the problem, which may or may not suit your style.

For example, when it comes to consuming API's these days, I shun libraries. Most web API's are very simple and don't need further abstraction. The library developers seem to add 3 layers of API on top of the API, just to suit their thinking.

share|improve this answer
add comment

At some shops, you may end up re-inventing the wheel due to licensing agreements and royalty fees. If the shop doesn't want to disclose any of its proprietary code then it may have to rewrite public GPL libraries.

The preference is to use any libraries before reinventing the software. However, there is definitely good knowledge and experience to be gained by re-inventing the wheel.

share|improve this answer
add comment

I spent many years developing in Cobol. There were almost no code libraries and we had to code everything ourselves. Most Cobol shops had their own subroutine library for the most common procedures for that shop.

I suppose that even now when using Java, I tend to code things that should be handled by library functions. You should have seen my Java array processing methods when I had no idea Collection classes existed. :-)

I guess the answer is, can I code the method or process quicker than I can look it up.

I'm glad you asked the question, even that you posted it on Stack Overflow. It's a nice change of pace from the "Which library should I use to add two numbers" questions.

share|improve this answer
add comment

As you are still learning and coding "for fun" there is nothing wrong with coding these yourself - in fact it's probably a good idea.

However, when it comes to writing commercial software you should look for 3rd party libraries that can help you concentrate on writing the code that is unique to your application. However, your time spent coding up some basic functions won't be wasted. You'll be able to use the knowledge gained to evaluate the library and decide whether it does what you need it to do.

Some libraries (not necessarily DirectX) may look good on the surface and perform well in demonstration systems, but when you implement them in a real system with real volumes of data they struggle. With the knowledge gained from implementing your own "library" code you'll be able to spot these problems much easier, work out whether you can mitigate them and perhaps even contribute a fix (if it's Open Source).

share|improve this answer
add comment

You can't write everything yourself. There are also things that really should be written by experts, including security software and complicated C++ templates. In any case, third-party libraries are likely to be better than what you'd write yourself, unless you spent a lot of time on it, or the libraries weren't particularly good.

If you want to learn how to write something, by all means do so. Just don't use it later if it's security software or something of the sort.

If the libraries don't quite work for your purpose, or are too general, you may have to write your own.

Write stuff in your core competencies. If you slap a game together out of third-party libraries hastily glued together, it will be bad. If you write what you're good at and use third-party libraries for the rest, it will be a lot better.

Also, this depends on the cost of your time. To a company, it's usually cheaper to buy something rather than to try to develop something corresponding in house, because programmers cost money and the library isn't directly profitable. Individuals generally don't value their time as highly, and can find re-inventing the wheel rewarding in itself.

share|improve this answer
add comment

If doing it yourself will be more enjoyable, a good learning experience you feel you would benefit from, or existing solutions aren't what you want, do it yourself.

If you have no idea what to do, it's complicated to make, or time is important, it would be better to use a 3rd party library.

share|improve this answer
add comment

I feel the same way sometimes. The way I see it is: If you need it done fast, look for a library; if you have the time, learning is preferable because if something breaks you know how to fix it and don't have to wait for a bugfix.

Another consideration is if there is a library that you should be learning to use, in your DX example if you are intending to become a DX wiz, you should probably do it the DX way. Paid DX work will probably prefer it too.

Hope this helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.