What is spaghetti code?

45

When you read about software architecture on the web, sometimes the term spaghetti code is used, referring to something that should be avoided. >

But I never quite understood what "spaghetti code" means.

    
asked by 03.09.2014 в 14:35

4 answers

20

It is a code with flow complexity, usually in a constant run line, where execution jumps are mixed using unconditional deviation structures, such as GOTO , END , etc.

An example with spaghetti code in BASIC:

10 i = 0
20 i = i + 1
30 PRINT i; " squared = "; i * i
40 IF i >= 10 THEN GOTO 60
50 GOTO 20
60 PRINT "Program Completed."
70 END

That could be implemented in a more structured way like this:

10 FOR i = 1 TO 10
20     PRINT i; " squared = "; i * i
30 NEXT i
40 PRINT "Program Completed."
50 END

It is also used as a term for blending technologies when developing with scripting languages, such as PHP + HTML. Understand that PHP is used as a template in MVC architectures of systems where classes that generate output (Vision) need to write pure HTML and insert dynamic data into their generation. The term spaghetti code here would be used when the logic of the PHP program is inserted along with the UI output.

An example in PHP + HTML

<!DOCTYPE html>
<html>
<head><title>Macaronic Code</title></head>
<body>
    <?php
        $ip = $_SERVER['REMOTE_ADDR'];
        $dbh = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');
        $statement = $dbh->query("SELECT lastlog FROM logins WHERE ip = '$ip'");
        $row = $statement->fetch(PDO::FETCH_ASSOC);
        $dbh->query("INSERT INTO logins VALUES ('$ip', CURRENT_TIMESTAMP) ON DUPLICATE KEY UPDATE lastlog = CURRENT_TIMESTAMP");
    ?>
    <p>Hello, <?php print $ip; ?>.</p>
    <script>
        var lastlog = "<?php print $row['lastlog']?>";
        if (lastlog > "") alert("You last visited "+lastlog);
        else alert("You've never been here before.");
    </script>
</body>
</html>

This code would have a better architecture if the connection logic were in a class or connection function, separate from the HTML. The query logic of the database is also in a class or function. And there was a flow control routine that called the output generation, passing the data it should use, perhaps in the format of an array.

And then the HTML part would only use PHP as a template according to the example below:

<!DOCTYPE html>
<html>
<head><title>Better Code</title></head>
<body>
    <p>Hello, <?php echo $data['ip']; ?>.</p>
    <script>
        var lastlog = "<?php echo $data['lastlog']?>";
        if (lastlog > "") alert("You last visited "+lastlog);
        else alert("You've never been here before.");
    </script>
</body>
</html>

In the above example we could still improve this code by separating the JavaScript part and inserting it as an external file in a non-intrusive way.

The Wikipedia entry in English also describes the code lasagna, ravioli and macaroni.

The article Spaguetti Code describes techniques, strategies for refactoring a spaghetti code.

To understand some of the other concepts mentioned:

Sorry for the references in English, but they have a better content material than the ones I found in Portuguese.

    
04.09.2014 / 16:49
50

So the Master Programmer said: A well written program is your own paradise; a poorly written program is its own hell.

The Spaghetti code is the antithesis of the Zen code.

It is process noise tending to infinity when expressed in code.

It is the preferred solution of beginners, but not refined palates; it kills the hunger, but it does not bring satisfaction.

The master programmer, on the other hand, prefers its clean and lean code. He caresses the processor instead of torturing it.

The Zen code is universal; With a look you know exactly what it does.
The code Spaghetti is cryptic. It can not be intuited. You need to map it.

The code Zen is succinct. In a few lines it covers the whole process. The Spaghetti code repeats rhythmically, but is pretentious and misuses time.

The Zen code facilitates and embraces growth.
The Spaghetti code makes maintenance difficult, costly, and slow.

The Zen code is like water, adapting to new shapes effortlessly.
The Spaghetti tag, when corrected at one point, creates bugs in others.

The code Zen is a poem.
The Code Spaghetti is the CFO's three-hour speech.

Sources:
- The Tao of Programming
- Fundamental Laws of the Spaghetti Code
- Ensō

With @brasofilo collaborations . Thanks!

    
04.09.2014 в 15:51
18

Spaghetti code is code that has complicated control flow, abusing mechanisms like exceptions, unconditional deviations ( GOTO ), and similar constructs. This is because the code flowchart of this type has many lines crossing and going everywhere, actually remembering a spaghetti noodle.

The spaghetti code is a standard practice in BASIC , for example, and the language Pascal came about precisely to encourage the use of structured programming, avoiding "control flow mess."

There is a Wikipedia entry about this.

    
03.09.2014 в 15:42
1

Spagheti code refers to the code whose flow of control is confused and intertwined as spaghetti. That way, each line of code can be referencing almost any variable anywhere and keeping the code becomes virtually impossible, because the risk of introducing errors elsewhere is very large. This term is widely used in the context of programming using the Goto statement.

This is different from macaroni code , which can be defined as a code that uses a mixture of programming languages in a single document. Thus, to understand a macaronic code it is necessary to be aware of most of the languages used in this code.

The example below is a macroscopic code that blends HTML, PHP, SQL, and Javascript into a single document.

<!DOCTYPE html>
<html>
<head><title>Macaronic Code</title></head>
<body>
    <?php
        $ip = $_SERVER['REMOTE_ADDR'];
        $dbh = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');
        $statement = $dbh->query("SELECT lastlog FROM logins WHERE ip = '$ip'");
        $row = $statement->fetch(PDO::FETCH_ASSOC);
        $dbh->query("INSERT INTO logins VALUES ('$ip', CURRENT_TIMESTAMP) ON DUPLICATE KEY UPDATE lastlog = CURRENT_TIMESTAMP");
    ?>
    <p>Hello, <?php print $ip; ?>.</p>
    <script>
        var lastlog = "<?php print $row['lastlog']?>";
        if (lastlog > "") alert("You last visited "+lastlog);
        else alert("You've never been here before.");
    </script>
</body>

    
24.05.2016 в 19:57