Skip to main content

Linus’ discussion about goto statements

As discussed by Linus Torvalds 6 years ago,


From: Linus Torvalds
Subject: Re: any chance of 2.6.0-test*?
Date: Sun, 12 Jan 2003 12:22:26 -0800 (PST)


On Sun, 12 Jan 2003, Rob Wilkens wrote:
>
> However, I have always been taught, and have always believed that
> “goto”s are inherently evil. They are the creators of spaghetti code


No, you’ve been brainwashed by CS people who thought that Niklaus Wirth
actually knew what he was talking about. He didn’t. He doesn’t have a
frigging clue.


> (you start reading through the code to understand it (months or years
> after its written), and suddenly you jump to somewhere totally
> unrelated, and then jump somewhere else backwards, and it all gets ugly
> quickly). This makes later debugging of code total hell.


Any if-statement is a goto. As are all structured loops.


And sometimes structure is good. When it’s good, you should use it.


And sometimes structure is _bad_, and gets into the way, and using a
“goto” is just much clearer.



For example, it is quite common to have conditionals THAT DO NOT NEST.


In which case you have two possibilities


- use goto, and be happy, since it doesn’t enforce nesting


This makes the code _more_ readable, since the code just does what
the algorithm says it should do.


- duplicate the code, and rewrite it in a nesting form so that you can
use the structured jumps.


This often makes the code much LESS readable, harder to maintain,
and bigger.


The Pascal language is a prime example of the latter problem. Because it
doesn’t have a “break” statement, loops in (traditional) Pascal end up
often looking like total shit, because you have to add totally arbitrary
logic to say “I’m done now”.


Linus


Read the full discussion [here]. PHP 5.3 recently introduced goto statements.

Comments

Popular posts from this blog

Architecture Complexity

Here are the items to consider: Coding to an interface Service Oriented Architecture Automated Testing Domain Driven Design Custom Data Access Layer Layered architecture Complexity is relatively equal the number of lines of code. Note that complexity is not bad. It must be justified.

Repair Windows 7 System Files

8 out of 10 average PC users have their box’s system files altered by malwares, viruses, etc. We usually reinstall the OS if the antivirus and anti malware software did not perform their job well. Here’s one way to fix the corrupted system files without the need of restarting your Windows 7 box. 1. Run the Command Prompt as Administrator 2. Type the following command C:\Windows\system32\> sfc /scannow 3. After the verification phase, you will receive a message about your system files’ integrity Windows Resource Protection did not find any integrity violations.

Android Studio:Unknown Host Error

After installing Android Studio, I got the following error: Unknown host 'services.gradle.org'. Please ensure the host name is correct. If you are behind an HTTP proxy, please configure the proxy settings either in Android Studio or Gradle. Consult IDE log for more details (Help | Show Log) Solution File --> Settings --> HTTP Proxy --> Auto-detect proxy settings