Skip to main content
Homestar9
Inspiring
May 10, 2018
Question

Best Practice Question: Should You Have Getters and Setters for Private Properties in a Component?

  • May 10, 2018
  • 3 replies
  • 648 views

Let's say you have a component with a private property (a dependency) which will be injected into the component when it is instantiated.  Now, when you're working inside the component, is it considered best-practice to have getters and setters for private properties even when they will never be exposed outside of the component?  

I've seen some examples online in other languages (e.g. C#) where dependencies are injected and then referenced without relying on getters/setters.  I haven't really been able to find a consensus on the optimal way to proceed when it comes to private properties.

Here's an example of a simple component that has a dependency (dependencyExample) which exists as a private property. 

In this first example, I've used getters and setters which set the property when the component is instantiated (setDependencyExample()).  Then, when a method (doSomething()) needs to utilize the property, the getter (getDependencyExample()) gets triggered in order to gain access to it.

https://paste.ofcode.org/ubhMPgSZR3rvExxES6yLHB

Now here's a second example where the private property does not have any getters/setters.  Instead, the property is set and accessed directly via the variables scope:

https://paste.ofcode.org/JjbUWc6PBMez42i8UVgHn5

It seems to me that if you don't use getters/setters for private properties, you wind up with  potentially much leaner code (if you have a lot of private properties).   However, I'm not sure if this way of doing things violates some best-practice rule of OOP.   I would love to hear your thoughts on the matter. 

This topic has been closed for replies.

3 replies

Homestar9
Homestar9Author
Inspiring
June 5, 2018

Thanks BKBK for the insight.

I definitely agree with you and have a rule for myself to only create getters/setters if the property is public.  The only exception to this rule would be for any private property that needs to have some other behavior or validation occur when set.  In the case of the latter, I make sure the setter/getter is private as well so it is not exposed.

e.g. Public Properties:

// public getter
function getName() {
     return variables.name
}

// public setter
function setName( required value ) {
     variables.name = arguments.value
}

e.g. Private Properties

// no getter/setter needed for something like a dependency class
function init( required privateDependency ) {
    variables.privateDependency = arguments.privateDependency;
    variables.privateDependency.doSomething();
}

// setter needed for private property that needs to do something when set
private function setPrivateValue( required value ) {
     // perform validation or do something magical
     ...

     // set it to the variables scope
     variables.privateValue = arguments.value;

}

BKBK
Community Expert
Community Expert
June 3, 2018

Homestar9 wrote:

Now, when you're working inside the component, is it considered best-practice to have getters and setters for private properties even when they will never be exposed outside of the component?

No. Object-Oriented Design best-practice says "private remains private (the rule), unless you are compelled to expose (the exception)". That's encapsulation or information-hiding. The "unless" means that getters and setters are optional.

It seems to me that if you don't use getters/setters for private properties, you wind up with  potentially much leaner code (if you have a lot of private properties). However, I'm not sure if this way of doing things violates some best-practice rule of OOP. 

Not exposing any private properties via getters and setters is actually best-practice. You introduce getters and setters only because you must. And you restrict the getters/setters only to those properties which the caller requires to use the component. That is the principle of encapsulation.

I would think of it this way. As component or class, I am the server or service. Whoever invokes my methods is the client.

Now, think of the client as someone at the other end of the telephone, asking questions. Then it may be necessary to enable the client to

getFirstname ()

or even to

getLastname().

Will you allow them to

getAddress()?

getDateOfBirth()?

getSSN()?

Were they to ask me such, I guess my answer would be "And a nice day to you, too.".

WolfShade
Legend
May 11, 2018

I agree with you on the bit about having leaner code without the getters/setters.  And I'm not sure if there is any best-practice violation by not using them.  However, I will say this:

I took over as lead developer where I work, and the previous lead has been pissing me off for years even though he's no longer here.  Why?  Well, because he discovered ColdFusion Builder and it's ability to point to any DNS in CFAdmin and create CFCs for every table that all have getter/setter.  It has created what I refer to as "Celtic knot" processing because it creates a Gateway, DAO, and bean for every table, and they all become involved for every transaction.  The old site (we recently updated) contained (I'm not kidding) 3500 files (.cfm, .cfc) partly because we had so many tables in our database.

The updated site contains less than 200 cfm and cfc files beause I manually created only what we needed, and I'm only putting bare minimum code in our components.  I feel like the site processes faster without all the get/set stuff.

But that's just my opinion.  I don't have numbers to back that up.

V/r,

^ _ ^

PS.  I don't like MVCs, either.  Complexity for the sake of being complex.

Homestar9
Homestar9Author
Inspiring
May 29, 2018

I feel your pain WolfShade.  Having to jump into an existing project with 3500+ files sounds like a nightmare.  Even if the old coder documented everything and left verbose comments it would still be a daunting task to start modifying a monstrosity like that.

I've noticed that many OOP and DDD approaches to development tend to lean more towards "creating more classes" as opposed to "creating more methods/functions".  This generally leads to creating more CFC files and increasing the total number of files in a project.

I personally don't use interfaces, but in other languages, they are highly encouraged and could easily double the number of files a project uses if you had one or more interfaces for every class/CFC.   

There's a podcast that I listen to regularly called Coding Blocks​ and in a past episode they discussed whether having too many files in a project is a problem or not.  They also had an interesting comparison of OOP vs procedural programming techniques.  One of the hosts made an interesting point that the job of the IDE is to manage your files for you so as long as they are properly organized it shouldn't really matter how many files there are.  My main takeaway from the segment was that writing OOP tends to lead towards creating more files with fewer lines in each file, whereas procedural programming will leave you with fewer files but with more lines of code per file.

I'm still on the journey of discovering how to develop with an OOP mindset in CFML and I'm also relatively new to the idea of MVC.  Hopefully after I hammer out a few projects I'll have a clearer idea of how everything works in comparison to how I used to build apps.

Thanks for your input on the subject!