The Stack Overflow podcast is back! Listen to an interview with our new CEO.
    Tweeted twitter.com/#!/StackProgrammer/status/472236578056577025
4 edited title
| link

Should there be "Default approach" when creating a class from scratch: getters and setters for all of the fields of the classeverything, or limited access?

3 added 272 characters in body
source | link

Should I havethere be getters and setters for all of the fields of the class? Or should classes have more limited access?

Until recently I always had getters (and sometimes setters but not always) for all the fields in my class. It was my 'default': very automatic and I never doubted it. However recently some discussions on this site made me realize maybe it's not the best approach.

When you create a class, you often don't know exactly how it's going to be used in the future by other classes.

So in that sense, it's good to have getters and setter for all of the fields in the class. So other classes could use it in the future any way they want. Allowing this flexibility doesn't require you to over engineer anything, only to provide getters.

However some would say it's better to limit the access to a class, and only allow access to certain fields, while other fields stay completely private.

Up until recently I hadWhat is your 'default' approach when building a class from scratch? Do you make getters (and sometimes setters but not always) for all of the fields in the class. It was very automatic? Or do you always choose selectively which fields to expose through a getter and I never doubted it. However recently some discussions on this site made me realize maybe it's not the best approach.

I'm not sure what approach I should take. What is the better approach in your opinionwhich to keep completely private?

Should I have getters and setters for all of the fields of the class? Or should classes have more limited access?

When you create a class, you often don't know exactly how it's going to be used in the future by other classes.

So in that sense, it's good to have getters and setter for all of the fields in the class. So other classes could use it in the future any way they want.

However some would say it's better to limit the access to a class, and only allow access to certain fields, while other stay completely private.

Up until recently I had getters (and sometimes setters but not always) for all of the fields in the class. It was very automatic and I never doubted it. However recently some discussions on this site made me realize maybe it's not the best approach.

I'm not sure what approach I should take. What is the better approach in your opinion?

Should there be getters and setters for all of the fields of the class?

Until recently I always had getters (and sometimes setters but not always) for all the fields in my class. It was my 'default': very automatic and I never doubted it. However recently some discussions on this site made me realize maybe it's not the best approach.

When you create a class, you often don't know exactly how it's going to be used in the future by other classes.

So in that sense, it's good to have getters and setter for all of the fields in the class. So other classes could use it in the future any way they want. Allowing this flexibility doesn't require you to over engineer anything, only to provide getters.

However some would say it's better to limit the access to a class, and only allow access to certain fields, while other fields stay completely private.

What is your 'default' approach when building a class from scratch? Do you make getters for all the fields? Or do you always choose selectively which fields to expose through a getter and which to keep completely private?

2 added 272 characters in body
source | link

When you create a class, you often don't know exactly how it's going to be used in the future by other classes.

So in that sense, it's good to have getters and setter for all of the fields in the class. So other classes could use it in the future any way they want.

However some would say it's better to limit the access to a class, and only allow access to certain fields, while other stay completely private.

Up until recently I had getters (and sometimes setters but not always) for all of the fields in the class. It was very automatic and I never doubted it. However recently some discussions on this site made me realize maybe it's not the best approach.

I'm not sure what approach I should take. What is the better approach in your opinion?

When you create a class, you often don't know exactly how it's going to be used in the future by other classes.

So in that sense, it's good to have getters and setter for all of the fields in the class.

However some would say it's better to limit the access to a class, and only allow access to certain fields, while other stay completely private.

I'm not sure what approach I should take. What is the better approach in your opinion?

When you create a class, you often don't know exactly how it's going to be used in the future by other classes.

So in that sense, it's good to have getters and setter for all of the fields in the class. So other classes could use it in the future any way they want.

However some would say it's better to limit the access to a class, and only allow access to certain fields, while other stay completely private.

Up until recently I had getters (and sometimes setters but not always) for all of the fields in the class. It was very automatic and I never doubted it. However recently some discussions on this site made me realize maybe it's not the best approach.

I'm not sure what approach I should take. What is the better approach in your opinion?

1
source | link