Sometimes sudden (or slow) realisations can wreak havoc in ones mind. I've always (since long?) been a a proponent of constructor injection for dependencies. On the other hand I was not unused or diametrically opposed to constructing objects with their values usings getters and setters. But now, these setters scare me. I'm suffering from setter anxiety.

This is a simple example, this post was triggered by an object that had about eight properties of which some where required, some were not and one was calculated externally and injected in the service that stores these objects and all this state was guaranteed by chance. If that does not sound as bad modelling to you you're lost :P

Had I started modelling without setters it would never have ended up like that, there would simply have been no way for me to create the chaos.

Yes, I quoted myself :p

Lets all start suffering from setter anxiety!

P.S. This blog is a try-out. Shorter braindumps, possibly less well thought trough. I do this because a lot of posts stay stuck in conceptualisation fase :)

Share this post!

Comments

And maybe you don't even need all those setters. You might have behaviour that describes the processes better and need the data and there is no need to make it public.
But maybe I'm wrong. It is just a thought.

First of all, I would assume needing optional values is a modelling problem. Maybe there is no Product with an optional Price (as an example) but a Product and a SellableProduct. SellableProduct is a poor name, but you'll get the idea I presume.

I suffer this thought. I think should setters can help with refactoring and working.
If you put more checks into your setters you can then save yourself inside the object having more repeated if statements.
So $product->sizes can in $product->setSizes($size) check for it being an array or maybe an object and certain keys and return false if not, so you only need to test for true and false outside.

Maybe the angst is just a natural and good conditioning to make us fear adding too many properties to an object. There by getting us to evaluate after 5 or 10 if refactoring is needed?

Still not a solved question though, just one to be consistent on.

Leave a comment!

Name*

Company

Web site

E-mail address*

Comment*

Italic and bold

*This is italic*, and _so is this_.
**This is bold**, and __so is this__.

Links

This is a link to [Procurios](http://www.procurios.nl).

Lists

A bulleted list can be made with:
- Minus-signs,
+ Add-signs,
* Or an asterisk.

A numbered list can be made with:
1. List item number 1.
2. List item number 2.

Quote

The text below creates a quote:
> This is the first line.
> This is the second line.

Code

A text block with code can be created. Prefix a line with four spaces and a code-block will be made.

About me

I am a developer at Schiphol Group, the Dutch national airport. Previously I worked for Procurios, a webdevelopment agency with about 35 employees in central Netherlands.

I will write about how and why I learn things to show what being a programmer can involve. Furthermore this is a way to materialize new topics for myself :)

On this blog I share what I learn. So if I am wrong, please correct me, if I am not, rejoice: Someone is right on the internet!

@flijten RT @raganwald: Agile:
“What is really important is how people work with each other, and what they focus on, not the minutiae of ceremony a…