Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 14 Nov 2012
Offline Last Active Nov 16 2012 05:22 PM

Posts I've Made

In Topic: C++ - Is Goto a Good Practice?

14 November 2012 - 10:29 PM

As long as a language has goto, it isn't intrinsically bad to use it.

It is possible to use it either in a right or in a wrong way.

The thing is that as a language has a higher level than others, it is in general spontaneously easier and natural to avoid using goto.

I am currently mainly an x86 Assembly language programmer but I can program a bit of C, Pascal, and a good amount of JavaScript and PHP, so I can tell you that in languages higher than Assembly (including C) I rarely use goto. It might seem paradoxical but Assembly language has helped me no end to find ways to program very elegantly and concisely without using goto, or excessive nested ifs or nested function calls.

Now, I find myself all the time converting higher-level code to Assembly by hand. In that case, you can see that down to the bare metal level all of your loops, conditional code and the like, must be converted to conditional Assembly instructions that work exactly like goto, and in general it can't be avoided at that point (which by the way is by no means an obsolete environment but just the very essence of a machine).

I also find myself converting Assembly code to higher-level code, and in this case I must find ways to do it efficiently. I almost never need to use goto when doing this.

In other words, knowing how to use goto properly is necessary, not to be abused and not to be ignored either (specially if you know and want to use Assembly language, among other things, to work a little bit in designing or understanding compiler-like programs that generate Assembly code).

If you use it too much in a high-level language, chances are that you are still in a very incipient level of knowledge for a proper implementation of your logic when you could use syntax elements better suited for what you want to achieve in that language (e.g., using goto for no special reason when you could have used a while loop that maybe you just didn't know how to use).