Orientation in code

This article describes general methods for clarifying longer code. It is useful for easy orientation for you and other people who will be forced to read it. I will also mention unwritten rules that should be followed.

All these methods and procedures are for programmers and their convenience. The computer is a machine, he doesn't mind the confusing, unformatted long code.

Comment

Comment has no effect at script at all. It will be ignored during processing. No visitor to your site will be able to see it either. It's purely for you.

$a = 5; // variable note, only 1 line 

// internal note, only 1 line

/* this note can be arbitrarily long
to 2 lines, 
 
4 lines, even more */

################

Line and graphic separation

It is good to separate the related parts of the program into logical blocks. Then it is easier to find, add, edit, ... because you already know where they are.

$a = 5; 
$b = 3; 
$c = 15; 
################ 
$d = $c-$b+$a; 
$e = $c+$b+$a; 
################ 
echo $d; 
echo $c; 

The demonstration clearly shows 3 different blocks that do a similar activity and are more similar than others. The whole block section can be separated by a line or grid.

Documentation comments

For longer scripts where it borders between script and program, it is good to write something like "Documentary commentary".

Example:

/** 
* @author      Name (email) 
* @copyright   Name and Date 
* @license     URL name (license) 
* @link        URL address 
* @var         Type description (variables or properties) 
* @package     Name (package) 
*/ 

And more, there are over 20 ...

It is not normalized anywhere and is not mandatory. But this is an unwritten rule and it is nice to follow it. Especially when you distribute your source code to other people.

Use English in comments

If you don't write the code for yourself, it's best to write everything in English (especially variable names). English is a worldwide language and is expected to be known to every developer.

Order, multiplication and division

In mathematical operations, always think that it is better to multiply first and then divide (there will be less rounding). It may happen that there is a decimal number between the calculations and then the whole result may be wrong (PHP naturally rounds to 11 decimal places).

echo 10/3; 

This example prints 3.33333333333 . Now let me show you a specific sign example:

echo 1 / 2 * 2; // this is worse because 1/2 = 0.5*2 = 1
echo 2 * 1 / 2; // this is better because 2*1 = 2/2 = 1

In both cases there are intermediate calculations, if it were larger numbers, then it is possible that a decimal number will come out and then both results will be different. You must try to write the signs so that the positive number comes out or as close as possible to the positive number.

If you're a familiar developer and aren't afraid of complex mathematics, you can count in fractions. Fractions are fine, accurate ... but complex and slow.

Previous chapter (Types of Cycles) Next chapter (Data Sending Methods)

Back to Learn PHP main menu