+ 4
Why is "goto" considered to be so taboo?
I knew that these existed a while ago but never used them because everybody said that they were terrible to use. Yesterday, I decided to use them in some of my code to see how they'd work and they're very nice, convenient things. I don't think it's responsible as programmers to ignore such a powerful tool simply because it's considered "bad practice." Make it good practice and use it then. Really, the same thing goes with "using namespace std" - you see two sides of the argument, one which believes it's bad practice, the other that does not.
5 Respostas
+ 15
It isn't recommended, because if you use a loop, you can see where the block ends and begins within curly brackets. This isn't the same with goto. Also, it does work great and similar to loops, but it's not that readable, especially in bigger products, therefore is recommended against using for the readability of code.
Another key point is that it doesn't have a scope. It just goes back to wherever you set it to go back. You can't create a variable between the goto and where it is meant to be, because that variable won't be handled as if it's in a loop. Let's say you add 1 to a variable var within that code. Each time it goes back, it keeps adding 1, leaving it in an unpredictable state. It's considered dangerous to use goto.
Now, this is the opinion that I have seen out there as to why goto is bad. I personally have not used it, but wouldn't be against it's use. It's just you have to be very careful with it's implementation.
As for "using namespace std;", it includes the definition of many words. Say you're working on a big program using multiple namespaces at a company. There are namespaces that redefine different keywords such as "vector". This can cause errors, because the compiler won't know which one you are referrinng to, and it assumes based on picking the best one, but at the same time, this can easily go wrong and cause unexpected results. My recommendation: You can use the std namespace in a small program or one with namespaces that don't conflict or are self designed, but when you start working on bigger programs, don't use "using namespace std;", rather "std::vector" or whichever you sre trying to use, so you can always know exactly what you're calling, and it won't cause big errors in the future.
+ 5
[supporting "not taboo"] One of the worst things about 'goto' is all the white noise it created.
Try searching on it and it's like every technical reference on the planet is afraid of the truth (use cases for 'goto' exist).
Use goto when it clarifies/streamlines your code path.
~ See any Linux kernel source; it's very easy to find.
~ Here's a Python where goto would work if available:
while True:
# stuff for loop
for a in range(100):
# stuff
if some_condition: break # goto...
else: continue
break
#...lands here
Do you see what that does? It's clever! -- but you have to study it to understand it's double-breaking without setting the stu...sorry: necessary "I just broke" flag.
Convolving your codes to find acrobatics around gotos can waste effort and even introduce security bugs.
(Used properly, you'd want to goto out of the above trap)
Let clarity win.
+ 2
"using namespace std" at the top is objectively bad because there is a better alternative:
int main(){
//only for this function!
using namespace std;
}
I think goto mostly became taboo because a lot of programmers grew up with languages like BASIC which only has `goto` to structure code, and so a generation of programmers was misusing `goto` in other languages, where there also were better alternatives most of the time.
I think `goto` is fine. There aren't a lot of use cases in a language like C++ but they exist. I have seen a lot of very bad code abusing exceptions or `break` statements where a simple `goto error` would have been more elegant. I also use it on occasion (rarely). My personal rule of thumb is to use goto to only jump past the last return statement, and to have at most one label in a function.
+ 1
Thanks to all who commented! I now understand goto a lot better and will only use them where it's easy to follow and necessary.
Thank you for helping me become a better programmer! 😀
0
GOTO is used mainly in BASIC, also in Batch scripts, Pascal... It is useful in scripting languages like Batch and somehow BASIC (some dialects). But you should avoid using it with today languages, it became antiquated.