In addition to what has already been covered in the existing answer, there are also those code snippets that serve as an example of how to use a certain function.
Example:
/*
Usage sample 1:
$rs = Foo(123);
return array(1, 2, 3);
Usage sample 2:
$c = Bar::Method('any');
$rs = Foo(123, $c);
return array(1, 2, 3, 'Bar' => (stdclass))
*/
function foo($arg1, $arg2 = null, $arg3 = null, $arg4 = null)
{
// aqui tem as rotinas, as firulinhas, etc.
}
This is also a very old habit from the time when "nobody" documented anything. Everything was documented in the code itself.
The ideal is to do everything organized with documentation on how to use routines and what they serve. As for removed codes it is recommended to have a versioning control.
There are still people who refuse to organize in this way and insist on "documenting" the code itself claiming that it is more practical. They don’t normally use versioning either. When you change something, you save a backup and arrange it manually. The problem is it’s out of order. When you need to work in teams, hire someone new, etc, you need to train that person to fit that unknown pattern globally.
A major problem in "documenting the code itself" is that it is very common for even the original code creator himself to not remember more than he did and remake something that already exists. Creates a new function and there is already a function that does what you want. But since there’s no documentation and it’s all inside a jumble of code out of tens of thousands of files, the guy will never remember because he doesn’t even have a tool that organizes the documentation. It has everything well described but inside comments in the archives.
The ideal is to organize using widely accepted standards.
Analogy
For those who are floating, let’s make an analogy. Think of an engineer with responsibility to build a skyscraper. The guy does not document using the engineering standard and decides to document writing on the walls of the building the way he thinks best. The building may not fall, but it will be a terror for maintenance. The building will last 100, 200, 400 years. Other engineers will continue maintenance and need documentation. But they will have to read the "comments" engraved on the floor, ceiling, pipes, walls, etc. And no guarantee that they are understanding correctly.
PHP Documentation
It is important not to confuse it with those commented codes under a certain pattern. If you see something like this it is some standard documentation:
/**
* This is the description for the class below.
*
* @package my-package
* @subpackage my-subpackage
* @author my-name
* @version my-version
* ...
*/
phpDocumentator, Doxygen, among others.
See more on SO-en: https://stackoverflow.com/questions/1182781/how-do-you-document-your-php-functions-and-classes-inline
The example here is about PHP due to the tag you put in the question, but the logic is the same for any project even outside the computer area.
Not everything is perfect
Of course, having versioning and organized documentation does not mean anything if it is poorly described. It is also common to pick up projects where versions do not have clear descriptions. They are usually vague things that make it more confusing. And in the documentation also vague things, no examples of use, no explanations of relevant details.
Example 1, negligence:
function foo()
{
// um codigo pequeno de 2 linhas
}
They usually ignore small codes and don’t detail what it’s for. It records in the documentation but doesn’t explain what this crazy business does. Sometimes it’s even garbage you forgot to remove but you just sit around.
Example 2, sloppiness:
function foo()
{
// um codigo imenso cheio de coisas sinistras e obscuras
}
Go to the documentation and find a generic description:
"This function is responsible for bringing the result of the request and
returns an array"
So you’re like, "Yeah, so what? How do you use that shit*?".
Even worse when you read the versioning, in the descriptions, in practically everything is like this: "Added improvements".
This way is even worse than doing "the old way" describing in the code itself in comment form.
What is right is to be very clear and descriptive.
Ethics
An observance that few care about is not misjudging someone else’s work because you don’t know the conditions, causes, reasons, reasons and circumstances that the previous programmer had to do X or Y.
For example, often the programmer is forced to do something he does not want to do and must obey because he is a subordinate. Another situation is the rush to do something urgent. The boss demands that the guy complete a drastic change in the system within an unfeasible timeframe where he won’t even have time to test, let alone document. So you stick it all in the codes anyway and leave it to me to sort it out when you get a chance. Of course the day to organize never comes and it stays there even clutching with other situations also urgent.
Therefore, when you take a project that another professional left, avoid speaking badly because you may be harming the person unfairly. The criticism you leave for later when you understand what and why it was that messed up way. And sometimes when you criticize without knowing the reasons, you end up burning yourself. Do not misjudge because not all programmers have the privilege to be able to work in a fully organized environment.
"I’m learning PHP and I use scripts ready to study them...", don’t you understand them better when they’re commented on? Doesn’t it help to have an explanation about what a particular block does? That’s why
– Miguel
@Miguel I think he meant that there are blocks of code within comments. Something like:
/* funcao($parametro) { return $parametro; } */
– Jéf Bueno
haa @jbueno, understood, so the question just got better
– Miguel
@jbueno Exactly.
– Marcos