function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MrTheTylerMrTheTyler 

Best Practices: Constructor or Initialization?

Been reading the docs (Using Constructors & Using Initialization Code) trying to understand when to use initializer vs. constructor.  In the "Using Initialization Code" link it is stated:

 

 

If you do not want to write your own constructor for a class, you can use an instance initialization code block to initialize instance variables. However, most of the time you should either give the variable a default value or use the body of a constructor to do initialization and not use instance initialization code.

 

 

It's a bit vague.  Is there a good rule of thumb to follow?

 

Cheers,

 

Tyler

John De SantiagoJohn De Santiago
I typically only use constructors to set default variables for my properties and member variables. I use initialize methods to perform any startup logic. Keeps my class construction free of any logic.
paul-lmipaul-lmi

if you're in visualforce, and are using any of the ajax tags a lot (actionfunction, actionsupport, etc), you would be wise to bind any "sticky" UI list elements to variables that are set in a constructor, rather than having properties or getter methods that directly query the DB to get the data.  this is because whenever you call the "action" of one of these ajax methods, all getters are called, which causes your ajax stuff to appear slower than it normally would.

 

Example (unoptimized)

 

public class myclass{
public list<account> getAccounts(){
	list<account> acc = new list<account>();
	acc = [select id, name from account limit 10];
	
	return acc;
}
}

<apex:page controller="myclass">
	<apex:repeat value="{!Accounts}" var="a">
	{!a.name} <br />
	</apex:repeat>
</apex:page>

 the above example, once extended with other public methods/properties, would call getAccounts for every ajax action you put into the page, regardless of whether you wanted to refresh that accounts list or not.  Since this scenario would be more useful if getAccounts() were only querying once (for instance, on render/page load), we move it out of a getter method, and instead populate a variable via the constructor.

 

 

public class myclass{
public myclass(){
	list<account> l = new list<account>();
	l = [select id, name from account limit 10];
	if(l.size() > 0){
		accts = l;
}
public list<account> getAccounts(){
	return accts;
}
private list<account> accts = new list<account>();
}
<apex:page controller="myclass">
	<apex:repeat value="{!Accounts}" var="a">
	{!a.name} <br />
	</apex:repeat>
</apex:page>

 in this version, the accounts list is populate via the constructor, so other page interactions will not trigger additional SOQL queries, which, as you build richer pages, can severely bog down the performance when using actionfunction/actionsupport/ajax in Visualforce.

 

Rule of thumb, if it's something that's populated on page load, and doesn't need to change via ajax, do it in the constructor, not a getter/property.

 

Doing the above via initialization code achieves the same effect in my experience, but I prefer creating the custom constructor definition vs. standalone initialization blocks.  With the constructor, you also have complete control over the order of operations as the page is rendered.

 

 

 

 

 

MrTheTylerMrTheTyler

that is some good advice on the Ajax.  Thanks!

 

One thing I've been playing around with is using initialization statements after each variable declaration in the header of my class.  What I like about this so far is that it is very easy to understand at first glance to what value the variables are being initialized.  As opposed to seeing the variables declared in the header and then cross-referencing to the constructor.

 

 

class myClass{

	case c = new case();
	{
		c.subject = 'This is a test';
		c.status = 'Closed';
	}
	

	otherClass othCl = new otherClass();
	{
		othCl.p1 = 'hello';
		othCl.p2 = 'world';

	}	


	public myClass(integer CupsofCoffee)
	{
		othCl.p3 = CupsofCoffee;

	}

}