Home grown or save your time?

Started by
53 comments, last by MaulingMonkey 18 years, 8 months ago
Hi, im wondering whether it will pay to write my own list template class, or to use the one in the stl? Im writing my EntityManager class, so need to keep a list of entities. Not sure whether to write my own list class or not....
Mark Ingramhttp://www.mark-ingram.com
Advertisement
Use the one in stl.
I don't use STL, but I don't write an entire class or template either. I just add simple linked-list capability to things that need it. It's about 5-10 lines each for add, remove and rearrange.
Quote:Original post by Kris_A
I don't use STL, but I don't write an entire class or template either. I just add simple linked-list capability to things that need it. It's about 5-10 lines each for add, remove and rearrange.


So, let's see... 5-10 lines each means 30 lines, add to this the traversal code (either 10 lines once and 10 lines for each traversal, or 60 lines once to implement an iterator and no additional work per traversal), the code to replace various standard library algorithms (copy, for_each) which represents quite a nice bit of code, this adds up to 500 lines. Now, writing, debugging, profiling and optimizing 500 lines of code to reach the same level as the STL ought to take you two days. If you want advanced functionality (and an optimized one at that) such as templated allocators, sorting, wrapping push_front and push_back iterators, add a couple of weeks.

If std::list provides what you need, use it. If you need something it doesn't provide, but can be built on top of it, then use it and build that thing on top of it. Only if you have needs so specific, precise and exotic that the standard implementation does not provide them at all, should you roll out your own implementation. Otherwise, you're just spending more time to get an often inferior result.
Quote:this adds up to 500 lines. Now, writing, debugging, profiling and optimizing 500 lines of code to reach the same level as the STL ought to take you two days

Well, it doesn't. If I wanted to use STL then I would. If I wanted someone to tell me what to do, I would be asking a question not answering one. If Skute doesn't want to do it the same way, that's fine by me...
Quote:Original post by Skute
Hi, im wondering whether it will pay to write my own list template class, or to use the one in the stl?

Im writing my EntityManager class, so need to keep a list of entities. Not sure whether to write my own list class or not....


Want to create a game? use the STL.
Have a deep craving to battle with pointers and do silly misstakes but learn how a linked list work? Write your own, then use the STL instead.

As for the intrusive approach mentioned earlier, simply don't to that you'll end up with a class that has severe personality problems and tries to make quite a lot more than one thing. (Oh, yeah there actually exist scenarios where it makes sense, bulk PC programming isn't one of them though.)
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Quote:Original post by Kris_A
Well, it doesn't. If I wanted to use STL then I would. If I wanted someone to tell me what to do, I would be asking a question not answering one. If Skute doesn't want to do it the same way, that's fine by me...
Don't get defensive. You're just a bad software developer is all. You really shouldn't be dispensing advice, given the apparently poor quality of the software development decisions you make (at least in this case).
Quote:Original post by Skute
Hi, im wondering whether it will pay to write my own list template class, or to use the one in the stl?


If your goal is to learn to write a linked list - write one.
If your code happens to need a linked list, use the one in the library - it works, is standard, feature-reach and integrates with the rest of the library.
If you can demonstrate std::list is unsuitable to your needs - consider writing a custom allocator: that's often the customization people really need.
If an allocator still doesn't cut it - go back to writing your own, but try to conform to the STL's container interfaces and concepts, for familiarity (which, at that level, you would have) and integration purposes.

Quote:Im writing my EntityManager class, so need to keep a list of entities. Not sure whether to write my own list class or not....


Then don't. You've got more important things to do, don't you?

Quote:Well, it doesn't. If I wanted to use STL then I would. If I wanted someone to tell me what to do, I would be asking a question not answering one. If Skute doesn't want to do it the same way, that's fine by me...


You hadn't justified the validity of your approach, making your personal opinion of little help to Skute and leaving you wide open to a refutation. There is no need to take personally the fact that ToohrVyk explained the downsides of your solution, for Skute's benefit if not for yours.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Writing your own linked list class is not very hard to do. If you feel like doing it, go ahead and do it for the learning. Don't be afraid of making "silly mistakes", and don't be afraid of pointers. If you do it right, it will work and will most likely be as fast as the STL linked list in practice.

Its just not as practical as having a standard templated linked list class that is compatible with all the other functionalities of the STL.

Looking for a serious game project?
www.xgameproject.com
Quote:Original post by Jolle
Use the one in stl.


x2

This topic is closed to new replies.

Advertisement