Questo sito utilizza cookie per migliorare la tua esperienza. Puoi scegliere quali categorie autorizzare. Privacy Policy

Preferenze Cookie

Scegli quali categorie di cookie autorizzare. I cookie necessari sono sempre attivi per garantire sicurezza e funzionalità di base.

Necessari Sempre attivi
Cookie essenziali per il funzionamento del sito, la sicurezza e la protezione anti-spam. Non possono essere disattivati.
Analitici
Cookie che ci permettono di capire come i visitatori utilizzano il sito, per migliorarne funzionalità e contenuti.
Marketing
Cookie utilizzati per mostrarti annunci pertinenti e misurare l'efficacia delle campagne pubblicitarie.

Telefono: 379 14 89 430

Orari: Monday to Friday, 9:00 a.m. to 6:00 p.m.

First the comments, then the code

blank

In this article I want to talk about a topic that is very close to my heart and that many developers seem to leave on the back burner. This topic represents not only an undeniable “best practice” in the way of developing code, but also a form of respect towards the community of developers and engineers who will have to put their hands back on code written by someone else: I am talking about code comments.

A very dear university professor of mine, Prof. Carlo Gaibisso, used to say this phrase in his lectures on “Structured Programming” in the C language: “You write the comments first and then the code.”

How can you blame them?

Despite the fact that programmers are a class of “stage animals” in the world of computer science, and despite the training a developer may have, it is a fact that it is always easier and more immediate to read something that belongs to our common way of communicating. It is much easier to read in Italian what an algorithm is about than to understand what an algorithm written in code does; it becomes the more complex to understand it the more complex the algorithm.

Object-oriented programming also incorporates procedural programming. Within the methods of a class, one programs procedurally.

Procedural programming is so called because code is executed according to a procedure. I can write procedures for anything… For example if I have to calculate the Fibonacci succession I know that to calculate the number at step n I have to take the previous number and add it to the still previous number.
Therefore the procedure will be: starting with S = 1 1, f3– > I take f2 I add it to f1, and I get

S = 1 1 2

I take f3 = 2 I add it to f2 = 1 and get

S = 1 1 2 3

I take f4 = 3 I add it to f2 = 2 and get

S = 1 1 2 3 5

You will understand that it is much easier to instantly understand the definition, “To calculate the nth number of the Fibonacci sequence I add the previous two” than fn = fn-1 + fn-2 especially if this formula is written with variables and cycles within a computer algorithm!

PHP example for calculating Fibonacci series without comments

[php]<br />
<?php<br />
class Fibonacci<br />
{<br />
public static function calculateSuccession($n)<br />
{<br />
$n = $n – 2;<br />
$a = 1;<br />
$b = 1;<br />
echo $a . “
”;<br / >
echo $b . “
”;<br / >
for ($i = 0; $i < $n; $i++) {<br />
$c = $a + $b;<br />
echo $c . “
”;<br / >
$a = $b;<br />
$b = $c;<br />
}<br />
}<br />
}</p>
<p>Fibonacci::calculateSuccession(1000);<br />
[/php]

PHP example with comments

[php]<br />
<?php</p>
<p>/**<br />
* Fibonacci is a class for calculating the Fibonacci succession<br />
* and its verification<br />
*<br />
* Fibonacci is a class for calculating the Fibonacci succession<br />
* The verification of the golden ratio through the relationship between successive terms<br />
* verification by tartaglia triangle and verification by<br />
* Cassini, Catalani and D’Ocagne’s equalities<br />
*<br />
* @author Simone Renzi<br />
* @version 1.0<br />
* @access public<br />
* @see http://renor.it<br />
*<br />
**/<br />
class Fibonacci<br />
{<br />
/**<br />
* @method calculateSuccession – Static method to calculate succession<br />
* of Fibonacci for $n terms<br />
* @param int $n – The number of iterations<br />
* @return void has no return values, it just prints the values<br />
* of the series at each iteration<br />
* @access public<br />
**/</p>
<p> public static function calculateSuccession($n)<br />
{<br />
//As I have two fixed parameters I remove them from iterations<br />
$n = $n – 2;<br />
$a = 1;<br />
$b = 1;<br />
//Print the two starting parameters<br />
echo $a . “
”;<br / >
echo $b . “
”;<br / >
//Cycle for $n<br />
for ($i = 0; $i < $n; $i++) {<br />
//Calculate the value of the subsequence at step $n<br />
$c = $a + $b;<br />
//Print on screen the value<br />
echo $c . “
”;<br / >
//move variable values<br />
//f2 becomes f1<br />
$a = $b;<br />
//f3 becomes f2, the next iteration will calculate the new f3<br />
$b = $c;<br />
}<br />
}<br />
}</p>
<p>Fibonacci::calculateSuccession(1000);<br />
[/php]

I can continue to apply the procedure of taking the previous two numbers to infinity. Obviously computing an infinite set of numbers takes an infinite amount of time so we opt to find the subsequence at term n where n is the number of iterations for which the algorithm will have to perform the procedure.

From the pictures we can see that writing commented code in a virtuous way requires many more lines and therefore more time but in case of maintenance everything will become extremely easier and faster. We invest a small part of our time before so that we do not have to waste whole days afterwards.

Procedures

I can apply a procedure in any context, even for cooking. A recipe is nothing but a procedure: take a saucepan, put 2 tablespoons of extra virgin olive oil, add a clove of garlic, a hot pepper, turn on the stove and simmer, etc.

Procedures are inherent within functions and can be combined to solve larger problems. Returning to the Fibonacci example, another algorithm might verify that the ratio between two consecutive numbers in the Fibonacci series approximates as n increases more and more the golden ratio. A further function could verify that the obtained succession of n terms considering the of the terms on each diagonal of the tartaglia triangle corresponds to the Fibonacci succession, etc.

On par I could say that the previous procedure useful for preparing the soffritto should be carried out on par with the procedure for cooking the pasta.

As you may have realized, a procedure is best and most quickly understood if there are comments describing the steps.

It is usual to insert comments first and then code because this practice allows us to write code very quickly without forgetting anything and leaving other programmers who will have to integrate other functions to quickly understand the famous “what is needed for what.”

 

[starbox]

Potrebbero interessarti anche

Custom Web App: How to Choose the Right Company in Italy for Your Project
Software Development & Programming

Tuesday, 08 September 2026

Custom Web App: How to Choose the Right Company in Italy for Your Project

What is a custom web app (and why off-the-shelf software isn't enough)A custom web app is a browser-based application built s...

Pubblicato da Simone Renzi

Custom ERP: How to Build a Management System Tailored to Your Company
Software Development & Programming
Software Development & Programming

Monday, 07 September 2026

Custom ERP: How to Build a Management System Tailored to Your Company

Off-the-shelf ERP or custom management software: what's the differenceAn ERP (Enterprise Resource Planning) is a software sys...

Pubblicato da Simone Renzi

Custom CRM Development for SMEs: When It’s Worth It and What to Consider
Software Development & Programming

Sunday, 06 September 2026

Custom CRM Development for SMEs: When It’s Worth It and What to Consider

Custom CRM or off-the-shelf solution: what your SME really needsA CRM (Customer Relationship Management) is the tool with which a ...

Pubblicato da Simone Renzi

Software House for Custom ERP Solutions in Italy: Selection Guide
Software Development & Programming

Tuesday, 01 September 2026

Software House for Custom ERP Solutions in Italy: Selection Guide

Off-the-shelf or custom management software: when tailor-made solutions actually pay offAn off-the-shelf management solution is of...

Pubblicato da Simone Renzi

Custom Software Development for Companies: When It’s Worth It and How Much It Costs
Software Development & Programming

Monday, 31 August 2026

Custom Software Development for Companies: When It’s Worth It and How Much It Costs

Custom software or ready-made solution? How to figure out what you really need"Custom software" refers to an application...

Pubblicato da Simone Renzi

How much does it cost to develop a business mobile app? Price guide and key factors
Software Development & Programming

Monday, 13 July 2026

How much does it cost to develop a business mobile app? Price guide and key factors

Why the right question isn't "how much does it cost" but "what do you need"Asking how much a business app...

Pubblicato da Simone Renzi

Non hai tempo?
Chiedi al nostro assistente AI

RENOR & Partners S.r.l.

Ciao! 👋 Sono l'assistente AI. Come posso aiutarti oggi?
Scrivi il tuo messaggio...
Developed by RENOR & Partners - https://renor.it