Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualDARSC0D3

Posted 09 October 2012 - 08:29 AM

First I will answer your question.
No you don't need to write setters and setter for every field in your class. Only if think you are going to use them or have the need for them to be public.
Making such decision is all up to you and is called software design. Use your logic to layout the classes in such way that they will work together but also will not cause problems behavior wise when you are able to modify properties publicly.

As for getters and setters them self. I have feel obligated to say they are not so great as every body might think. Actually they are even evil. First of all for the fact that most people never put anything in a getter setter block besideds passing true a value which is actually just mindless typing working which doesn't make sense at all.
Secondly for the fact that getters and setters are no more than syntax sugar in the end it is a function that return a fields value and set a value using function. With that said it more logical to make a function/method that has a better description about what the operation is going to preform on a class.

Compare the following example
[source lang="java"] spaceship = new SpaceShip(imgSpaceShip, posx, posy); if( spaceship.x < screen.width) spaceship.x += speed; if( spaceship.Y < screen.width) spaceship.y += speed;[/source]

to..

[source lang="java"] spaceship = new SpaceShip(imgSpaceShip,posx,posy,speed); spaceship.move(screen);[/source]

This is a small part pseudo code that reflects real good what I mean that using getters and setters does not promote better software design. It is not even considered OOP.
I used to be a big fan off getters and setters until I starting using more languages that had no syntax sugar for getters and setters. At first it hated it then i found out that I wrote better OOP and made less mistakes due better open close principle and that methods attached to classes where better descriptions on them self. If you would still feel the need for model / object to have mainly public properties you might think of using a struct. Therefor I have to disagree with people in this thread whom claim that structs and classes are the same this is simple not true.

#1DARSC0D3

Posted 09 October 2012 - 08:28 AM

First I will answer your question.
No you don't need to write setters and setter for every field in your class. Only if think you are going to use them or have the need for them to be public.
Making such decision is all up to you and is called software design. Use your logic to layout the classes in such way that they will work together but also will not cause problems behavior wise when you are able to modify properties publicly.

As for getters and setters them self. I have feel obligated to say they are not so great as every body might think. Actually they are even evil. First of all for the fact that most people never put anything in a getter setter block besideds passing true a value which is actually just mindless typing working which doesn't make sense at all.
Secondly for the fact that getters and setters are no more than syntax sugar in the end it is a function that return a fields value and set a value using function. With that said it more logical to make a function/method that has a better description about what the operation is going to preform on a class.

Compare the following example
[source lang="java"] spaceship = new SpaceShip(imgSpaceShip, posx, posy); if( spaceship.x < screen.width) spaceship.x += speed; if( spaceship.Y < screen.width) spaceship.y += speed;[/source]

[source lang="java"] spaceship = new SpaceShip(imgSpaceShip,posx,posy,speed); spaceship.move(screen);[/source]

This is a small part pseudo code that reflects real good what I mean that using getters and setters does not promote better software design. It is not even considered OOP.
I used to be a big fan off getters and setters until I starting using more languages that had no syntax sugar for getters and setters. At first it hated it then i found out that I wrote better OOP and made less mistakes due better open close principle and that methods attached to classes where better descriptions on them self. If you would still feel the need for model / object to have mainly public properties you might think of using a struct. Therefor I have to disagree with people in this thread whom claim that structs and classes are the same this is simple not true.

PARTNERS