Random Homebrew: Cubez
Friends: Coding 'n Cracking - Nymphaea - PS3 Forum - darkforestgroup - daxhordes.org - Tgames - coldbird - gopsp.it - pspstation.org - prometheus - hgoel.info - MakeSmartTV - ps vita

'Goto' Statement Morality

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device

'Goto' Statement Morality

Postby Wdingdong » Sun Mar 25, 2012 3:55 pm

I've read about it a lot. That using the 'goto' statement is not a good programming practise.
I think the experts here can explain this to me.


And, is it also bad to use 'goto' statement in C?
// Big thanks to people who share information !!!
Wdingdong
 
Posts: 127
Joined: Tue Aug 02, 2011 5:34 pm

Re: 'Goto' Statement Morality

Postby snailface » Sun Mar 25, 2012 4:39 pm

It's actually a good way to break out of multiple nested loops instead of spamming 'break' statements. It's a lot less confusing.

However, use them sparingly as they can can make programs confusing and less readable if overused. And always leave a comment telling what line the Goto statement 'goes to'.

You really shouldn't find yourself in many situations where it's needed. Long story short, don't abuse it. :)
Image
snailface
 
Posts: 100
Joined: Tue May 24, 2011 8:02 pm

Re: 'Goto' Statement Morality

Postby codestation » Sun Mar 25, 2012 4:47 pm

There is nothing wrong with the goto statement, the only problem is when programmers use it in a way that affects code readability and maintenance.

For example i use them when i have to deal with complex error handling and cleanup code. Sure, i could rewrite the same thing using nested if, extra variables and switches and end with a convoluted mess that nobody would want to maintain (but hey, gotophobic programmers are happy with that :lol: ).

TL;DR: don't abuse goto and use it wisely, don't end being another programmer with gotophobia.
Plugin list
Working on: Python (django, sqlalchemy, PyQt, etc) + Lua
Repositories: github, bitbucket, google code
Just feel the code..
opencma/vitamtp for Archlinux
User avatar
codestation
Big Beholder
 
Posts: 1277
Joined: Wed Jan 19, 2011 3:45 pm
Location: /dev/negi

Re: 'Goto' Statement Morality

Postby m0skit0 » Sun Mar 25, 2012 9:52 pm

snailface wrote:It's actually a good way to break out of multiple nested loops instead of spamming 'break' statements. It's a lot less confusing.

I totally disagree. Is it less confusing to fill the whole code with labels? I don't think so. If you think so, then why even use functions? In which case, why even use C when you can use assembly?

Personally I've never used goto or break statements because I have gotobreakphobia.

The problem comes mostly with error handling as codestation points out. AFAIK C does not provide any decent way for error handling, so you might use goto for such things (although that would look like Visual Basic's On Error Goto horrible source of bugs...). IMHO even assembly has better error handling than C (using sofwtare interrupts for example). I personally prefer to use codestation's nightmare "returning an error code from functions that fail". On other languages you have more decent designs to deal with errors (like Java's exceptions).

Resuming: IMHO you can use goto if there's no decent subsitute for it, or if you think it will make the code more readable and maintainable. Anyway, try to keep it to a minimum.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: 'Goto' Statement Morality

Postby Wdingdong » Mon Mar 26, 2012 5:14 pm

Yes, actually, it all began due to error handling.

And then I came to 'A wise programmer knows how to construct a program wisely and don't need goto'. So, I thought, what's the flaw in my program(an imperative program).
Do object oriented concepts provide you any substitute? Like I found through Google, concepts like encapsulation and destructor(clean up) help you.
I mostly do Imperative programming.
// Big thanks to people who share information !!!
Wdingdong
 
Posts: 127
Joined: Tue Aug 02, 2011 5:34 pm

Re: 'Goto' Statement Morality

Postby m0skit0 » Tue Mar 27, 2012 10:25 am

AFAIK not all object-oriented languages provide decent error handling, but most of them do nowadays (like C++, C#, Java...).
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: 'Goto' Statement Morality

Postby m0skit0 » Tue Apr 24, 2012 12:09 pm

Just wanted to throw this in because I found it very useful and interesting (and ANSI C compatible): http://throwtheswitch.org/white-papers/ ... intro.html
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: 'Goto' Statement Morality

Postby Wdingdong » Tue Apr 24, 2012 6:10 pm

Awesome.

Thank you very much :D
// Big thanks to people who share information !!!
Wdingdong
 
Posts: 127
Joined: Tue Aug 02, 2011 5:34 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests