Best Practices for OOP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Kasta

    #1

    Best Practices for OOP

    Hi,

    I'm trying to determine what the proper way to create objects would be in C#
    (or any language, practically).
    There are two different ways of implementing objects that I can see, and I
    hope someone can give their two cents as to which is more preferred.

    Object 1: Have a Class with readonly properties and mutate all of the
    private variables directly within the class. Any function within the class
    may directly modify any private variable within the class.

    Object 2: Have a Class with both sets of properties and only access the
    Private Variables using those properties, Including directly within the
    class (The Property itself, of course can mutate / access the variable, but
    nothing else.)

    I'm torn. I can see value in both.

    In Object 1: It saves problems and you prevent any possible manipulation of
    the properties from outside sources. (You can have a readonly Get property,
    and any holding a reference the Object 1 can't manipulate the private
    variable at all. This is good, right?!

    In Object 2: You risk having outside objects manipulating the private
    variables. BUT, in inherited classes, you can change practically anything
    and not have to worry about changing a base class if you need functionality.


    Any thoughts on this, people?

    -J



  • Jack Mayhoff [MSFT]

    #2
    Re: Best Practices for OOP

    Do what best suits you. Nuff said.

    Just because design patterns have been written about, doesn't mean you have
    to adhear to them.


    --

    Jack Mayhoff
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.

    "Joe Kasta" <J@microkroff.c om> wrote in message
    news:O4gY8wYnDH A.964@TK2MSFTNG P10.phx.gbl...[color=blue]
    > Hi,
    >
    > I'm trying to determine what the proper way to create objects would be in[/color]
    C#[color=blue]
    > (or any language, practically).
    > There are two different ways of implementing objects that I can see, and I
    > hope someone can give their two cents as to which is more preferred.
    >
    > Object 1: Have a Class with readonly properties and mutate all of the
    > private variables directly within the class. Any function within the class
    > may directly modify any private variable within the class.
    >
    > Object 2: Have a Class with both sets of properties and only access the
    > Private Variables using those properties, Including directly within the
    > class (The Property itself, of course can mutate / access the variable,[/color]
    but[color=blue]
    > nothing else.)
    >
    > I'm torn. I can see value in both.
    >
    > In Object 1: It saves problems and you prevent any possible manipulation[/color]
    of[color=blue]
    > the properties from outside sources. (You can have a readonly Get[/color]
    property,[color=blue]
    > and any holding a reference the Object 1 can't manipulate the private
    > variable at all. This is good, right?!
    >
    > In Object 2: You risk having outside objects manipulating the private
    > variables. BUT, in inherited classes, you can change practically anything
    > and not have to worry about changing a base class if you need[/color]
    functionality.[color=blue]
    >
    >
    > Any thoughts on this, people?
    >
    > -J
    >
    >
    >[/color]


    Comment

    • Daniel O'Connell

      #3
      Re: Best Practices for OOP


      "Jack Mayhoff [MSFT]" <v-jacoff@online.m icrosoft.com> wrote in message
      news:eweaP1YnDH A.2964@tk2msftn gp13.phx.gbl...[color=blue]
      > Do what best suits you. Nuff said.
      >
      > Just because design patterns have been written about, doesn't mean you[/color]
      have[color=blue]
      > to adhear to them.
      >[/color]

      As much as I hate to...I have to agree on this. Do what makes sense in your
      situation.
      Don't be afraid to have to subclass or put a little extra work in to get the
      project working.

      However, this guy isn't actually from MS, so in general ignore him, ;).
      [color=blue]
      >
      > --
      >
      > Jack Mayhoff
      > Microsoft Online Partner Support
      > Get Secure! - www.microsoft.com/security
      > This posting is provided "as is" with no warranties and confers no rights.
      >
      > "Joe Kasta" <J@microkroff.c om> wrote in message
      > news:O4gY8wYnDH A.964@TK2MSFTNG P10.phx.gbl...[color=green]
      > > Hi,
      > >
      > > I'm trying to determine what the proper way to create objects would be[/color][/color]
      in[color=blue]
      > C#[color=green]
      > > (or any language, practically).
      > > There are two different ways of implementing objects that I can see, and[/color][/color]
      I[color=blue][color=green]
      > > hope someone can give their two cents as to which is more preferred.
      > >
      > > Object 1: Have a Class with readonly properties and mutate all of the
      > > private variables directly within the class. Any function within the[/color][/color]
      class[color=blue][color=green]
      > > may directly modify any private variable within the class.
      > >
      > > Object 2: Have a Class with both sets of properties and only access the
      > > Private Variables using those properties, Including directly within the
      > > class (The Property itself, of course can mutate / access the variable,[/color]
      > but[color=green]
      > > nothing else.)
      > >
      > > I'm torn. I can see value in both.
      > >
      > > In Object 1: It saves problems and you prevent any possible manipulation[/color]
      > of[color=green]
      > > the properties from outside sources. (You can have a readonly Get[/color]
      > property,[color=green]
      > > and any holding a reference the Object 1 can't manipulate the private
      > > variable at all. This is good, right?!
      > >
      > > In Object 2: You risk having outside objects manipulating the private
      > > variables. BUT, in inherited classes, you can change practically[/color][/color]
      anything[color=blue][color=green]
      > > and not have to worry about changing a base class if you need[/color]
      > functionality.[color=green]
      > >
      > >
      > > Any thoughts on this, people?
      > >
      > > -J
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Joe Kasta

        #4
        This guy ain't from Microsoft.

        Yeah,

        I figured that one out pretty quickly Just goes to show you how professional
        non-Microsoft people are..

        Thanks for the assistance, though.

        -J


        Comment

        • Michael A. Covington

          #5
          Re: This guy ain't from Microsoft.


          "Joe Kasta" <J@microkroff.c om> wrote in message
          news:%238buxmhn DHA.684@TK2MSFT NGP09.phx.gbl.. .[color=blue]
          > Yeah,
          >
          > I figured that one out pretty quickly Just goes to show you how[/color]
          professional[color=blue]
          > non-Microsoft people are..
          >
          > Thanks for the assistance, though.
          >
          > -J[/color]


          Er... We haven't a clue what you're talking about. Presumably you are
          referring to an earlier message here, but there are hundreds per day and we
          don't know which one you have in mind.

          Care to elucidate?


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: This guy ain't from Microsoft.

            Michael A. Covington <look@www.covin gtoninnovations .com.for.addres s>
            wrote:[color=blue]
            > Er... We haven't a clue what you're talking about. Presumably you are
            > referring to an earlier message here, but there are hundreds per day and we
            > don't know which one you have in mind.[/color]

            He's referring to messages from "Jack Mayhoff [MSFT]" - including one
            earlier in this very thread.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...