How to use the 9th rule of Object Calisthenics in PHP?

Asked

Viewed 231 times

8

Object Calisthenics making a translation into Portuguese means "programming exercises", and consists of 9 rules created by Jeff Bay in his book The Thoughtworks Anthology.

The Object Calisthenics is a series of good practices and programming rules that were created by the Java developer community. My doubt takes into account only the 9th rule and, in short, says that:

  1. No Getters/Setters/Properties: the use of the access methods should only be used to obtain the state of an object, as long as you do not use the result to make decisions outside the object. For, any decision based entirely on the state of an object must be made within the object itself. The use of Getters/Setters/Properties also violate the open/closed principle of SOLID

This whole concept was based on here.

What made me a little confused is that using access methods is considered good programming practices. I would like more information and practical example, if possible, of how I can implement the 9°.

1 answer

9


Introduction on good practice

Where are you saying that use getters and setters is good programming practice? Who said? What is the reliability and relevance of this? And even if you have something like this, what does good practice mean? Nothing!

It’s usually just the opinion of someone telling you what to do in something they don’t even know you’re working with. So the first thing you should do is to abandon this idea of good programming practices or whatever.

Learn how things work, dig in, learn what each mechanism, technique, concept, methodology, etc. looks like in every detail. Know the basics. Understand why someone says something. What is behind it. Does it speak with propriety? Does she only want to sell books, lectures, courses, consultancies, etc.? Is she biased? Which is not all bad, but you need to know who she is to better evaluate what is being said. Is she speaking within a specific context? What does it say solves one problem and causes another? There it explains in depth what the reason for that?

We need to have a critical thinking of everything we receive, we cannot take anything as absolute truth. What you read here on the site is rated by several people, but it doesn’t mean it’s right or best for you.

Good practice only serves as a tip to study more on the subject.

How to deal with this problem

I have three views on the subject.

My favorite one is: if you have no advantage in having these methods to create them?

In PHP it is common not to have many advantages because it is a language of script, then linking your use is done on time always, scripts They are never absurdly complex systems. In other languages I would think what to do, in PHP I would almost always use the direct attribute until I had a reason to change. It’s simpler, it’s more efficient, but for PHP, efficiency doesn’t matter. When I need to change I can make use of magical methods.

I know some people use PHP as a language Nterprise nowadays. I think this is a mistake, but then it makes sense to use the mechanism, it just doesn’t make sense the decision by the tool.

The other view is that you need to take precaution in having the method already available if one day you need to include a processing in the state access or mutation in the class. I think valid, but I don’t like it much.

It is more valid in Java, for example, where the link occurs in the compilation. This seems to be the one you learned. Many people think this is encapsulation, it’s a way, but not all of it.

There are cases where you have to do this to virtualize access to the field, which is fundamental for a certain mechanism to work. Very common among the Orms, which I even criticize a little this abuse. Nor did I mention the anemic classes that work essentially with the state.

The last view is the strictest object-oriented view where it says each method must have a defined function, must do something of its own, must have an operation that actually manipulates the object and not a specific point of the object. If one wants to be "as sure" as possible, then one should follow this.

Conceptually the quote posted in question is correct, at least in part, methods to use getters and setters is the easy way to let the object be modified or accessed.

There it says:

you must say for the class to do something, should not ask for for her to do something for you

This means that you should not use mechanisms but abstractions. Say what you want, don’t say how you want.

A part is wrong because if the field should only be modified or accessed by the object itself then it has no reason to have getters and setters, do everything on the object itself. There will be no external links, refactoring something internal is always very easy. So I wonder if whoever wrote this thought this through.

Back to the blind rules

Finally, I am against almost all of these rules, especially as they are golden rules to be followed blindly. There is a rule that comes down to being completely absurd, if someone follows will have a monster on hand. I am not against knowing them, I am against not questioning them.

Martelo dourado batendo em um parafuso

Completion

I recently replied about this and put examples in several responses. Read all about it. Understand why technically and conceptually these methods need to exist, and why they can be avoided by pragmatists.

Do not let the balance be changed, do an operation that changes the balance as a result. Do not allow changing the name of the person, allow it to have the name authorized to change by court decision, or that has a contractual change. Determine an action that causes the attribute to be modified.

Is that precious? Can it create more complications than solutions? Does it swell code? Create new maintenance management needs? Yes. But it can also be the best solution in specific cases.

The secret is dosing well, and this is only done when you don’t follow rules, but you learn why things exist. Not all abuse is necessarily bad.

A real example with real requirements would make you think about how to correctly implement something. Abstractly it’s just the idea.

This Object Calisthenics specifically I find one of the biggest nonsense that has ever appeared on the Internet, I wonder if it was not a Hoax that went too far.

  • I confess it was one of the most enlightening readings I’ve ever had about getters e setters. Not only the publication but also the links. I confess that the only concept I had about access methods were opinions of others saying that it is best to use. But thanks to your reply I changed my mind. Opened my mind to the questions. Thank you for the great explanation.

  • Great, you don’t know how happy I am.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.