Reinventing the Wheel

This is a discussion on Reinventing the Wheel within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I noticed that a lot of people advocate using the STL vector, string, etc so as not to reinvent the ...

  1. #1
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Reinventing the Wheel

    I noticed that a lot of people advocate using the STL vector, string, etc so as not to reinvent the wheel which is great for production code. You want to have something that speeds up production of a software product and make it easily portable to several platforms. For learning purposes, isn't reinventing the wheel kind of necessary? I read some older articles about programmers creating their own toolkits for programming. Do any of you have your own tookit that you put together? I also see that people still seem to be making their own string classes, BigNum classes, vector and the like despite all the advice to the contrary to just STL.

    Do you think that saying well its easier to just use the vector class is better then advocating someone trying to implement their own version? We wouldn't have boost or linux or several other options if someone didn't think they could do it better?

    Do you think that creating a personal tookit of your own classes is still advisable? If so, what would you recomend to include in a personal toolkit?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    For learning purposes, reinventing the wheel is not necessary, although it can be quite beneficial. When learning the language, you should first learn how to roll the wheel. This is why we tell people to use vector, string etc. Those tools were created and tested and do what you want better than anything you will build yourself most of the time.

    Once you are comfortable and proficient with the use of the tools, then it is certainly a good idea to learn how they work. One way to do that is to create your versions. If you are creating your own versions, then using them in your own hobby code is also a good idea, as you can continue to debug them and learn from your mistakes and successes.

    In the case of C++ more than most languages, it seems to me that people reinvent the wheel more than usual. I believe this is because there are so many people who are used to C with classes or pre-standard C++ (because they used it or because they were taught it by someone that used it). I don't think that it happens so often because people are doing it to learn, they often really think it is the best way.

    Personally, I'd probably rather learn a different language or learn more tools in C++ than practice re-making existing ones, but I'm sure I'd enjoy that and get something out of it if I did it.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    For learning purposes, isn't reinventing the wheel kind of necessary?
    I agree, at least when one reaches that stage where he/she is able to use wheels effectively and now wants to deepen one's knowledge by learning how to design and make wheels. I just posted this in the C++ programming board, actually:
    Write your own vector (and list, set, map, etc) class template to push yourself to your limit, thereafter use standard library containers (and those of Boost) unless that option is somehow not available to you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> thereafter use standard library containers
    The only clarification I would make to that is that you should be comfortable using those containers well before you start trying to write your own versions.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Well a solid understaning of the STL is something I would agree on. I know the main reason for learning STL is to speed up the learning curve. A student doesn't have to spend weeks or months creating their own versions of the STL before they can start using them in a program. They can start off with them right away. They are just a black box and all you have to do is learn the interface. I was just toying with the idea of creating a series of tutorials where you build on the basics with the whole goal being to implement something useful. I was thinking that your own version of the STL would be something that you could use, but since it would be reinventing the wheel why bother. When I do a search for tutorials, i find several that deal with how a linked list works, how a stack works, how to parse strings, etc. I was trying to think of a project that would tie them all together, that would also be useful.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Its worth investigating, in many cases a custom implementation can often be more environmentally tailored than its library counterpart, just take a look at some memcpy implementations.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >For learning purposes, isn't reinventing the wheel kind of necessary?
    Absolutely. If you don't know how the standard library does things, you're not in a position to use it wisely. That's why you'll see lots of people use a vector when a set would be better suited, all because they simply don't see the difference.

    >Do any of you have your own tookit that you put together?
    Yes. The more experience you get, you'll find yourself writing the same thing over and over. Your toolkit is a collection of easily reusable code that handles the things you keep repeating.

    >I also see that people still seem to be making their own string classes, BigNum classes,
    >vector and the like despite all the advice to the contrary to just STL.
    Well, the standard library doesn't have a BigNum class. You'll see wannabes of the standard library when people are trying to learn of course, and a lot of people wrote their own classes when the STL wasn't standard. Another reason is that the standard library is very generalized and heavy weight. If you know what you're doing and don't need that kind of flexibility, you can trim the fat by writing your own specialized class.

    >Do you think that saying well its easier to just use the vector class is better then
    >advocating someone trying to implement their own version?
    As with most advice, there are exceptions. Yes, you should prefer to use the libraries that are already written, tested, and in widespread use. No, you shouldn't be adamant about it and keep an open mind when you do need to step off of the beaten path. I think you're trying to apply our advice as gospel, which it isn't. If you don't want to use the standard library, or have a reason not to, don't.

    >Do you think that creating a personal tookit of your own classes is still advisable?
    Absolutely! A personal toolkit doesn't necessarily duplicate the efforts of the standard library. Sure, it can in some places. But it's there to help you be more productive by, get this, not reinventing the wheel.

    >If so, what would you recomend to include in a personal toolkit?
    Mine includes binary search tree and linked list implementations that are comparable to the standard library. I even have personal implementations of the standard library that are noticeably better, which I'll use instead. But for the most part, my toolkit consists of reusable chunks of code that I found myself writing often over the years. You can't just say "I'm going to build a toolkit" and do it in a few days. It's tailored to what you do and how you write code, which all comes together over a span of years.
    My best code is written with the delete key.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I always wondered about that, Prelude. I can't find your own implementations on eternallyconfuzzled. Any specific reason for that? Also, better in interface or implementation? And if it's implementation, then better than which specific implementation?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I can't find your own implementations on eternallyconfuzzled. Any specific reason for that?
    Depending on how I plan to use the code or where I've already used it, I can't make it public domain. And I only put code that's PD on my website.

    >Also, better in interface or implementation?
    The interface is well defined by the standard. Trying to improve it usually results in something worse, like Microsoft's "safe" library.

    >And if it's implementation, then better than which specific implementation?
    The implementations I test against are Visual Studio, Borland, and GCC.
    My best code is written with the delete key.

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Way back in the days before the STL I had to write all of this stuff myself. Sad to say my implementations were so specific to my program that they would not serve well as a general purpose container.

    But Prelude is right in that if you know how to implement a certain type of container you can trim a significant amount of fat when you roll your own. However performance often comes at the cost of compatibility which usually means yes your container is fast but perhaps not fast or as fast as the STL is in all instances.

    I often write my own linked list classes to deal with render lists. A linked list is a very simple structure and I usually do not need all of the features that the STL offers nor do I need to use a library which accounts for all those features.

    But for the most part I do use the STL quite heavily and it performs very well across a wide range of applications. Plus it's also nice to be able to use code that while it may not be 100% suited to your app, at least you don't have to code the container. The main bottleneck in my field of choice for programming is getting data to the video card. The less data I have to send across the bus the faster the game will be. So graphics and game programming is not about what you do render and update, it's about what you don't render and update that makes the biggest difference. Container speed is normally a moot point.

    My previous version of my StarX engine, then called XSpace, used a vector for all of the lasers that were fired in any one sector. I used a simple brute force approach with no insert operations and never had a problem up to about 5000 lasers. Throw in some models, AI, scripting, etc, and you would have enough lasers and projectile tracking to have a very huge space battle at interactive framerates. The only container I've noticed some slow-downs in was std::map but that had more to do with how I was using it than the container code. You can misuse any type of container or apply it incorrectly or in the wrong situation. That does not mean the container code is bad.


    So before you go code your own, understand how the container works in the STL, find out the parts you simply do not need, and profile the STL version and yours before deciding to use your own.

  11. #11
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I tend to do it a lot, sometimes to brush up on my skills (I don't want to distance myself too much from the low-level aspects of the language) and sometimes because I'd rather create my own rather than learn how to use yet another API.

    In fact, the reason for writing my (still comparatively small) GUI widgets library (as seen in my synthesiser) was because I couldn't work out how to extract a numeric value from a scrollbar under the Win32 API. I simply got frustrated and wrote my own tools that work how I want them to.

    In retrospect, it was the best move because I later implemented some very specific controls (the shaping curves, etc) that probably are not available under the standard library of controls. I suppose I could have just used wxWidgets or something but, again, that means learning yet another API (which I probably should do at some point, though).

    The class hierarchy needs some tweaking and there are many more UI elements I want to implement, but I think it's a nice bit of code that I've enjoyed writing.
    Last edited by samGwilliam; 07-07-2007 at 11:55 PM.

  12. #12
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by samGwilliam View Post
    The class hierarchy needs some tweaking and there are many more UI elements I want to implement, but I think it's a nice bit of code that I've enjoyed writing.
    This line neatly illustrates the difference between coding for fun and coding for work. I wrote my own GUI library in a job once too. It was really interesting and great fun. But I wasn't allowed start it until the project team had investigated that we really needed our own GUI library.

    On your own time, re-invent all the wheels you want. Who knows, you may even come up with a better wheel? But while you're working an employer isn't paying you to make a better STL. He wants you to add saleable value to his software. Re-implementing the STL won't do that.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  13. #13
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    True, but I don't even know what STL is.

    I'm a C programmer who uses classes occasionally.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Then perhaps you should improve your knowledge of C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Should I?

    I'm doing alright so far. I just don't like relying too much on other people's code.

    And I don't generally consider myself a C++ programmer.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reiventing the wheel (again)
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2007, 02:26 AM
  2. my mouse wheel
    By DavidP in forum Tech Board
    Replies: 0
    Last Post: 11-06-2006, 08:58 AM
  3. Problems with mouse wheel...
    By xkrja in forum Windows Programming
    Replies: 1
    Last Post: 09-12-2006, 02:22 PM
  4. Logitech MOMO wheel broke, fixed manually
    By Xei in forum Tech Board
    Replies: 2
    Last Post: 07-25-2003, 02:29 AM
  5. Mouse Wheel In Console
    By GaPe in forum C Programming
    Replies: 0
    Last Post: 09-07-2002, 02:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21