Rus Articles Journal

What has to be a code the Internet - pages? Readability vs size. Part 2. We told to PHP

In the first part of article about florets, i.e. about html a code. And now give about berries - php a code!

Let`s say you write a script for php and write it, not especially caring for readability. Yes, you write it till some hours day by day, by heart remember that means this or that fragment that is meant by this or that variable. But here you wrote to

a script. And all! And then wanted to edit it. For example, a mistake in a code which you did not notice earlier.

you open it and see, for example, it:

of if ($a == $b & & ($c> = $d || $q!= $p)) { $s=w ($a); echo `Result: `. x ($s); } you something understand

? You can tell what does this fragment of a code? In what sense? Of course, no! But it is only one line of a code and the script can borrow both 10, and 20, and 50 lines and what then? To sit in the afternoon and to remember that variable $a and $b mean and to look for where you described functions w () and x ()?

Is good, and now look at such here piece of a code:

of if ($result == $znachenie & & ($max> = $min || $otvet!= $error))


of $OKresult=obrabotka ($result); / / Processing of result

of echo `Result: `. Preobr ($OKresult); / / Transformation of data to a clear format.


Agree, in the last code even the person who is not understanding programming already understands something!

Though optimizers of the websites, probably, on me will attack with angry curses now: a pier, not I accustom to that! So and the page will weigh as an elephant! As a result both loading longer, and a traffic will be and more in general! The size - the better is less! in principle they are right

, the first piece of a code weighs 71 bytes, and the second 223 bytes

Yes, unfortunately, with an arithmetic growth of readability the weight of pages, but there`s nothing to be done geometrically grows. I specially checked. I removed all excess gaps, transitions to a new line etc. on one them the pages. And the page decreased in a size of 6, 7 times! It provided that I to variables did not change names for shorter in one - two symbols, and it would be so possible to reduce the size in 10 or more times!

Impresses? And why such victims, you ask? If you at least once wrote a long script, then you, probably, will understand me. If is not present, then now I will explain. Present to

, you write a script. You write as in the first option - with unclear names of variables, as much as possible reducing the weight of pages, not doing any excess gap, and you write everything nearly to one line. Yes, in a time of writing you remember, where and in what place at you what is written down what variable that means. And here you wrote

A in few months you want to correct a script and what you see?! You do not remember any more that this or that variable means what means this or that piece of a code. Moreover, it is not visible where begin and where cycles, conditions etc. come to an end. Also believe, you should spend at least half an hour for understanding the script! And if a script big and difficult, and you not the professional - at least an hour one and a half! And you, maybe, will manage and to bring changes in 10 minutes!

You want to spend every time so much time on recall that, where and when you wrote how all this works what it means also what with all this to do? If yes - that please, write everything as there is a wish. And if is not present, call the " variables; on - human ! Also do not hesitate of comments, they for the present did not do to anybody such catastrophic harm that to get rid of them. Believe when somewhere in a shestist lines of unreadable php a code is not obvious the mistake (i.e. when in general everything works, but not always as it is necessary), search of a similar mistake turns into hours-long hell!

you Remember a saying: penny-wise and pound-foolish ? Surprisingly but for a saytostroyeniye it is applicable too! Having been too lazy to spend 20 - 30 minutes for making a code readable, you will spend every time when editing same 20 - 30 (if no more) minutes only for understanding a code.

And now I want you to warn a little. I sometimes see also options, similar to these:

of if ($resultata_rabotu==$znachenie_iz_bufera_obmena & & ($maximal_chislo_polsovatela> = $minimum_chislo_reshenia || $otvet_vychislenia_vurashenia!=$error_vo_vrema_rassgeta)) / / Check of a condition such - depending on that - and at those - circumstances.


of $good_resultat_rabory = obrabotka_resyltata_vyshiskenia ($resultata_rabotu); / / Function of processing of result which at first does - and then this and still it.

of echo `Result: `. Preobrazovanie_resultata_v_drygoi_format ($good_resultat_rabory); / / Function of transformation of data to a clear format which will transform data from it - a format in such - for the best understanding of result the person.

Well everything is good

, but moderately! Comments are not compositions - reasonings, and names of variables are names, but not phrases. Excesses are not necessary, try to write clear, but it is not necessary to comment on obvious things and to write kilometer names of variables. It too!

So, we will sum up the result. It is not necessary to pursue the minimum weight of the page at all, fighting for each byte to the detriment of readability of a code. The internal code has to be such that at desire it was possible to understand it easily and quickly! But also you should not write and paint each trifle.

Of course, all people different and to someone to understand the principle of work of this or that script, comments are not necessary at all - he and so understands everything, and not to do someone without them! Everything is relative. Of course, there are no special criteria of how to write a code. Write a code so that you understood it and understood it quickly enough, but not so: works, and weight small, and the fact that nothing also all is clear in one line well so costs of production! . It is in advance failure approach, you as a result will lose more, than won - penny-wise and pound-foolish!