First to be objective I have to suggest something subjective.
Teamwork
You say "make it easy for anyone to touch the code after". That "who" is very important. As much as you have a correct way of dealing with the subject technically one aspect cannot be overlooked. Politically you have to take into consideration specifically who is this "who".
Following what your team expects in many cases is more important than following rules, especially the blind rules. How do you expect a code to be easy to maintain if everyone who will probably touch it does not expect and possibly do not understand what you have written, no matter how correct it is?
It gets worse if you don’t even know who these people are, IE, when you are thinking of future programmers who replace you in software development.
Functional style X imperative style
Looking at your code, he has a problem for most programmers. I would guess that 90% of programmers who might come to maintain their software don’t quite understand the functional style of programming. Even those who understand the use of collection extension methods for queries do not fully understand their implications (Lazy Evaluation and side effects for example). And what will happen when this programmer is going to mess with your code? He will probably abandon his and write his own version.
You can argue that it is the programmer’s obligation to know how to use all language resources. And I’d be with you on this. I’ve even resigned from a job because I was required to make such simplistic codes for the most beginner intern in programming. I wasn’t politically correct, I just made a choice for my life.
The common programmer better understands imperative style, he understands how the loop works. It is no use saying that your code is obviously taking 9 elements from a collection and applying a formula to each of these elements and adding up all the results. It’s obvious to me how this works, but it’s not for many people. Maybe the programmer can even understand, but doesn’t know how to change it to achieve a different result that maintenance requires. We realize this happening right here website all the time.
And imagine in a more complex example. Your example is simple and not so hard to understand. I’m considering that it’s just a short example rightly posted like this, but the problem will actually appear in more complicated situations.
In some cases it can be difficult to make the functional style properly since we do not always know the implementation. In imperative style an inefficiency would be more obvious.
I know this example doesn’t need performance, but the programmer will have difficulty improving more complex code by not fully understanding what is happening in the code.
The intern
So I’ll tell you that depending on the possible scenario of who is going to maintain this code is difficult maintainability because it uses resources that few people are familiar enough.
I myself would not follow this rule of the lowest common denominator, but this is my decision and I am willing to bear the consequences of it.
Variable names
By making a specific analysis in your code (only in the parts that matter most), I say that your variables use good names.
Yes, variables must have significant names. And yours do. Because auxiliar
is more readable than aux
or because primeiroDigitoVerificador
is better than dv1
? (I used lowercase simply because this is the convention adopted in language)
Who said to use contadorDasVezesQueAlgumaCoisaOcorreEmUmLaço
is better than i
. Too long? I did what the "rule" says! But even if I use only contador
it does not help understanding more than i
in a loop.
Of course the further away from your declaration a variable will be used, the more the name needs to be meaningful. But when it will only be used in limited scope, the name is not so important. Of course, don’t use random names (you knew a programmer who used swear words in all variables). You dosed well.
Using a meaningful name can be useful for semantics to a chunk of code and avoid commenting, but it’s not always that important. Comments should be avoided, but you have to analyze the context, it’s more wrong to follow a rule blindly.
It is not curious that virtually every example of teaching code lambda uses x
how argument? Examples written by geniuses of programming! Don’t they know the books that say variables should have significant names? Or do they just know how to use the rule the right way and not blindly?
If you are going to analyze it well, in your example if the argument was called elemento
or item
would be more descriptive, but everyone uses x
and no one who knows the mechanism used fails to understand that there is an element of the collection.
The lesson here is to be careful with established rules. Whether they are spoken by strangers on the Internet or by established computer authors.
Even the best most popular books have been written in contexts that most people are unaware of, are usually vague when something should be used (and it has to be, there are so many scenarios) and they don’t usually explain how to reach that conclusion (when explaining, does not emphasize this, emphasizes the simplified rule that everyone will keep). So a book that seems very good and well-intentioned ends up, by failure of readers, leading people to commit barbarities, where the intention was opposite. And worse, almost no one notices this happening. They start using the book as a crutch for their atrocities.
For me, one of the most important principles in coding is the Don’t Repeat Yoursef. He preaches that, simply, codes should not be repeated.
In your code there is repetition. An excerpt can be abstracted into a function and eliminate repetition.
When you have code repetition, it’s harder to maintain. The amendment at one point does not ensure that the other point is amended in the same way.
Of course the chances of error in the example posted are not great. But they exist.
This is similar to the comment problem. It is common for the comment to be repetitive, even when it does not appear to be. When you change a code and do not change the comment to reflect the change, potentially you may have dissonance. Remember this is not a rule, I’m not saying I shouldn’t use comment.
Note that abstracting in function the repeated code in question will not make the code more concise, but will make it have its unique representation. DRY is not to make code concise. Concision does not always help maintenance, having a single point to change an algorithm helps maintenance. As long as creating this abstraction is no more complicated than leaving the code inline.
I admit that I exaggerate about DRY, but ignoring it has always brought me more maintenance problems.
Also do not follow DRY blindly.
Tips
(are not rules)
- ask others to analyze your code;
- try to understand your old programs;
- try reading your code without the help of editors who "embellish" the code;
- know all the coding rules and learn how nay use them (how to use is easy).
References
Some of the books that talk the most about the subject are Clean Code and Code Complete. The Portuguese version is not so good.
I was a little reluctant to put them on because they’re a little dogmatic, they try to sell ideas that don’t work that well in all situations, they sell ideas that seem absolute when they’re not, and they can create a trend for those who are not experienced and think that the book works like a manual. If you don’t know how to use the tips that books give, risks becoming dogmatic too.
The first sees a very specific way of developing, practically obliges to choose certain tools (not software but paradigms, methodologies and techniques) for everything. If you know how to take only what is most important to apply in their situations can take advantage of your content.
The second is a little broader in the considerations but tries to be too specific in some cases and does not contextualize well. It is common to give tips that work well in one language and not in another, but it does not deal with this.
Concluding remarks
You still have a problem of readability by other factors, mainly because of the ternary (I love this operator) it was hard to read the code. It seems that you were so happy that you managed to do everything in one line, that you forgot the readability. Or not, after all you are here asking her :)
Keeping many lines of code can affect maintenance as well. Concision well thought out, and not artificial, is a good thing too. Just can’t overdo it.
Not always when a person cannot understand your code means that it is unreadable, may be that you are dealing with a "parachutist", increasingly common in our midst.
I put some naming conventions in that reply.
And I try define what is clean code. And also elegant code.
Your question is whether you should encapsulate these 3 commented codes in functions with a relevant name and call these functions instead of putting the code in line and comment on what he does (as you did)?
– Maniero
@bigown Not my doubt how to know that a code you are writing will be easy to maintain for the next programmer, sometimes we programmers get excited and do things that were to become giant in a few lines (but the next one to get our code, can spend a lot of time to understand 100% of the logic we use). I wanted to know how abstraction is good and feasible from a code maintainability point of view.
– Laerte
I don’t know if it’s easy to answer that without resorting to an opinion, but it’s a good try. What I can say is that most programmers are usually more comfortable with more imperative style, separating the steps further. I already think that big code is hard to follow. But I know that I am minority.
– Maniero
@Bigown I asked the question based on a Programers, I believe it is possible to get an answer without going to the side of opinion.
– Laerte
There the answers help little, interestingly have better answer c/zero votes the most voted are anti-respostas. Here also did not start very well, passing the tangent, but I hope that still comes something good.
– Maniero
I just wanted to leave my thanks to this issue. Regards
– Francisco Maria Calisto