Results 1 to 4 of 4

Thread: [RESOLVED] Understanding of class methods

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [RESOLVED] Understanding of class methods

    This quote is from the book C#: The Complete Reference.

    the methods of a class typically manipulate and provide access to the data of the class. With this in mind, recall that Main( ) in the preceding examples computed the area-per-person by dividing the total area by the number of occupants. While technically correct, this is not the best way to handle this computation. The calculation of area-per-person is something that is best handled by the Building class, itself. The reason for this conclusion is easy to understand: The area-per-person of a building is dependent upon the values in the area and occupants fields, which are encapsulated by Building. Thus, it is possible for the Building class to perform this calculation on its own. Furthermore, by adding this calculation to Building, you prevent each program that uses Building from having to perform this calculation manually. This prevents the unnecessary duplication of code.
    I was hoping someone could explain this a little bit more. I still don't get why it makes any difference whether the act of this method is in main or otherwise outside of main as it's own method. The method still exists in the class right? Main is in the class. I don't understand what difference this makes to the objects.
    Thanks in advance for breaking this down further.

    I don't get any of what this line is saying:
    Furthermore, by adding this calculation to Building, you prevent each program that uses Building from having to perform this calculation manually. This prevents the unnecessary duplication of code.
    How would it be doing it manually?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Understanding of class methods

    The whole point of OOP is encapsulation. A class should contain all data (properties) and behaviour (methods) that an object of that type has. If you declare a method in the Building class that calculates the area per person then that behaviour is encapsulated within that class. Everywhere you have a building object you can call that method and get the area per person for that specific building object. Let's say that you didn't do that. Let's say that you now use the same Building class in multiple projects. In each of those projects you'll have to write the same code to calculate the area per person for a building. If it's encapsulated in the Building class itself then you only have to write it once.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Understanding of class methods

    Main() is an entry point. It should never do anything but create objects and call other methods. If they were using Main() to mean something other than an entry point, it's a very badly chosen name.

    It's also a terribly contrived and trivial example, as is the norm with textbooks. You're quite correct: no-one except professors give two hoots whether Building does its own division operation or whether you do it yourself. But the theory starts to make sense when you have more complex operations.

    Classes are, by definition, reusable: they exist to avoid code duplication and redundancy. Classes encapsulate both data and logic. This means that any time a class can do its own work, it should: such as in this example. Suppose that the calculation of area per person was terribly complex and no-one could remember how to do it without hunting through cross-references in a 500-page manual. In this case, it makes sense to write the operation once (in the Building class) and simply call this function from each place it is required. This avoids the need for redundant code, hence reducing the number of chances of errors and easing maintainability (in case the formula for area per person somehow changes).

    (These same principles all apply in procedural and functional languages, of course. OO proponents simply like to make a big deal of it as if it's a feature unique to OO. But never mind about that.)


    In case you're still thinking "what's the point", consider the scalability of both methods: one involves only ever writing the code once no matter the complexity (linear scalability); the other involves always writing the same code every time (exponential scalability). You can guess which model tends to be chosen for serious development.

  4. #4

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: Understanding of class methods

    Thank you so much. Those were excellent explanations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width