Methods before Members or Members before Methods?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Methods before Members or Members before Methods?

  1. #1
    Join Date
    Dec 2005
    Posts
    134

    Methods before Members or Members before Methods?

    Methods before Members or Members before Methods?

    In a header file, which do you define/declare first. Do you list all your variables before the methods (also known as functions), or do you do it the other way around?

    I always thought that you put the variables first. That was the way I was taught in school. Now I hear that that is wrong and you have to do it the other way. I am told you should list the methods first.

    I know this is all just asthetic. But there must be a set accepted way to do this. What is it?

  2. #2
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    I've always done it the former way, too. The logic being that your methods will presumably be using the variables and you can't use something until you declare it, right?

    But, as you said, it will work either way, what with prototyping just needing to know which types you wish to use, but not the actual variables themselves.

    'Hope that made sense...

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    Your company will either define this in a policy or leave it up to you. It does not really matter. One idea is to put the public, exposed functions toward the top for your users. For a class that is reused and shared and all that good stuff only a few public functions should define the interface, and normally no variables at all, so that is what is toward the top.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    It really doesn't matter so long as you stick to a consistent convention.
    I prefer to declare the public member functions first because users of the class are more interested in the interface rather than the implementation. This "member functions first" is supported by another convention: I always declare public members first, protected and private coming last.
    Danny Kalev

  5. #5
    Join Date
    May 2007
    Posts
    843
    This is a issues of personal style. From the creator of C++, he always declare the variable with the keyword private rather than method first.

    I will also declare variable first without private key word.

    I hope this help.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    declaring variables without an explicit "private" keyword isn't recommended for two reasons: the code is less readable, and worse yet, if your later change class to struct, your variables become public.
    Danny Kalev

  7. #7
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    What I have seen is that the variables (called fields or properties in other languages) are written first in object oriented languages like Java or C++. And of course, it is better to specify the private access modifier in C++ (in Java, you have to specify the access specifier for each variable, or it takes the default). And, according to some object oriented programmers, it is considered a bad practise to have any public properties. They use getter and setter functions (methods) to access / modify the variables. This usually helps to make the class and objects more solid.

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    I havent figured out that one yet. How is a function as follows:

    classname::setX(int value)
    {
    x = value;
    }

    more solid than

    classinstance.x = value;

    Its the exact same thing. Granted, for externals (human, file, etc) you sometimes have to watch for bad values and protect against them which requires a set method, but for the vast majority (or at least for mathematical things) all values are legal and the set function is just 4 wasted lines of code.

  9. #9
    Join Date
    May 2007
    Posts
    843
    Thanks Danny for help.

  10. #10
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by jonnin
    I havent figured out that one yet. How is a function as follows:

    classname::setX(int value)
    {
    x = value;
    }

    more solid than

    classinstance.x = value;

    Its the exact same thing. Granted, for externals (human, file, etc) you sometimes have to watch for bad values and protect against them which requires a set method, but for the vast majority (or at least for mathematical things) all values are legal and the set function is just 4 wasted lines of code.
    Well, assuming that we're talking about a proper class that has such setters, the member function has two advantages: it hides implementation details and it enables you to isolate design changes to a single place. I'll give you an example: suppose that at a later stage you decide to replace the int member of the class with an object of type Int. In that case, you will only hae to modify the implementation of the member function while making this code change transparent to the users. Notice also that there are good chances that the function will be inlined so there's no performance penalty here. Finally, suppose you want to profile your code and check how many times the function is called. You can add a static counter that's incremented with every call, or you can add a logging operation to the member function without forcing users to moidfy their calls of that function.
    Danny Kalev

  11. #11
    Join Date
    May 2007
    Posts
    843
    Danny is a C++ programmer king.

Similar Threads

  1. Replies: 2
    Last Post: 10-13-2005, 12:16 PM
  2. Comparison methods
    By ericelysia1 in forum Java
    Replies: 34
    Last Post: 05-15-2005, 07:39 PM
  3. How to model "virtual" attributes and methods in UML
    By Larry Hayashi in forum Architecture and Design
    Replies: 2
    Last Post: 05-01-2003, 12:33 PM
  4. Hiding methods from COM
    By Frank Oquendo in forum .NET
    Replies: 0
    Last Post: 07-24-2002, 02:41 PM
  5. Replies: 4
    Last Post: 10-27-2000, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center