It's a discussion about follow very strictly the SRP (Single Responsability Principle) vs. be more flexible when you write simple codes, like setting or getting properties values, even out of a direct context.
My first doubt is about where SRP should be included. I always read about SRP should be applied on classes. But and about methods, properties or traits (as in PHP), ...?
If this answer is no, maybe we can discard all text below.
To exemplify, let's define an example case. To be more easy understand, we'll write a singleton structure for a class called Example
. Disregard IoC, for this case.
It should have two methods, basically:
getInstance()
: returns an unique instance of your class; andrebuildInstance()
: it'll only destroy current instance and recreate it;
Basically to it works, getInstance()
should check if the internal instance was set, create it if not, then return it. And rebuildInstance()
should destroy current instance if it was defined and create a new, without return nothing.
Let's turn it more visual (in some pseudo-language):
class Example
{
static private Example instance;
static public Example getInstance()
{
if (!this.instance)
{
this.instance = new Example;
}
return this.instance;
}
static public void rebuildInstance()
{
if (this.instance)
{
destroy(this.instance);
}
this.instance = new Example;
}
}
So note that:
getInstance()
have two responsabilities: 1. check and instantiate; 2. return.rebuildInstance()
have two responsabilities: 1. check and destroy; 2. instantiate;
You will see that both should instantiate, so let consider it as an effect duplication. To avoid that, I should decouple my methods to each one have a single responsability, then I need creates two other methods:
buildInstanceWhenNeed()
;destroyInstanceWhenNeed();
It'll turn more clear what each methods does by name and by its structure. So our code will seems like:
class Example
{
static private Example instance;
static private void buildInstanceWhenNeed()
{
if (!this.instance)
{
this.instance = new Example;
}
}
static public Example getInstance()
{
self.buildInstanceWhenNeed();
return this.instance;
}
static private void destroyInstanceWhenNeed()
{
if (this.instance)
{
destroy(this.instance);
}
}
static public void rebuildInstance()
{
self.destroyInstanceWhenNeed();
self.buildInstanceWhenNeed();
}
}
Okay.
Seems that now I'm respecting SRP completely on methods, very strictly. Each method have a single responsability without get out the class responsability. Except by the fact it have a singleton pattern inside of it, but I told to disregard IoC usage on start of topic.
So now I have additional questions:
- Should I apply SRP on methods, decoupling it, to make sure that it have a single responsability on each method?
- If yes. Should I do it even when the method don't duplicate any effect? For instance: if
rebuildInstance()
doesn't exists here, then I don't need decouplegetInstance()
because is the unique instance that will, in fact, create a new instance of class. So in this case, do I still need createbuildInstanceWhenNeed()
or not? - If yes, but lower priority. And about performance? I mean, when it really affect the runtime, decreasing considerably the speed of application because of ultra decoupled methods (which perhaps only affects interpreted languages), but with high specifications to each one.