117

I find myself constantly running into this expression "don't reinvent the wheel" or "never reinvent the wheel" when I ask some questions on SO. They tell you to use some frameworks or existing packages. I know where this attitude is coming from since it's unwise to waste time on something others have already solved. Or it that so?

As a student, I find by using some code others wrote to solve my problem I can't learn as much as I'd like to, and I gain less insight. And sometimes I think that phrase is mainly for working programmers facing deadlines and not for students like me.

Is it that bad to "reinvent the wheel"? Maybe I'm thinking it wrong? Maybe there is a way I can avoid reinventing the wheel and at the same time learn a lot?

Robert Harvey
  • 199,517
Gnijuohz
  • 2,055
  • 51
    You won't get much benefit from joining a gym if you get other folks to lift the weights for you (unless you are learning to be a manger). – Charles E. Grant Apr 11 '12 at 03:41
  • 83
    It's ok to reinvent the wheel when you're going to be building wheels. When you're building houses, it's a good idea to assume the wheel-builders know what they're doing better than you do. – zzzzBov Apr 11 '12 at 05:11
  • 22
    You at least need to TRY making a wheel! Otherwise you do not know why you should use a prefabricated one. –  Apr 11 '12 at 06:41
  • 59
    It is never appropriate to state an absolute. :) – user Apr 11 '12 at 07:30
  • 2
    It can be ok to reinvent wheels when you are creating extremely optimized code. Standard libraries are very useful, but it is often possible to create something that is less general but faster. – Leo Apr 11 '12 at 09:41
  • This is one of those phrases that I think depends a lot on context; it is a useful guideline. There are lots of different wheels for cars because the manufacturers wanted specific qualities for their wheel, so saying "never" can be a bit prescriptive. http://ipbiz.blogspot.co.uk/2005/11/uspto-allows-wheel-patents-at-about-73.html – Jaydee Apr 11 '12 at 14:18
  • I've posted a question on meta to see if there is a tag for these questions: http://meta.stackexchange.com/questions/128948/is-there-a-tag-for-re-inventing-the-wheel-for-educational-purposes – Stuart Blackler Apr 11 '12 at 14:35
  • 1
    There's several sizes of wheels, if you don't find your size, why not? But at least be sure that the wheels are not suitable for you before start :) –  Apr 11 '12 at 15:06
  • @KonradRudolph Luke built a lightsaber, he didn't re-invent it. Though we have no hard evidence to either case, I suspect he did nothing ground-breaking or innovative, but instead followed prescribed instructions to create a personalized tool, rather than revolutionize it. In other words, I differentiate assembly from innovating. – hexparrot Apr 11 '12 at 15:16
  • @hexparrot “reinvent the wheel” is exactly that, nothing more. – Konrad Rudolph Apr 11 '12 at 15:49
  • @MichaelKjörling only a Sith deals in absolutes – Woot4Moo Apr 13 '12 at 13:51
  • Gnijuohz, the future is about integration. Regardless of what you learn in school, one of the best skills you can have as a programmer is learning how to learn to use different "wheels". – Marco Apr 18 '12 at 19:44
  • If all of the wheels come in sets of 4, or don't fit your axels, or are of bad quality, etc then inventing a new wheel to suit your needs is perfectly fine. – Ryan Mann Feb 20 '16 at 21:37

20 Answers20

126

I think you make a good point. Most of the programmers on this site are likely working professionals whose goal is pretty much to create quality software as quickly as possible. Reinventing the wheel fails this goal on two counts.

  1. Re-writing code that exists is wasted effort that could be used on the unique parts of your system and makes the project take longer than is necessary.
  2. The first version of any code is more likely to have bugs/unforseen issues. Most libraries and re-usable components have been battle tested and patched multiple times. If you re-invent a hashing algorithm or try to create your own RDBMS (unless that is what the project is) more often than not you are going to end up with inferior results.

That said, in an academic environment the goal is to learn, not deliver software on a budget. Re-inventing a wheel to understand how the spokes or axle work is a great way to accomplish that goal. That's why many programming curricula include a class on building compilers when very few working programmers ever have cause to need to do that.

JohnFx
  • 19,070
  • 37
    I agree with the idea to understand how things work, though I also do wish programming schools took some time in their curriculum to learn how to use third party libraries and why you should. I constantly find programmers and developers of professional capacity who cannot do this and end up reinventing the wheel. – Spoike Apr 11 '12 at 06:11
  • 3
    great point about compilers – Chani Apr 11 '12 at 09:17
  • 1
    I think learning not to reinvent the wheel is more important. There are many things you can do that don't involve creating something that was already created. – Andreas Bonini Apr 11 '12 at 15:50
  • @Krelp While one is a student, the concepts are the most important part. Many professors use code samples to allow a bridge theory and application. Those are the types of programs that are going to reinvent the library but it is to make sure that the students understand the material. – Jetti Apr 11 '12 at 16:09
  • 2
    As a student you need to reinvent the wheel to see why it goes as fast as it does, but as a professional you need to use commercial wheels so you can go as fast as possible. Unfortunately, as it turns out, SO is primarily a site for professionals, not for students (didn't they ban [homework] for a while?) – Tacroy Apr 11 '12 at 17:40
  • Perfect explanation. I often find myself reinventing many 'wheels' when I jump into a new language (hence a student of that new language). I'd rather KNOW it than just be able to copy-and-paste from some tutorial. Doing so on one problem can also lead to innovative solutions for another. – Gaffi Apr 12 '12 at 12:27
  • @Spoike the trouble with teaching is that there is a long lag between what you learn and what you use when you leave college. So the 'state of the art Java framework' you learnt in college is obsolete and replaced with a new "state of the art C# framework" when you get a job. (and that C# one... its just becoming obsolete today). So never bother teaching them specifics, teach the concepts. – gbjbaanb Apr 13 '12 at 13:39
  • There will always be specifics taught, as long as understanding of the broader concepts goes with it that is ok. Lets face it the kid who is in Freshman CS Now may still be coding in 30 or 40 years using languages that have not yet been invented or lisp – Zachary K Apr 17 '12 at 07:47
  • I remember at school a professor said: "In this chapter, we're going to learn and implement the quick sort algorithm in C++, but in the future in your programmer's life don't do this, there are library that are already highly optimised in C++ to do such things and you will never reach the same performance"; After more than 20 years I tried to apply this principle as much as possible. That said, it is very recommended to re-invent the wheel as a student if you're going to learn algorithms or what a framework does in the backend. – рüффп Sep 01 '20 at 20:06
20

The answer depends a lot on the context. If you would like to gain more in-depth understanding of data structures by trying your hand at implementing a hash table, "reinventing the wheel" is the best thing you can do. If you are learning how to write compilers and need a symbol table, implementing your own hash map instead of reusing one from the standard library is a complete waste of your time.

Sergey Kalinichenko
  • 17,471
  • 4
  • 58
  • 73
17

As a student, I would expect you to begin your programming education by first copying a wheel or two to start with, then learning to modify wheels to see how they work, and to understand any limitations. Later you might even create a brand new wheel of your own to see if you can improve on the design, or to show your course supervisor your understanding of the concepts involved.

As a working professional however, I would expect you to have learned which wheel to use to solve any given problem, and when it might be appropriate to modify an existing wheel if it only partially solves your problem. If you can't find a wheel anywhere, then perhaps you have identified market niche, or you haven't explored far enough, and you would need to be experienced enough to know when it would be appropriate to create a brand new wheel of your own.

The issue of when it is appropriate to re-invent a solution is complex, and it requires time and experience to learn when it might be better to create a brand new version of something that has already been done before. When you have only been developing for a short time, it is better to simply use an existing solution, and to ask your mentors to suggest options. When you have tight deadlines and a lot of uncertainty in a project, using something existing can be a huge time saver, and is always your first choice. You can always refactor to use other solutions later if it is appropriate to do so, even if this means eventually returning to reinvent your wheel.

S.Robins
  • 11,485
  • 3
  • 37
  • 52
12

Being a teacher or programming, I constantly battle with the opposite side of the problem: when do I ask students to reinvent the wheel?

Take these simple situations: we're studying sorting algorithm, and I set tasks to write a program that sorts some data; or working on date functionality, and I ask for a calendar.

For both of these, there are countless ready-made libraries and functionality available; but I want the students to avoid them, and develop their own version of a sorting algorithm or of a calendar.

Now take this other one: I set a task to write a simple application for, say, scheduling appointments. This is likely to need sorting, and a calendar, and more. This time "don't reinvent the wheel" applies: I don't want students to struggle with solved problems, but instead assemble existing functionality to obtain a result.

My difficulty is, how much to ask you to recreate the existing, which does get you to learn, and has the advantage of well-trodden problems with know difficulties that I can use to make you practice the craft, and how much I should place you in a real-world setting, where wheels are not reinvented?

To answer your question more directly, two suggestions:

  • if a teacher tells you to "not reinvent the wheel", they probably say so because they have designed their problem that way. Maybe they want you to try a library, struggle with someone else's implementation of an algorithm, rather than write your own - there's learning in that too, and recreating is interesting but misses the point.
  • if you have too many exercises assembling library calls, rather than developing algorithms, you could raise with your teachers the question of balance between these activities - highlight you've never been asked to write your own algorithms for, e.g. sort and merge. Communication will never go amiss.
boisvert
  • 201
  • 2
  • 7
6

Practice I doubt the first 1000 lines of code anyone writes are very unique.

Expand your toolset Using a framework has more benefit when you understand what it is doing (Alost to the point you could do it yourself.) so you know how to apply it.

Understand the "Wheels" Using a poorly constructed and worn-out wheel or one that doesn't fit, is no excuse for blindly sticking to this rule-of-thumb. You may be short on time, funding, expertise, so you just patch it up and finish the trip.

There are few absolutes.

JeffO
  • 36,816
  • The first 1000 lines of code anyone writes tend to be very unique in terms of creative mistakes. – leftaroundabout Apr 11 '12 at 20:32
  • @leftaroundabout - I guess I should limit that statement to lines of code that can compile. – JeffO Apr 12 '12 at 01:45