Archived

This topic is now archived and is closed to further replies.

kuphryn

Beyond Fundamental C++ - Experience

Recommended Posts

kuphryn    210
Hi. I often post questions about C++ status relative to other programming languages as well as how to get specific programming working under C++. I want to switch direction for once and ask about good programming technique. I am at a point where programming with C++ is no longer intimidating unless it involves something that requires creating an entirely new data structure. How do you define good programming technique? Ultimate, the better the programming technique including program design, the more efficient the final program will operate. Furthermore, the program file should be smaller while undoubtedly producing the same result. I often find myself implementing if/else, while/do while and using bool as a "count" to manage the if/else and loops. In terms of effective programming, is that good practice? Do not get me wrong. I use only what is required and nothing more. It is just that sometimes the section of the program I work on need that kind of technique. I would like inputs from experience programmer about implementations that increase performance. Last, I read Deitel & Deitel C++ How to Program and Accelerat C++ by Andrew Koenig and Barbara E. Moo quick because it is such a short book. I prefer Deitel & Deitel C++ How to Program because it is a complete book. I have never come across a more complete and effective C++ book such as C++ How to Program. Nonetheless, I find Koenig and Moo implementing more involved programming techniques such as using more STL in function parameters, returning STL and lastly calling functions within functions. Maybe these technique seems different because of the way Koenig and Moo wrote their book when. They seems to like everything compact. Kuphryn

Share this post


Link to post
Share on other sites
coderx75    435
To be honest with ya, most programmers find their own styles and ways of doing things and "the best way to code" can''t really be transmitted in advance. It''s like asking "What''s the best way to play Chess?" Practice and as time goes on you''ll begin to find better ways to implement.

But a bit of advice that I can give is to be open to new things. If you code one way, but every book and document shows a different way to code, try to adapt it into your style and see if you like the result. Otherwise, just do what you need to do. Look back at what you did 6 months later and I garauntee you that you will see a better way to do the same thing.

Oh yeah... and stay away from them goto statements! =)


Many of the truths we cling to depend greatly on our own point of view

Get Tranced!

Share this post


Link to post
Share on other sites
kuphryn    210
Advice noted.

Most notable C++ books like Deitel and Deitel C++ How to Program and Accelerated C++ by Koenig and Moo present class declaration in the order of public, protected and then private. I do it in reserse order. Maybe I should try it the other way.

Thanks,
Kuphryn

Share this post


Link to post
Share on other sites
coderx75    435
Hehehe, that''s a matter of style. It makes sense tho, you would want public to be first since its what the world outside of your object must use... but, I''ve been programming C++ for 10 years... in my personal code, private comes first =)


Many of the truths we cling to depend greatly on our own point of view

Get Tranced!

Share this post


Link to post
Share on other sites
Shannon Barber    1681
"Design Patterns" Erich Gamma & etc. al.
"Effective C++" Scott Meyers
"Effective STL" Scott Meyers

Did me _wonders of good for me, though I've not read the ones you mentioned above, so I don't know how much material is redundant.

Next is the topic of refactoring (aka/related to "eXtreme Programming" - god I hate that name, it's almost as bad as lossy). Not everyone seems to like XP, but refactoring is all about improving the code.

Magmai Kai Holmlor

"Oh, like you've never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Edited by - Magmai Kai Holmlor on December 15, 2001 8:14:53 PM

Share this post


Link to post
Share on other sites
null_pointer    289
quote:
Original post by kuphryn

How do you define good programming technique?


I wish I knew.

quote:
Original post by kuphryn

Ultimate, the better the programming technique including program design, the more efficient the final program will operate. Furthermore, the program file should be smaller while undoubtedly producing the same result.


Ideally, yes; practically, no. There are two styles of programming: ideal and practical. Ideal assumes an excellent compiler built to handle all C++ statements with equal ease, producing fast, efficient machine code. Practical assumes something like Visual C++. Start ideal and work towards practical; eventually this conversion will become automatic.

Here is my advice:


1. To build a program, you must communicate to the compiler what it must do when given a set of input to produce the desired set outputs, and a language is merely a tool that facilitates this communication.

2. From the compiler''s perspective, there is no necessary relation between C++ code that looks nice and code that is fast and efficient.

3. Styles and conventions are, more often than not, workarounds for one''s own ignorance of the language. With C++''s typing and scoping rules, there is little use for notation. Learn to use the language to express yourself; that is why it exists.

4. If efficiency is your utmost concern, structure your data based on how your task is to be done.

5. The best approach is the minimalist approach with a good design document. Do not write code unless you must.

6. Do not idolize language features. Many people form emotional attachments to words like class and virtual. Try to pick the language features based on what you need to get the job done, rather than on what the latest fad dictates. If you are not sure, experiment and ask questions about how things work.

7. Do not accept advice simply because of the person who gives it. To accept advice, you must have the knowledge and experience to evaluate whether or not the advice is good. So get people to explain why they are giving you that particular piece of advice; ask for examples and evaluate their claims and assumptions to see if they are correct. Be open-minded, but demand facts.

8. Do not accept advice from someone who claims that x language feature is completely useless. Some keywords in certain contexts are deprecated, because they have been superceded by other keywords that make more sense; however, no language feature is useless.

9. Learn to use exceptions instead of returning error codes. With proper use of exceptions, you will net a small speed boost while improving the design of your code. Do not use exceptions for things that are not exceptional; i.e., do not use them as merely a way to redirect flow control, since they should indicate some sort of unexpected behavior.

10. Do not expect the compiler to be useful for anything but C code with some hacks to make the C++ features compile without errors. C++ compilers today seem to be rather poor when compared with what they could be. There is no reason why C++ cannot be interpreted or dynamically compiled like Java, or why no compiler that I aware of can actually compile Standard C++.

11. The true power of C++ is only realized by doing what few other languages (no other languages?) can do: combine multiple programming paradigms. There are four: procedural, modular, object-oriented, and generic. Learn all four and learn how and when to use them. People get so caught up in "procedural vs. object-oriented" and "modular vs. generic" arguments that they fail to realize that each task in a program has its own most suitable paradigm. For some tasks - perhaps a GUI - a class hierarchy with abstract classes (i.e., OOP) might very well be the best solution, while for another - a library that translates system routines into portable routines - namespaces and non-member functions might be the best choice.

Share this post


Link to post
Share on other sites