Thread: Coding style

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Coding style

    I would like to hear some thoughts about two different ways to code.
    Would you put all variables int the beginning of the class and after that the functions, or do you mix them around?

    Lets say you have a function that uses a list of integers. That list is only used by that function. Would you do
    Code:
    class A
    {
       int a;
       int b;
       int list[20];
       ....
       void fun1();
       void fun2();
       void listFun();
       ...
    };
    or
    Code:
    class A
    {
       int a;
       int b;   
       ....
       void fun1();
       void fun2();
       int list[20];
       void listFun();
       ...
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    Would you put all variables int the beginning of the class and after that the functions, or do you mix them around?
    Generally, I declare the public members of a class first, followed by protected members, followed by private members. Since member variables are usually private, they are thus declared towards the end.

    The rationale is that everyone interested in the class would normally be interested in the public interface, but only those wanting to inherit from the class would also be interested in the protected interface, and only those needing to maintain the class would also be interested in the private implementation, hence this presents the parts of the class that the most people would be normally interested in first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it bad style of coding ??
    By noobcpp in forum C++ Programming
    Replies: 15
    Last Post: 11-06-2008, 10:39 AM
  2. Your Coding Style?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 45
    Last Post: 06-02-2005, 08:19 AM
  3. Coding style?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 10-22-2003, 11:26 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. coding style
    By ActionMan in forum Linux Programming
    Replies: 1
    Last Post: 10-03-2001, 07:36 AM