We all love Visualforce, right? It gives us the chance to have that finer grained control over how things look for users and users love us making things look good for them – often much more than any functionality we provide. And when dealing with Visualforce, it’s often not long until we need to write a Custom Controller or a Controller Extension in Apex to allow us to fulfil some requirement or other. Controllers provide us with three main functions; to provide access to data, to provide us access to business logic through actions and to provide navigation. Actions and navigation are pretty straight forward as there is only one way to implement them in your controllers. Data however can be implemented in two ways and what’s worse… at the same time!

The Visualforce

For this example let’s take a very basic page, it doesn’t do anything except for display some data in an outputText field.

We are using a Custom Controller to supply this data and we’re binding to myVar on the controller to get this data into the page.

The Controller

So whilst the page is straight forward the controller becomes a little more exciting, relatively speaking of course. Apex is in general based on Java but it has aspects of C# thrown in as well in certain places and this is one of those places. First a quick recap of data in controllers combined with a very mini history lesson.

The Java Way

The Java-esque way of providing the myVar data to the Visualforce page is to have a “getter” method. Getter methods employ a naming convention to let the VF engine know where it can find the data to bind to. The naming of our methods have to follow the syntax: get<Name> where <Name> is the name that you want to use in your VF page and to set it you would have a method prefixed by set rather than get. So in our example to implement this in the most simplistic way we would have a varibale and two methods like so:

Notice the difference here; we are using myVar in the VF page but the method on our controller has a different name; getMyVar(). There’s nothing overly wrong with this, it’s worked for years and years in the Java world. However C# introduced a different concept; that of automatic properties.

The C# Way

Automatic properties were introduced to try and cut down on having to write lots of “boiler plate” code… so to achieve the same result for myVar the C# way would be to simply write:

This is obviously a lot easier for developers and developers generally like easier things. Hence probably why this was included in Apex (although don’t quote me on that fact).

Great, But Who Cares?

Well, I suspect in general noone really cares about this. Most developers have a prefered method of providing data in controllers and stick to it. And if multiple developers work on the same controller then obviously the process of peer code review, coupled with our coding standards documents will ensure that everyone works in the same way, yes?

Back in the real world… I have often seen both methods of accessing data implemented in the same controller. Other than upsetting me slightly it also prompts the question in my mind what would happen if my Example Controller ended up having something like this written in it:

We now have a controller that contains both method for accessing the data specified by the {!myVar} binding in the visualforce page. So what does Visualforce do in this case? This code is completely valid; so the compiler won’t help you here. The Visualforce engine is going to have to make a choice, but what choice? Implementing and running this page/controller will actually answer that question for us. To save you from worsening your copy and paste RSI I will let you know that it will prefer the C# style property rather than using the getter method.

What About Extensions?

So the VF is expressing a prefernce for the property type declaration and we have tested this in a Custom Controller. But what happens in Controller Extensions? Well, if we only have one extension then the same rule applies it will bind to the property style method first. But what happens if we have a page that references two extensions and the two different declarations are split across the two extensions like so:

What would happen here? The general rules state that the first extension in the list gets priority over the second but we now have a complication in that the second extension contains the prefered property style declaration. What to do?! Unfortunately I ave nothing exciting to report here.. the first extension continues to win despite using the getter pattern – but, still it’s worth knowing.

Just Beware

So it’s possible to provide data to a VF page through a Custom Controller or Controller Extension in two different ways. Which one of those ways you choose, really is up to you. But which ever you choose please try to remain consistent – if you come to code that uses getters and setters then follow suit, no matter how dirty it makes you feel. Because as soon as you start mixing these things up in the same class some people are going to start seeing their code not behaving as they would expect.