Author Topic: OO programming.  (Read 505 times)

Offline raestlyn

  • Level 29
  • **
  • Posts: 464
  • Reputation: +9/-5
    • View Profile
OO programming.
« on: December 09, 2008, 03:33:14 AM »
I'm quite new to the concept of OOP, and I have been reading quite lot of it from the net lately. I did come across to a site where the basic principles of it had been questioned and I did find myself agreeing about the stuff the guy wrote. I haven't got anyone to tutor or guide me when I learned to code, so I don't know what is "the right way" to code OOP. Thats why I did find one thing very interesting, the use of 'getters' and 'setters'. I just can't see a reason to use them. Article about not to use them made me think even more why they are commonly used. Can someone with a lot more experience tell me why they are so good?


I can send you pics of my cocks if you want reference.


Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: OO programming.
« Reply #1 on: December 09, 2008, 05:09:20 AM »
You can get/set private variables, and also since the setters/getters are functions, you can do a lot more while setting/getting a variable, ie. check for permissions, set other variables, call other functions, etc.

If you think it's a lot of extra code because you have a lot of variables, you can also create generic set/get functions that will set/get any variable, as long as the variable exists. I don't remember the code for that, but I'm sure you could find it in the PHP docs.

Offline Scion

  • Level 27
  • **
  • Posts: 402
  • Reputation: +11/-0
    • View Profile
Re: OO programming.
« Reply #2 on: December 09, 2008, 05:55:03 AM »
Getters and Setters arnt evil.....

Just like everything else its how you use them thats evil....or not...depending on your view...

In Java its quite common to see objects with a getter and a setter for every single data item in it....these objects have a special name theyre called beans....

Many frameworks require that objects obey the bean pattern....

so a lot of developers will simply generate the full set of getters and setters for their object without first thinking what methods they will actually need, this is exasperated through the use of automatic tools for this...the unfortunate side effect is that the getters and setters ecourage developers to chain methods together.....

manager.getForm().getParameters().getGizmo();

The author of the JavaWorld blog is correct in that its better to actually think about what your doing and then delegate that action to the object that is most resonsible....this also has the benifit that it reduces the dependancies between objects....

However like i said getters and setters are not bad in of themselves.....

in fact one major benifit of using them over using the object properties/variables directly is that they hide the actual implementation of those variables....

for instance if you have a player object and a weapon object then you could choose to do

Player.weapon = weapon;

or

Player.setWeapon(weapon);

both ultimately achieve the same thing while the setWeapon method simply does "this.weapon = weapon;" but as soon as it starts doing something else....like

public void setWeapon(Weapon weapon) {
  this.weaponSwaps++;
  this.weapon = weapon;
}

your in trouble.....with the first aproach you will need to add Player.weaponSwaps++ everywhere that you set a players weapons....with the second aproach your already finished....if your still not convinced...lets say now you want to add some logging and a check to see if the swaps is too high...and check that the player can use that weapon....and ...and....

And that sort of also explains one of the main points of oo programming....An Object provides a convienient encapsulation for all the methods that act upon a common set of data....in this case the methods that act upon a player....

Objects however are not the panacea that some believe them to be ...  objects are not great at representing pure data structures, that is data without methods....if thats what your doing then you dont need to use getter and setters....and there may be a better option than a full blown object.








Offline genmac

  • Level 7
  • *
  • Posts: 34
  • Reputation: +0/-0
    • View Profile
    • Castle Siege
Re: OO programming.
« Reply #3 on: December 09, 2008, 03:23:35 PM »
Yeah the point of having a whole extra function for getting and setting is so you can do post/pre processing on the variable before assigning it.  If you don't need that, you can always just declare the variables public in the class and access them directly.
Castle Siege - persistent browser based medieval warfare

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal