Sei interessato ai nostri servizi di consulenza?

1 Clicca nella sezione contatti
2 Compila il form
3 Ti ricontattiamo

Se hai bisogno urgente del nostro intervento puoi contattarci al numero 370 148 9430

RENOR & Partners

I nostri orari
Lun-Ven 9:00AM - 18:PM

First the comments, then the code

by Simone Renzi / August 18, 2022
Post Image

This post is also available in: Italiano (Italian)

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

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

PHP example with comments

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

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.”

 

Simone Renzi
Seguimi

Scegli un'area

CONTATTACI

Ti risponderemo entro 24 ore

TORNA SU