• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
KaiserJohan

Overusing interfaces?

9 posts in this topic

Hello,

I have interfaces for virtually ( :D ) all classes I use; for example IThread.h, IMutex.h, IConditionVariable.h, IThreadPool.h, ISynchronizationFactory.h, etc.. and then .h/.cpp "-impl" versions of them. I use factory classes to create/destroy the implementations and then just pass around and work through the interfaces.

Questions:
[list]
[*]I do it because I find it clean/pleasant to work with, but quite some code I see do not use it. My question is why? Is it just a matter of taste? I know virtual methods adds an additional overhead, but besides that?
[*]In my factory classes I do not generally need dynamic class instantiation; for example I just have one "ThreadImpl" for win32/android, so I really could just have it return the ThreadImpl", but I prefer to work through an interface. Is it just a matter of preference or any practical reasons to chose one over the other?
[/list]
Thanks
1

Share this post


Link to post
Share on other sites
[quote name='KaiserJohan' timestamp='1347625645' post='4980028']
I have interfaces for virtually ( [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] ) all classes I use;
[/quote]

Nice! In my experience, interfaces are a way to avoid tight coupling of classes, so you can change the
implementation, add new implementations, and reuse classes without recompiling. However, it is a matter
of taste if you have a written an internal class that no one else will ever see or use, then it depends how you
use it.

I have always tried to follow Kent Beck's idea that only writing the code you need today makes
it easier to re-factor and change the code tomorrow. So I say, use all the interfaces you want to
keep your code clean and your interfaces loose, but don't add the interface until you actually need it. Edited by Glass_Knife
2

Share this post


Link to post
Share on other sites
There isn't such a thing as "overusing interfaces"!!! [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]
1

Share this post


Link to post
Share on other sites
[quote name='Telastyn' timestamp='1347638232' post='4980097']
[quote name='KaiserJohan' timestamp='1347625645' post='4980028']
My question is why?
[/quote]

Because having an interface with only one implementer means you're writing the code twice for no reason. No, "I might need a second implementation some day!" is not a sufficient answer. YAGNI.[/quote]

I agree with this. If you need a second implementation "some day," you can create the interface on that day.

[quote]Because if you allow people to make an implementation of something, they will. Some parts of the design are vital and/or fragile enough that you don't want people changing the behavior (perhaps incorrectly) and introducing widespread subtle bugs.[/quote]

This can happen even if you're [i]supposed[/i] to implement an interface: [url="http://blogs.msdn.com/b/oldnewthing/archive/2004/03/26/96777.aspx"]http://blogs.msdn.com/b/oldnewthing/archive/2004/03/26/96777.aspx[/url]
0

Share this post


Link to post
Share on other sites
You want an interface when you will have more than one implementation to choose between at[i] run-time[/i].
Your thread implementation changes at compile-time.

You just need a common header with multiple implementations:
inc/mutex.h
src/win32/mutex.c
src/linux/mutex.c
src/android/3.x/mutex.c
src/android/4.x/mutex.c
src/osx/mutex.c Edited by Shannon Barber
2

Share this post


Link to post
Share on other sites
Dynamic allocation of everything has performance consequences on top of that of virtual function overhead. First off, dynamic memory allocation itself is fairly expensive in the general case compared to allocating an object by value. Unless you override the behavior, this will generally involve synchronization so that memory can be safely allocated in multiple threads in the same time, and a search through a list of free memory blocks for available memory. This means that as you allocate and deallocate memory blocks this can get slower and slower. Even when you do override the behavior, it's almost never as cheap as allocating by value which generally just involves increasing the size of an existing allocation.

Secondly, it requires more memory than just allocating by value. First off, you have the additional memory required for the pointer to the object. Then you have book keeping overhead for dynamic allocations and potential internal fragmentation. Finally, if you have an interface with only a single implementation then you've got vptr overhead in each and every object. On top of that you've got potential external fragmentation. In addition to the raw extra memory you're using you also have worse cache performance due to decreased spatial locality. Given that memory is the single greatest performance bottleneck on modern machines, this can really hurt performance.

As an example, on MSVC 2010, 32-bit, Release without the debugger attached on Win 7, a CRITICAL_SECTION by itself is 24 bytes. Put a CRITICAL_SECTION in a class with virtual functions and it takes 28 bytes. Heap allocate that sucker and it takes up a minimum of 40 bytes between book keeping information and internal fragmentation. Then add the pointer you need to keep track of it and you've got 44 bytes, not quite double. Not that big of a deal for a CRITICAL_SECTION since you aren't likely to make many of them, but if you're doing it with everything, like you say you are, then it's going to add up.

Also, the use of interfaces rather than concrete types can inhibit inlining, not just in code using the interface, but in code implementing the interface if you aren't careful. If you have RTTI, exceptions or debugging symbols enabled, then you'll also be adding additional static information to the executable by using additional classes.

A particularly unfun part of this is that many of these performance effects are non-local. In other words, bad decisions in one section of code can affect the performance of unrelated parts of code. For example, if you swiss cheese your memory in one section of code, later code may have things like extra cache misses. You can reduce some of these effects by using pooled allocators, arenas, etc. but preventing problems in the first place is generally more effective than trying to fix side effects later.
2

Share this post


Link to post
Share on other sites
I don't know how else to put it, but this is serious code smell. It is over-engineering taken dangerously close to the extreme. In my experience, every time I've ignored YGANI, I've been sorry about it. Even if it weren't C++, but a managed environment where memory allocation is done properly (and therefore faster than in C++ unless you do it on your own), it would still be a very bad decision.

If an interface has one and only one implementation, and the reason for its existence is that it is prettier to have its name begin with "I", then you have made a bad design decision. If the code base is small and maintained only by you, it doesn't matter, but if you work on a bigger project with other people, you will be stuck with your decisions for quite a long time. Having to code more for no other reason that aesthetics is pointless waste of time.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0