Good practice
I’ll tell you what’s more important on this subject: they invented a plague on the developers' minds called "good practice".
The world of development will be much better when people stop talking about it. That’s one way of saying "do it the way I’m talking and don’t argue". The same goes for "bad practice" which is a "I’m saying it’s bad and bad". So you don’t have to explain anything. If it were good or bad, the person would explain. So the developer reading that can decide on their own if it’s good or bad and when to use it.
Almost everything that exists can be good or bad in most situations, but without understanding why, it serves for nothing. Everything has an exception, of course. So you said "good/bad practice," so get away from the answer. Either the person does not know what he is talking about or is biased (of course it may be just laziness/haste to answer, but how will you know?).
And what you’re calling attribute is actually called field.
int
as "attribute"
That answer linked specifically is far beyond the scope of her question and then falls into the problem of not making you understand what really mattered.
It is completely absurd to say that an "attribute"/field should not be int
. In this specific case I would say that there is not even a situation that prevents, creates difficulty, and especially is a "bad practice":). A field can be int
whenever necessary.
Of course he shouldn’t be int
if it is not necessary to be. But here is what I always say: the right is to do the right, not to do what is "good practice". And the right one is only known in the specific case. Every time you do the right thing somewhere and try to transpose it into another situation, you’re doing it wrong. Every situation is unique. Some coincidentally must have the same solution.
I think the answer meant that it’s not good to have an exposed public field and got confused in writing, that’s all.
Without going into too much detail because this is not the issue here, this is something that can be bad in many situations, especially in Java, and it is recommended to use a method getter/Setter for access to it. Anyway the phrase is very unfortunate. I am not going to discuss this here (but I disagree with what is said as absolute truth), the subject is different, but Linkei questions below that talk about the subject, if you need more information you can still ask a new specific question.
Your example
Your specific case is using the contador
as private. Ok. The problem with what you’re doing is that it creates a running condition in concurrent environments. You can have the counter increasing by 2 or more before being used in each one, then you will have two objects with the same index. It’s very rare to happen, which is why the day it happens will be very difficult to reproduce and find out why.
What you’re doing is not the best idea. I would try to do otherwise, but since I don’t know what the problem is, the requirements, anything I say will sound like "good practice".
###Getter/Setter
The indice
is public. It is no problem to be final
and public
. This in itself does not create any problem. It is usually better that your access is done by a method accessor (see in the questions below the why), but only usually, if there is no reason for it, can leave as public.
I agree that in Java, which is a language that does not like much of syntatic sugar, It is usually a good idea to access the fields by a method to ensure the best maintenance. If you need to change the form of access in the future you will not need to change all the codes that consumed your class. In other languages this can be more convenient and give more freedom, if the person knows what he is doing.
In Java, to expose a field publicly (any type of field) you must be sure that you will never have changes in the way you access it. It’s rare to be so sure. But there are cases.
The rest of what they talk about is a silly theory.
The ramaral response has an example of how to access with an auxiliary method. Note that you do not have a Setter, after all it would make no sense to have in a field marked as final
.
Completion
Never do anything because someone told you it’s right, learn because it’s right. Then you may find that the person who told you this may be wrong.
You were right to question here.
To speak the truth I think that none of the answers in these questions show well these questions in full, but reading everything already helps to understand and form a own opinion.
I think the utluiz just typed wrong in his answer. By the context I think he wanted to write "public attributes" but for whatever reason write "int attributes" and it passed beaten. There’s nothing wrong with int attributes.
– Victor Stafusa