Thread: How to write a program as good as possible?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    127

    How to write a program as good as possible?

    How to write a program as good as possible?
    a good program must be easy to work with and useful. however, i think it there's more.
    do you care about design and coding? do you care about how to use programming language in right way.
    C++ supports OOP and GP, and many questions can be fixed with inheritance and template. what will you choose between inheritance and template if both they are can fix the problem perfectly?
    I use many templates in my program, and somebody told me that i misuse template because it can be fixed with inheritance and template will bring many classes here, but i responded him that is a style and some problems can be fixed more easiler
    what do you think about this? and your opinion?
    thanks

    Jinhao
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    73
    i think inheritance depends on what your inheriting...

    If yah got a bald old fat man as your parent... then by gad.. inheritance sucks... who said that parent wants annoying children anyways? They smell.. well most of them..

    Many Classes? nah just a waste of money.. self teach yourself is more effective..

    Template?
    Temp = temporary.
    Late = late

    Temp+Late = temporary late... doesn't sound so bad to me..

    I say you shoot the "somebody".
    if you shoot the "somebody" then you won't have anyone telling you that what you are doing in wrong... you will then conclude that what you are doing is right..

    problem solved.. murder your adversaries... worked for Bill Gates...


    boy I gotta lay off the booze...
    Last edited by Deo; 06-12-2005 at 03:58 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    How do you program as well as possible? You read, you learn, you study, you gain experience.

    Between inheritance and templates, both have their positives and negatives. Neither is better than the other. There are no examples where both are the perfect solution. If both are equally good, then it doesn't matter which you choose.

    Don't switch templates to inheritance because your friend says so. Do it if you think inheritance is more appropriate for the job.

    BTW, inheritance is misused far more often than templates. I think it is quite possible that your friend misuses inheritance and your technique is fine.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Well, stemming off the last two...*ahem* pseudo-constructive posts, this is what I believe about the two topics at hand:

    Basically, you should use each in the best case for the particular method.

    For example, inheritance would be used in the following case:

    We have an Enemy class:
    Code:
    class Enemy
    {
    // Implementation
    };
    Now, we have the basics of an enemy, what if we want an easy enemy?
    Code:
    class EasyEnemy : public Enemy
    {
    // Implementation
    };
    Etc for any more enemies. This is the best case to use inheritance, when you have one object that is more or less, the child of another object.

    Templating, however is completely different.

    You would use templating in the case that you would like to write a generic class useable for anything. The best example for this is sorting functions and other very nice algorithms.

    I can't even imagine why you would write a sorting algorithm based upon inheritance, that just seems like overkill and a waste of code.

    So when it gets down to it, just like choosing your programming language, use whichever method works best in a given situation. Templating and inheritance are very different subjects, so I can't really see how you would have trouble deciding which to use when.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    a good program must be easy to work with and useful. however, i think it there's more.
    do you care about design and coding? do you care about how to use programming language in right way.
    Right ! Design is very important. Without a good design, you won't meet your goals. By design, I mean the planning and structure that's done before you write the code.

    If you think in terms of goals, the first goal is always to “meet the specs”. The program should do what it’s supposed to do without runtime bugs or errors.

    Beyond that, you might want to write “elegant” code. (That usually means the least lines of source code or the least number of operations or commands). Or, you might want to write “efficient” code which might mean the smallest or fastest executable code.

    My personal “default” secondary goal is to write code that is easy to understand. This means that I try to keep it simple and include lots of comments. Keeping is simple usually goes hand-in-hand with minimizing the number of lines, but I won’t do anything “tricky” to save a couple of lines. I seem to average about one comment for every 3 lines of code.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Templating and inheritance are very different subjects, so I can't really see how you would have trouble deciding which to use when.

    Totally agree with you there. Jinhao, what was the specific situation you were arguing about with this "somebody"?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Right ! Design is very important. Without a good design, you won't meet your goals. By design, I mean the planning and structure that's done before you write the code.
    Right, but it is impossible to start with a good design, a good design is always created by many changes of codes during development.
    thank you for ur "goals"
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    While your comments weren't directed at me, I'm kind of sceptical of them.

    >>but it is impossible to start with a good design, a good design is always created by many changes of codes during development.

    Indeed, every design changes during development. However, it is important to plan before you begin, as DougDbug said. If you don't start out with an overall vision of how you're going to accomplish the goal, you'll end up confused about what you're doing and probably have to change your design drastically several times before you finish. On the other hand, if you think things out before beginning, and choose the route that suits your goals the best, chances are you'll only have to make minor revisions to your overall plan.

    >>thank you for ur "goals"
    Jinhao, I can't figure out why you're being sarcastic here. IMO, the goals Doug mentioned answer your first question perfectly: #1 is to meet the specs, #2 is to make it elegant/efficient, #3 is to make the code readable.

    In any case, I'm still curious as to what the particular situation was, in which there was controversy between whether to use templates or inheritance.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I don't feel like going into details right now (and you can certainly find them with a bit of looking), but here is a tradeoff that you'll find between using object oriented vs generic programming when the two are ~ equally suitable for the problem:
    OOP : Slower, but less code will be generated.
    GP : Faster, but more code will be generated.

    (Note, the magnitude of the difference between the two is impossible to guess at without looking at the specific application and techniques used.)
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    C++ supports OOP and GP, and many questions can be fixed with inheritance and template. what will you choose between inheritance and template if both they are can fix the problem perfectly?
    For classes that are going to be used as data structures, prefer templates because templates do not restrict you to deriving everything from a common base class. But you also have to take into consideration the actual effort and the greater compiler time. If you're writing a class with complicated member functions, templates are bad; given compilers don't support export, you'll have to inline every method of the class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Program to write to memory map addresses
    By modest in forum C Programming
    Replies: 15
    Last Post: 06-03-2008, 03:20 PM
  3. how could i write this program: cat < apa | wc | wc > bepa
    By strugglingman in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 04:40 PM
  4. Hints to write a program
    By Bnchs400 in forum C++ Programming
    Replies: 28
    Last Post: 04-05-2006, 05:35 AM
  5. Challenge to write a program
    By Twisted.alice in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-15-2003, 12:00 PM