21
After reading a bit of Robert C. Martin’s Clean Code, he says that it is no use to "make up" a bad code with comments, in other words, try to explain a gambit made there.
It is also quoted about explain what you want to do in your code with your code and don’t use comments for it.; followed by the example that this:
if (employee.isEligibleForFullBenefits())
is better than this:
// Verifica se o empregado é elegível aos benefícios
if ((employee.flags & HOURLY_FLAG)
&& (employee.age > 65))
In addition to this answer, where it is written as write code clean and easy to maintain, the code below:
public bool ValidacaoCPF(int[] cpf)
{
int DV1 = 0, DV2 = 0, aux = 10;
if (!cpf.All(x => x == cpf.First())) //Verifica se todos os números são iguais
if (cpf[9] == ((DV1 = (cpf.Take(9).Sum(x => (x * aux--)) % 11)) < 2 ? 0 : 11 - DV1)) //Cálculo do primeiro digito verificador
return cpf[10] == (((aux = 11) * 0) + (DV2 = (cpf.Take(10).Sum(x => (x * aux--)) % 11)) < 2 ? 0 : 11 - DV2); //Cálculo do segundo digito verificador
return false;
}
Became that:
if (!cpf.All(x => x == cpf.First())) //Verifica se todos os números são iguais
//Cálculo do primeiro digito verificador
if (cpf[9] == (
(DV1 = (
cpf.Take(9).Sum(x => (x * aux--)) % 11
)) < 2 ? 0 : 11 - DV1
))
{
//Cálculo do segundo digito verificador
aux = 11;
return cpf[10] == (
(DV2 = ( cpf.Take(10).Sum(x => (x * aux--)) % 11 )) < 2 ?
0 :
11 - DV2);
}
As far as I’m concerned, you’ve had almost no significant change, you’re just trying to explain something you wouldn’t understand if you didn’t have those comments there. Right then, it fits into a dirty code for me.
In other words, comments were used to explain something that we would understand now only by the existence of them there.
In short, do comments really dirty the code instead of cleaning it? Where should we use them and when should we avoid them?
Information taken from page 86 and 87 of the aforementioned book.
Comments are very useful for documentation, especially in weakly typed languages, for example, you can better explain what such a method is for and what it needs
– Costamilam
One of the points he makes about the comments is that they tend not to be updated with the code changes, which makes them liars and mislead the reader. This would be one more factor to make only the really necessary comments and not go out commenting all that is line.
– Isac