📜 ⬆️ ⬇️

Causes and Effects

Here I want to share with you three examples of inadequate code. And at the same time I will try to disassemble and classify each case. Thus, I will tell not only “what is bad?”, But also “why?”

Lack of knowledge / experience.

Not so long ago, we had to revive a project that was once very popular in its circles, which occupied the first lines in search engines and left a tangible mark on the archive . Now he is quietly rolling "on neutral". After global processing and launching the project in full force, I will definitely write “how it was”. And now about that code which did not pass censorship.

The similar code was already published on habrakhabr . Short. By reference: selecting an identifier to insert a new item into the MySQL database table (the largest identifier is selected, one is added, a new record is inserted). The author of the masterpiece, which I want to present to you, went even further. ID is chosen randomly; attempts to insert a record do not stop until the pseudo-random number generator guesses the free ID.
The code looked like this:
$id = 0;
while (!$id || mysql_error()) {
$id = rand(1, 10000000);
mysql_query("INSERT INTO `table` (id) VALUES ('".$id."'");

Cause. Ignorance of the SQL features by the author (auto_increment in particular).
Council Study, study and study again. Read smart books, watch other people's codes, criticize your own code, ask for advice from more experienced programmers.


In the tenth grade in computer science class, I received a task to write a program that determines whether the entered number is a full square. The program was written and credited. Two years later, running my eyes over the code, I could not understand why it works. The algorithm consisted in comparing the root of the entered number and the particular entered number and its root (see the code below).

It looked like this:
if sqrt(x) = x/sqrt(x) then
writeln(x, ' - ')
writeln(x, ' - ');

Cause. I have not analyzed my code. From a mathematical point of view, the condition “sqrt (x) = x / sqrt (x)” is always fulfilled (for x> 0), and only due to the restriction of the discharge grid of operands the program performed its function (at the end of the fractional part an error occurred).
Council Understand your code. Make sure that the program runs the planned processes.


And finally, a terrible tale at night. The following code, unlike the ones above, was never written by anyone (I really hope so). Although its counterparts in other programming languages, it seems, there are actually. For example, #define TRUE FALSE (C, I guess?), Mentioned earlier . Or the notorious "dog" (@) in PHP.

push ss
mov ss, 01f7
; ...
pop ss

I will not assure you that this code is operational (I have been dealing with an assembler for a long time ... and not true). The general meaning that I wanted to pass: the stack segment address is placed on the stack, a random value is written to the stack segment address register, then some actions and the “recovery” of the stack segment address register from the stack. What happens next is unknown to anyone. Such cases threaten very long debugging.

Cause. Absolutely not thought out sequence of operations. Not taken into account the consequences of which may result in a harmless set of commands.
Council Control the indirect influence of the written code on what was written before or can be written in the future. Do not allow implicit restrictions imposed by the code on the program as a whole and its individual sections.

Everything written above can be found in any, a little bit literate, book on programming. And, perhaps, now, you will want to read one of them. Also, I hope, now, you will become more attentive to your code.

upd : Due to the appearance of a large number of defenders of "random" identifiers in the database tables, I will answer everyone at once: if you need to hide real IDs, you need to use mod_rewrite, and not distort the database.

Source: https://habr.com/ru/post/46196/

All Articles