Reputation: 144
I was reading a book named 'The clean code' and got stuck when the author was trying to tell about how can we write an efficient function.He says, "In order to make sure our functions are doing “one thing,” we need to make sure that the statements within our function are all at the same level of abstraction". So what the author exactly wants to convey by the levels of abstraction?
Upvotes: 3
Views: 335
Reputation: 14868
You can think of a function as the computational realization of some ability or behavior.
For instance, if you are modeling a lamp that you can turn ON or OFF, you could capture these capabilities in two functions, switchON()
and switchOFF()
. Or you could prefer to realize the same abilities using a single function switch(b)
with a formal parameter b
, etc. While pondering these considerations, your very notion of lamp is significantly simplified in that you will be only interested in some aspects of a real lamp: state (ON/OFF), color, etc. Depending on the role the lamp will play in relation with other objects, you will include some characteristics and ignore many others (e.g., price, weight, etc.).
The scope that defines your model, i.e., the features that you will pay close attention to, constitute your abstraction of the lamp. You can think of an abstraction as a caricature, a version of the actual thing (or concept) sharing as few attributes with the original as to resemble it, not as many as possible as to reproduce it completely.
So, what's a level of abstraction then? It is the set of attributes and behaviors that represent a certain trait owned by the model: one that is relevant to achieve some goal.
For instance, if your goal is to model a traffic light, your lamp will only need to be able to switch ON and OFF and to have a color within GREEN, RED, YELLOW.
Now assume you want to model a dynamic traffic system to optimize the flow of vehicles in a city. Your abstractions will have to include streets, crossroads, avenues, etc. As you build your model, you will be switching between different levels: the lamp, the traffic light, the green wave, rush hours, holidays, weekends.
The idea the author was trying to convey is that it would be bad for a function of the controller such as circumvent(accident)
to include a line of code dealing with the implementation of switch(b)
, because such a line of code belongs in a different level of detail, i.e., in a different abstraction layer, the one that fits within the realization of the lamp. Similarly, it would be bad for the body of circumvent(accident)
to make reference at some SELECT
clause of the database where the information is stored, because such a line of code only fits well at the data repository level (which knows very little about the accident
).
So, when designing a program or application, pay special attention to recognize its different strata. And then, when writing a function, make sure its code belongs in only one of your abstractions.
Upvotes: 4
Reputation: 14550
same level of abstraction:
function start_a_war() {
(targets_to_destroy, targets_to_occupy) = select_targets()
launch_rockets_at(targets_to_destroy)
send_occupation_forces(targets_to_occupy)
}
different levels of abstraction:
function get_diploma() {
select_industry()
select_university()
study_and_pass_all_exams()
go_to_administration()
if (secretary_is_sexy()) {
smile()
}
say('hi, i'm john doe, i've just passed all exams and i want my diploma')
put_right_hand_into_the_pocket()
grab_your_id()
while(id_not_visible_to_the_secretary) {
raise_right_hand_by(10 cm)
}
...
}
inside a function, you should always keep the same level of generalisation (abstraction)
Upvotes: 2