Choice of code design

Discussion in 'Game Development (Technical)' started by lordmetroid, Aug 25, 2007.

  1. lordmetroid

    lordmetroid New Member

    Joined:
    Aug 19, 2007
    Messages:
    20
    Likes Received:
    0
    So I decided to realize what I should have done a long time ago and actually start professionally developing games.

    I always coded in C for my serious projects and made use of module-type code design. However with the upcomming game I may be coding with a partner and so I am thinking of choosing C++ and have an independent-object-type code structure so code can easier be upgraded, replaced and worked on in parallel by many people. I don't know any C++ but the very basics of how to create classes. I am also uncertain if it ravioli code is preferable to lasagne code.

    What you all think?
     
    #1 lordmetroid, Aug 25, 2007
    Last edited: Aug 26, 2007
  2. Musenik

    Original Member

    Joined:
    Dec 5, 2004
    Messages:
    796
    Likes Received:
    0
    I have always preferred tightly wrapped ravioli objects embedded in a succinct stratification of lasagna modules.

    You think I'm kidding?
     
  3. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    I was a late comer to C++ and still tend to write projects in what could be described as C with classes.

    Short example, I will use a class to manage moving and firing different projectiles, but inside that will be a struct array and an ID enum. Pure C++ heads would use inheritance and have a class for each projectile type. I find that a bit silly tbh, as you never know what you're looking at, but I know that's just me, no flames plz.

    To actually answer your question, I'd suggest learning enough C++ to code like that above, as other "purer" c++ coders can still deal with that whilst inheriting their own diamonds of death to their hearts content. You can decide later if you're happy staying like that or want to adopt more of the oop mentality.
     
  4. bulbmonkey

    bulbmonkey New Member

    Joined:
    Aug 17, 2006
    Messages:
    7
    Likes Received:
    0
    Well, not needing to know the actual type is kind of the whole point.
     
  5. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    Here we go. You musta missed the bit that said this was just me and no flames plz. I know yours wasn't a flame, but this is how they start.

    FYI, I'm happy to have data-hiding help me avoid globals-related bugs and stuff, but the way I program I like to see where everything is. When it's 8 levels deep in inheritance it garners no faith from me at all. Whenever I program in an SDK that's written this way I'm always waiting for my bit to explode later. Max SDK is a classic example - If I could see the data I want to export, maybe I could bloody well export it without needing to learn the entire system.

    Functional C programmers coming to C++ need to see the code they're working with whilst they learn the subleties. (Or in my case don't learn them because they don't actually help you get games finished).

    Cue: Inevitable and boring holy war about how C++ solves world poverty... :)
     
    #5 Applewood, Aug 25, 2007
    Last edited: Aug 26, 2007
  6. Bad Sector

    Original Member

    Joined:
    May 28, 2005
    Messages:
    2,742
    Likes Received:
    5
    C++ is a horrible monster that needs to die a painful death and forgotten by any miserable coder that had the bad luck to cross with it, but it won't happen any time soon because there isn't any other C-with-classes-like language since most people prefer to go for a managed approach for new languages, like if all problems in the non-managed world are solved...

    If i didn't had to use 3rd part libraries (yes, i use a few), if i didn't had to work with others at some point, if it was officially supported in gaming platforms (xbox, ps, etc) and if the development environments weren't so screwed up (official IDE) or very narrowed to some kind of applications (Lazarus), i would write all my code in FreePascal. It has all the niceties of C and Pascal combined, without being the horrible beast that C++ came to be.

    Another option would be Objective-C, if it's object oriented part didn't used an alien syntax...
     
  7. datxcod

    Original Member

    Joined:
    Dec 16, 2005
    Messages:
    98
    Likes Received:
    0
    I don't know what's up with all those people hating C++. They are usually used to another language and didn't learn how to code proper C++ apps. Just a grab a good book about C++ and another about Design Patterns and dedicate two or three months to learn everything you can about the language, making all sorts of apps along the way. The syntax and rules of a programming language can be learned quickly but knowing where to apply design patterns and learning how to design good classes takes some time. Learning UML is also useful when you are working on a big project because it allows you to see how everything is going to work. If you are not even comfortable with the way C++ works (classes, objects, inheritance, templates, polymorphism), I really doubt you'll be able to code proper Java programs, or even complete flash apps.
     
  8. Bad Sector

    Original Member

    Joined:
    May 28, 2005
    Messages:
    2,742
    Likes Received:
    5
    Nothing wrong, it's just that the language sucks :). While it began with good intentions, in the way it became an overbloated monster with all sorts of little problems and special cases (you know, like the fact that 'new' in older compilers -before 1998- used to return zero when there was no enough memory and newer compilers raise exceptions - of which the behavior is unexpected if you actually disable exceptions from your compiler - so using proper C++ code written for earlier compilers isn't going to work with the newest breed, not to mention that such behavior doesn't really promise much for the language's compatibility with the future).

    According to Wikipedia, C++ "It is a statically typed, free-form, multi-paradigm, usually compiled language supporting procedural programming, data abstraction, object-oriented programming, and generic programming.". So what's the proper way for writing programs in a language that is made to be programmed in many different ways?

    There are two issues with saying something like this. One is that you assume that whoever dislikes C++ doesn't actually know about the language and how to design a program. This is wrong, because in order to have an opinion or something (whether this is good or bad) you first must understand and learn that "something". So while there indeed are people who dislike C++ while knowing nothing about the language, there are other people who have a deep understanding of it but still dislike it because, well, it's a mess :).

    The other issue is that you assume that people have to use C++ and they have to learn some "proper" (which due to the language's nature isn't really possible to define) way to program it. So why someone who programs C++ like if he was programming BASIC (i know such a person), Pascal, Java or some other "non-C++ language" is able to deliver the requested software with the way (s)he programs, has to spend three months (at minimum) to learn a mess of a language only because it happens to be the fourth most popular one? (java is the 1st)

    Of course you probably think that i'm contradicting myself for saying that last thing, but really i'm stuck with C++ only because my teammates use it. In fact, where i work i try to NOT use C++ wherever possible (for example since i'm the only guy working in the tools, i use Lazarus for developing them... i feel more comfortable there and i'm more productive, so why not?).

    That applies to most languages, not only C++. Even in Java where the language (and the API) somewhat forces you to use classes, in many cases you can just dump a shitload of static functions in a static class and call it a design.

    I can't comment on UML, i might check it out at some point when i have a lot of free time and comparable will, but from people who know UML and i respect their programming abilities, i've heard that it's a total waste of time.

    Of course the same people dislike Java, while i like it, so you never know.

    Sorry Applewood, what you did is like going to the center of an overcrowded beach with a huge speaker, yell "you are all morons, but i let's don't give much care about it, ok?" and expecting that noone will react :p. Such topics are not flameproof by nature :).

    Anyway, just to offer a bit on the discussion here. I would recommend to stick with whatever you are most comfortable with. If that's pure and plain C, then stick with it. C is a powerful and simple language so you won't have problems working with it. Modular programming can applied to C as well, you don't have to use C++ for that (you don't even have to do OOP, just break your modules in self-contained libraries).

    Now if you want to learn C++, do one step a time and just learn the traditional "C with classes" method. Don't overdo it with C++ities, because you'll lose the ball and instead of fighting with that damn algorithm that doesn't want to work, you'll fight with the language. For example, keep templates, rtti, operator overloading and complex, multiple constructors out in the beginning.
     
  9. brianhay

    Original Member

    Joined:
    Dec 27, 2004
    Messages:
    43
    Likes Received:
    0
    Yes there is - the D Programming Language - I like it! :cool:
     
  10. voxel

    voxel New Member

    Joined:
    Oct 29, 2006
    Messages:
    176
    Likes Received:
    0
    That's just your pro C++ bias coming out - seriously, you love it and you know it ;-)

    I'm sticking to the non-managed languages for core development because of the mess managed code can cause (different headache and one that I can't control as well). I migrate everything to scripting languages or data when possible. i.e Data-driven design.

    For the record, I think OO is bunk. I much prefer design patterns rather than force a pure data-hiding / object-knows-how-to-do-everything-on-itself paradigm. When I see a character class drawing itself(with DirectX) or performing physics on itself I kinda pull my hair out...
     
  11. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    This is why I risked my opening statement even though I knew it wouldn't stand alone.

    We had a grad start a while back and he was basically pretty decent at a fair few things, but he did all his projectiles and stuff like you say above - they all have a .move, a .draw etc. When I first saw he coded this way, I showed him how to unlearn that shit and do it 'my' way and our games frame rate stopped grinding when you let rip on the guns.

    I can cope with anyone using any paradigm they're happy with, as long as it actually works properly!
     
  12. lordmetroid

    lordmetroid New Member

    Joined:
    Aug 19, 2007
    Messages:
    20
    Likes Received:
    0
    Are you saying one shouldn't use the inheritance feature of classes at all? Or, do you mean that it is good to code in such fashion that one restricts inheritance to such an extent that one can easily keep track of what inherit what, right?
     
  13. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    Yes, restrict to where it only absolutely makes sense to would be my advice, at least to get started. When you're more comfortable with it, you can decide then whether you want to use more of this feature.

    It's not like it's a requirement though - I almost never use inheritance and my apps run just fine and maintain well - it's a personal preference as there is both an up and a downside ime. Given it's use is optional, I'd suggest putting it in the "learn later" pile and start getting on with the basics.

    There are lots and lots of yummy things in C++ that could've been in C if they thought of them at the time - it's not *all* about OOP. C++ is a good environment to work in imo, with stuff like default arguments, constructors and my personal favourite; operator and function overloading. This one sounds scary but it'll change your life for the better! Every language should have this. You'll see why when you compare a C matrix/vector class with a good C++ one.
     
  14. voxel

    voxel New Member

    Joined:
    Oct 29, 2006
    Messages:
    176
    Likes Received:
    0
    This is funny because a fewer of the things you've mentioned I don't particularly like in C++ - way too many gotchas in assignment operators and copy constructors and sometimes in overloading.

    I love the STL - but that beast needs to be tamed on the console side. I love pure virtuals for creating "interface" classes (ala Java - which is a cleaner C++). I love templates even with the code bloat. I despise RTTI and C++ exceptions - they work, but add too much bloat in a high-performance game.
     
  15. lordmetroid

    lordmetroid New Member

    Joined:
    Aug 19, 2007
    Messages:
    20
    Likes Received:
    0
    So how shall I structure my code? Is there any need for header files like in C and if so what shall be put into those files?

    In the past I coded like my projects with multiple layers of header files like so, http://www.nopaste.com/p/aB1j88xdN Shall one do the same think in C++?
     
  16. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    Oh, I quite agree. Whilst the features are nice, the syntax for them is an abortion and, I suspect, one of the reasons why people shy away from the language. I think they're tried too hard to make it look like vanilla c. Probably because in the early days it was indeed a c preprocess. But it isn't anymore!
     
  17. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    A "pure" cpp programmer won't have header files. You just have source and they're included lke headers in other dependencies. Basically, the source goes in the class definition itself (which to you would mean the header).

    I can't abide that though. I still use header files in the c style way, putting class prototpyes in the header and filling out all the functions using :: all over in a matching .cpp file. Because then you can still look in the headers for info without getting swamped with code. It's a pure productivity decision that goes against the grain of accepted wisdom as header includes are admittedly a pita. Forward class refs are nicer in cpp though so it's not as bad as c and I know I'm not the only one to work this way. In fact all our new guys have said they prefer this style.

    Again though, pure horses for courses - your mileage will vary greatly from mine or anyone elses.
     
  18. zoombapup

    Moderator Original Member

    Joined:
    Nov 25, 2004
    Messages:
    2,890
    Likes Received:
    0
    Speaking as someone who's kind of morphed from C to C++ and now to C++ but with design patterns, I can honestly say that from personal experience, the way to go is to where possible, use the patterns and forget being a slave to the language.

    Design patterns kind of work in any language, but in particular I've recognised a bunch of cases where I've seen inheritance used when a totally different setup should have been used (and a certain design pattern would have explained WHY).

    The projectile one is the classic. Given the number of weapons and projectile types we had in worms, the inheritance model we used for it got REALLY nasty. Something like 120+ weapons etc.

    Now I've seen the old school C + assembler version of the same code and that was function pointer table crazyness to look at, but simpler to comprehend than the inheritance version (because as Paul mentions, you dont have tons of hidden code).

    What do you know, with the design pattern version of that same problem, you dont have the horrible looking code of the old school version OR the inheritance hiding problems of the second method. WIN!

    So, learn C++ from a C coders perspective, but absolutely make sure you learn design patterns and use them. That way the C++ you learn actually works to your advantage instead of just making your code harder to follow.
     
  19. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    First I've heard of this, sounds like something I should read up on.

    Got a good resource without flashing adverts ? :)
     
  20. HairyTroll

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    582
    Likes Received:
    0
    Multimethods tend to help in these situations. Not possible to do in C++ though.
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer