Good principles needed.

This is a discussion on Good principles needed. within the C++ Programming forums, part of the General Programming Boards category; I need a list of the DOs and DO NOTs in c++. Like for example, it is good practice to ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    106

    Good principles needed.

    I need a list of the DOs and DO NOTs in c++. Like for example, it is good practice to break up your code into functions, even if the code works others -- good practice and style, you know?

    One we learned in my programming class was that most programmers who program industrially do not comment their code, and the prof stressed out that you should comment A LOT -- I'm interested in things of this nature.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by elninio View Post
    I need a list of the DOs and DO NOTs in c++. Like for example, it is good practice to break up your code into functions, even if the code works others -- good practice and style, you know?

    One we learned in my programming class was that most programmers who program industrially do not comment their code, and the prof stressed out that you should comment A LOT -- I'm interested in things of this nature.
    Yes, your code should have small functions that each do a single task each. I'm working with some ancient code that has monolithic functions that are 100-1000 lines long with almost no comments. I wish I could meet the person who wrote the code so I could "thank" them appropriately for the mess they made.

    Get this book, it's something every C++ programmer should have:
    C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    http://www.parashift.com/c++-faq-lite/ is one resource that comes to mind
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    You only comment what you need to. You dont comment the obvious for one thing.

    Code:
    i++: // increment i
    You should only comment complex things that you may find hard to understand if you re-read it in 3 months time for example and may of forgotten how somthing works. Its also usually a good idea to place a small comment above each function prototype header so you know what the function does before you read the code.

    As for the dos and donts, its really programmer pecific id say, but in general these are what I follow:

    DO:

    Break your code into functions to maintain good readability
    Use good readable indentation - 3 to 4 spaces max
    Ensure you use variable names that are obvious and dont require a comment to verify it
    When using classes, split it into three files if possible - ensuring implement is split from definition
    Use const reference to pass an object to a function if you dont itend the function to change it
    Use const as much as possible to protect data passing to and returning from a function
    Only use pointers when you need to, dont use them for the sake of it
    Get into the habit of using vectors over dynamic C-style arrays when possible
    Practice safe computing - especially using memory allocation
    Remember to use "delete" when you are finished with "new" using pointers.
    Make sure your code is readable, and maintainable by another person reading it
    Ensure a function preforms a suitable task and when needed returns a result

    DONT:

    Create a pointer and forget to initialize it a value
    Same goes for a constant ^^
    Use bad indentation - it makes reading a program a nightmare
    (When possible ) rely on the compiler to create a default constructor - create your own as much as possible
    Create functions for the hell of it - like above, make sure they preform a required task
    Try to re-initalize a constant variable
    Return an array/vector from a function
    Forget the usefulness of references, they are "safer"pointers, but you do need pointers on some occasions of course
    Use void main()
    Mix C code with C++ in one program
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I'd like to second the recommendations of C++ Coding Standards and C++ FAQ Lite. Two excellent sources for these kinds of best practices.


    >> When using classes, split it into three files if possible
    ?? What's the third?

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Daved
    >> When using classes, split it into three files if possible
    ?? What's the third?
    Not sure, perhaps a reference to PIMPL?

    Two resources:
    Man Pages.
    GIYF

    Do:
    Close all resources. (Memory, files, network handles, etc.) No more of this "oh, you just have to reboot it every week/day/hour/second."
    Choose the appropriate container.
    Treat compiler warnings as errors until you have a good reason to do otherwise.
    Be aware of thread-safety, exception-safety, or any other *-safety requirements of your project/environment.

    Don't:
    Suffer from NIH (not invented here) - if there is an appropriate (perhaps open source) library available, use it. (This is a sub-rule of: right tool for right job)
    Have your code show up on this site's front page.
    Needlessly sacrifice portability. Sometimes you know you can, but if there's no good reason, don't. Wrappers can help bridge the differences between OS.
    Blow resources out of the water. Just because your test environment has 2TB of everything doesn't mean the end users' will.*

    [rant]* My former school system has a web-based grading/attendance software. Every six weeks, the teachers enter grade. Every six weeks, the system is brought to its knees. (Plus, my schedule every year seemed to be the output of rand(). Scheduling software should not place a senior in freshman band.) [/rant]
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    (Plus, my schedule every year seemed to be the output of rand(). Scheduling software should not place a senior in freshman band.)
    When Bill Gates was in school, he wrote a scheduler that placed him in classes with the hot chicks.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Start with the C++ FAQs book by Cline, Lomow, and Girou, and then read Scott Meyers' books.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Cactus_Hugger View Post
    Suffer from NIH (not invented here) - if there is an appropriate (perhaps open source) library available, use it.
    I would add a caveat to this rule. Use the appropriate library if it's active and well-supported and/or you have the source code.

    My current client got burned going with a couple vendors to provide software instead of developing it in house. Two years later, we're in the process of replacing one with another 3rd party and re-writing the other. Bad support in both cases, bad quality in one.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by swgh View Post
    DONT:
    (When possible ) rely on the compiler to create a default constructor - create your own as much as possible
    Why not? Putting an empty constructor has the same effect. What's the benefit? Sure, if you want to actually do something besides default initialization, then you need to put in a constructor, but what's the point of creating identical code to what the compiler effectively generates for you?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by King Mir View Post
    Why not? Putting an empty constructor has the same effect. What's the benefit? Sure, if you want to actually do something besides default initialization, then you need to put in a constructor, but what's the point of creating identical code to what the compiler effectively generates for you?
    Well, I'd not say that the constructor I'd provide would be the same as the one provided by the compiler...
    Consider:
    Code:
    struct A
    {
    	double a;
    	double b;
    	int c;
    	char *d;
    };
    The default generated by the compiler does nothing, and the structure is left in an undefined state.
    Barring some other constructor that has a purpose, a default constructor that I write would initialize those to some sane value. (Probably 0, 0, 0, NULL here)
    Edit: Now, if your struct/class is a composition of other classes like vectors, lists, strings, etc - things that initialize themselves to sane values, then yeah, there's no point in writing a default constructor - the compiler's will work fine.
    Last edited by Cactus_Hugger; 07-19-2008 at 11:54 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here's some more rules (although I'm pretty sure they're in the book I mentioned):
    - Use RAII as often as possible.
    - Learn the STL thoroughly and use STL algorithms rather than writing your own.
    - Don't reinvent the wheel. Look at what's already available before you write a new function/class.
    - Learn the benefits/drawbacks of each STL container, and use the right STL container for the job.
    - Declare variables as close as possible to the place of first use. Don't put all your variables at the top of the function, this isn't C after all.
    - Don't write C+ code (i.e. C code + classes). There's a lot more to C++ than just classes.
    - Learn how to use & write templates and use them whenever you need a generic solution to a problem.
    - Don't write platform specific code if there's a more portable solution available.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by Cactus_Hugger View Post
    Well, I'd not say that the constructor I'd provide would be the same as the one provided by the compiler...
    Consider:
    Code:
    struct A
    {
    	double a;
    	double b;
    	int c;
    	char *d;
    };
    The default generated by the compiler does nothing, and the structure is left in an undefined state.
    Barring some other constructor that has a purpose, a default constructor that I write would initialize those to some sane value. (Probably 0, 0, 0, NULL here)
    Edit: Now, if your struct/class is a composition of other classes like vectors, lists, strings, etc - things that initialize themselves to sane values, then yeah, there's no point in writing a default constructor - the compiler's will work fine.
    I see.

    That's basically a special case of "always initialize variables". But it is something to keep in mind, since with classes the code that uses the variables isn't near the code that initializes them.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm going to add one more thing to my list. This is a bit dependent on the software, but:

    I operate two fairly large pieces of equipment. (One is controlled by a pair of Win2k PCs, the other is a small embedded computer with a touchscreen interface.) One thing I want from both: I should have a way to get extended, detailed information on its inner workings, with one or two pushes of a button. This is (or would be) great to have if I think it's acting abnormal somehow. The Win2k ones especially annoy me - if something is up, usually its response is a very cryptic 32bit error code. (App specific, not Win32 codes) Words speak louder than 32bits!
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,668
    Quote Originally Posted by swgh View Post
    Mix C code with C++ in one program
    Heh, I did that once. Not pretty.

    Don't cast floats directly to DWORD, cast it to long first.
    Use spaces to indent not tabs, or else your code's formating becomes IDE dependent.
    Align your structs! Make sure all the struct's vars are aligned on a byte evenly divisible by the size of the var itself. (I.E don't have a WORD sitting on an odd byte)
    When commenting, say why your doing what your doing, not what your doing.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good architecture/design needed for "sample pipeline"
    By johny145 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 10:43 AM
  2. Conversion with a twist (help needed)
    By Mosquito in forum C++ Programming
    Replies: 7
    Last Post: 06-09-2005, 10:14 PM
  3. Direct3D help needed
    By kawk in forum Game Programming
    Replies: 1
    Last Post: 02-28-2005, 06:13 PM
  4. How good do you have to be to get into the business?
    By SmashBro in forum Game Programming
    Replies: 13
    Last Post: 12-16-2002, 12:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21