Results 1 to 9 of 9

Thread: OOP Best Practices

  1. #1

    Thread Starter
    Lively Member tamjap's Avatar
    Join Date
    Jun 2007
    Posts
    99

    OOP Best Practices

    My first two programming jobs have been working on products that were largely designed and written by procedural programmers. Despite all the schooling on OOP, I am still a little shaky on some of the 'correct' ways of doing things. Here is my question of the day:

    My project has a database backend and is set up with several objects, most of which contain 20 or more properties. If I am working on a particular screen (web page in this case), and I need one or maybe two fields from a particular object, is it worth the extra overhead to instantiate the entire object, filling all 20 or so fields, just to grab that one that I need? Or is it better to just write a simple SQL statement and grab the data directly?

    Thanks for your input!
    • That's the way things come clear. All of a sudden. And then you realize how obvious they've been all along.

    - Madeleine L'Engle

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: OOP Best Practices

    The answer is that it depends. OOP isn't necessarily the "correct" way to program, it's a programming model that allows for more extensibility and easier reuse of components.

    If you just need to create a page that displays a couple of fields from a database table, it's generally easier to just pull out those two fields and display them. If you're going to work with the data and do a lot of stuff with it, then it would probably save you a bunch of trouble to create some objects and then add methods to them, but if you're just pulling data to show on a screen, it's not necessary.
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: OOP Best Practices

    Another consideration is whether the initial design separated database from UI. If there were classes for dealing with the database, and those classes served up the data, then there would be a long-term cost in maintainability if you simply bypassed the structure to fill a special need. At that point the structure becomes "These classes interact with the database....along with the occasional other item whenever somebody felt it was convenient."
    My usual boring signature: Nothing

  4. #4
    Fanatic Member CodedFire's Avatar
    Join Date
    Aug 2007
    Location
    In Cog Neato
    Posts
    568

    Re: OOP Best Practices

    Is this not what partial classes are all about, basicly you make loads of little partial classes into one big database class, that way you only have to use the parts of the class you need when creating the object.
    Languages: Visual Basic 05/08, C# 08
    IDE: Express Editions
    Framework: 2.0, 3.0, 3.5


    Lesson 5: Don't take domestic advice from perpetual singles. - Mendhak

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: OOP Best Practices

    You can call a method which returns that object with just those few required fields populated.

    Partial classes are all part of the same class, so in the end they compile as the same object. The advantage of partial classes is that you can separate out bits that you may not touch (like designer code) or it allows different developers to work on the same class in a solution.

  6. #6
    Fanatic Member CodedFire's Avatar
    Join Date
    Aug 2007
    Location
    In Cog Neato
    Posts
    568

    Re: OOP Best Practices

    Can you not instantiate a partial part of a partial class as its own object?
    Languages: Visual Basic 05/08, C# 08
    IDE: Express Editions
    Framework: 2.0, 3.0, 3.5


    Lesson 5: Don't take domestic advice from perpetual singles. - Mendhak

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: OOP Best Practices

    You can have a file1.vb

    vb Code:
    1. Public Class CodedFire
    2.  
    3. Public Function GetJoinDate() As String
    4. Return "Aug 07"
    5. End Function
    6.  
    7. End Class

    And in file2.vb,

    vb Code:
    1. Partial Class CodedFire
    2.  
    3. Public Function GetLocation As String
    4. Return "In Cog Neato"
    5. End Function
    6.  
    7. End Class

    Now when you instantiate CodedFire, all of the methods and properties from all of the partial classes for CodedFire from all over the place are given to you in a single object.

  8. #8
    Fanatic Member CodedFire's Avatar
    Join Date
    Aug 2007
    Location
    In Cog Neato
    Posts
    568

    Re: OOP Best Practices

    Wow thats not how i understood them at all, well it is but i also thought you could use the partial part as a seperate class.

    Thanks mendhak
    Languages: Visual Basic 05/08, C# 08
    IDE: Express Editions
    Framework: 2.0, 3.0, 3.5


    Lesson 5: Don't take domestic advice from perpetual singles. - Mendhak

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: OOP Best Practices

    No prob, and now you see how the new 'designer.vb' concept works. Visual Studio places the declaration of the objects on your form/web form in the designer class so that you don't have to deal with it.

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