📜 ⬆️ ⬇️

Why did the devil come up with javadoc

It is believed that a good program should be well documented.

SUN even came up with a special javadoc format - “a standard for documenting Java classes”. In my practice, it was a completely common occurrence when some code did not go through the Code Review, because some methods had no comments in it.

I argue that this approach is outdated, and today I will tell you why comments are evil.
')


Life example


This is a real code, written in 2010 by a quite diligent programmer who was not lazy and wrote a comment to his method. Satisfied with myself, but I went to pour myself a cup of coffee from the apparatus, but let us see for now what we have here.
public class AddressUtil {
/**
* Format string as address , expected input
* format:"EE ; ;Tallinn;Narva mnt;120B;831;10127"

*
* @param flatAddress
* @return Formatted address

*/
public static String toString(String flatAddress) {......}
}

Fine! We have a well-formed javadoc commentary from which a special program can generate HTML documentation. It is easy to understand from it what (theoretically) this method does.

Where is the devil hiding?


But where are the little things the devil hid in? And here they are:


Magic


And now let's play wizards and turn some colored pieces into a garland. Let's make some magical passes. Sim-salyabim, Akhalay-makhalay, Lasyki-masyaski ...
What we got in the end?
public class AddressUtil {
public static String formatAddress (String flatAddress) {......}

@Test public void testFormatAddress() {
assertEquals("Narva mnt 120B-831 10127 Tallinn",
AddressUtil.formatAddress("EE ; ;Tallinn;Narva mnt;120B;831;10127"));
}

}

How is the new version better than the old one?

In a word,


GOOD NAME + TESTS = DOCUMENTATION

or rather, “run documentation” (executable documentation), that is, documentation that can not only be read, but also “run”, automatically checking that it is still adequate.

They say that Confucius had a poster over his bed:

Convert comments to executable documentation

Afterword


I am only afraid that our valiant programmer, having returned from the kitchen, will not understand the focus, because he did not see our magical movements. He will demolish the tower from the mere fact that his comments are SOMETHING POTTER, and he will try to find us and destroy for such subversive activities. And his coffee in the meantime cool. Well, and that is not bad: after all, coffee, they say, is harmful. So, we did one good thing today.



UPDATE
In the comments a lot of indignations fell over the fact that it is inconvenient to use open-source libraries without documentation. This article is not about export code. It is about the “internal” code that you and your colleagues write, and you and your colleagues will be the only ones that you have to change several times. In the "export" code, everything is different - there is certainly a need for documentation, but it is necessary to monitor the state separately. You do not need to change it, but only to read. This is a completely different story.


Andrey Solntsev

Source: https://habr.com/ru/post/97320/


All Articles