Hello, Goto is recommend in linux kernel programming, but it is despised in many other situation. There are four rationable for using goto in Documentation/CodingStyle. Do you have some viewpoints about "why goto" or "why not goto"? I'm glad to get your point. Thank you.
On Fri, Jul 17, 2015 at 1:25 PM, Navy <navych@126.com> wrote:
Hello, Goto is recommend in linux kernel programming, but it is despised in many other situation. There are four rationable for using goto in Documentation/CodingStyle. Do you have some viewpoints about "why goto" or "why not goto"? I'm glad to get your point. Check the file drivers/staging/dgap/dgap.c there is a function called dgap_init_one() which is using 6 goto statements. Please try to convert that file without using goto and i hope you will understand practically why goto.
regards sudip
Very easy: static int dgap_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { int rc = 0; int cleanupState = 0; struct board_t *brd; void cleanup() { if (cleanupState > 4) { dgap_tty_free(brd); } if (cleanupState > 3) { dgap_free_irq(brd); dgap_tty_unregister(brd); } if (cleanupState > 2) { dgap_tty_unregister(brd); } if (cleanupState > 1) { dgap_free_flipbuf(brd); } if (cleanupState > 0) { dgap_cleanup_nodes(); dgap_unmap(brd); kfree(brd); } } if (dgap_numboards >= MAXBOARDS) return -EPERM; rc = pci_enable_device(pdev); if (rc) return -EIO; brd = dgap_found_board(pdev, ent->driver_data, dgap_numboards); if (IS_ERR(brd)) return PTR_ERR(brd); rc = dgap_firmware_load(pdev, ent->driver_data, brd); cleanupState++; if (rc) { cleanup(); return rc; } rc = dgap_alloc_flipbuf(brd); if (rc) { cleanup(); return rc; } rc = dgap_tty_register(brd); cleanupState++; if (rc) { cleanup(); return rc; } rc = dgap_request_irq(brd); cleanupState++; if (rc) { cleanup(); return rc; } /* * Do tty device initialization. */ rc = dgap_tty_init(brd); cleanupState++; if (rc) { cleanup(); return rc; } rc = dgap_tty_register_ports(brd); cleanupState++; if (rc) { cleanup(); return rc; } brd->state = BOARD_READY; brd->dpastatus = BD_RUNNING; dgap_board[dgap_numboards++] = brd; return 0; } Am 17.07.2015 10:11 schrieb Sudip Mukherjee:
On Fri, Jul 17, 2015 at 1:25 PM, Navy <navych@126.com> wrote:
Hello, Goto is recommend in linux kernel programming, but it is despised in many other situation. There are four rationable for using goto in Documentation/CodingStyle. Do you have some viewpoints about "why goto" or "why not goto"? I'm glad to get your point. Check the file drivers/staging/dgap/dgap.c there is a function called dgap_init_one() which is using 6 goto statements. Please try to convert that file without using goto and i hope you will understand practically why goto.
regards sudip
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
On Fri, Jul 17, 2015 at 8:55 AM, Navy <navych@126.com> wrote:
Hello, Goto is recommend in linux kernel programming, but it is despised in many other situation. There are four rationable for using goto in Documentation/CodingStyle. Do you have some viewpoints about "why goto" or "why not goto"? I'm glad to get your point. Thank you.
Bit of kernel History here, read what Linus said about goto http://koblents.com/Ches/Links/Month-Mar-2013/20-Using-Goto-in-Linux-Kernel-... Thank you Warm Regards Anuz
Hi! On Fre, 2015-07-17 at 15:55 +0800, Navy wrote: [...]
Goto is recommend in linux kernel programming, but it is despised in many other situation. There are four rationable for using goto in
"goto" is (usually totally) forbidden for beginners/inexperienced programmers because some of us are old enough to have started programming with Basic on the C64 (no functions there - just "goto" and "gosub") and know what may happen in the long run if you write more than a hello-world.c ... My usual answer to "when may or should I use 'goto'" is: You will know when it's time - before that, simply don't use it.
Documentation/CodingStyle. Do you have some viewpoints about "why goto" or "why not goto"? I'm glad to get your point.
It mainly depends *how* you use it - see the patterns in the kernel for not so bad ones;-) And - as others wrote - rewrite the code without 'goto' and look into it after 3 months and decide which version is more readable/understandable. BTW that holds for all programming "style advices" (starting from "when should i factor out a function" over "how large should a function should be" and "too few or too many comments" to ...). It is like everywhere else: If the guideline is trivial to check, it is probably silly anyways. The big goal in (99,9% of) software development is: You want source code to be as easy to read and understand as possible - and nothing else! Coding style guidelines are just that: guidelines in that direction but never necessary nor sufficient to guarantee that (so the occasional violation for good reason - which one writes into a comment;-) - is not evil). Bernd -- "I dislike type abstraction if it has no real reason. And saving on typing is not a good reason - if your typing speed is the main issue when you're coding, you're doing something seriously wrong." - Linus Torvalds
On 17 July 2015 at 11:00, Bernd Petrovitsch <bernd@petrovitsch.priv.at> wrote:
Hi!
On Fre, 2015-07-17 at 15:55 +0800, Navy wrote: [...]
Goto is recommend in linux kernel programming, but it is despised in many other situation. There are four rationable for using goto in
"goto" is (usually totally) forbidden for beginners/inexperienced programmers because some of us are old enough to have started programming with Basic on the C64 (no functions there - just "goto" and "gosub") and know what may happen in the long run if you write more than a hello-world.c ... My usual answer to "when may or should I use 'goto'" is: You will know when it's time - before that, simply don't use it.
Documentation/CodingStyle. Do you have some viewpoints about "why goto" or "why not goto"? I'm glad to get your point.
It mainly depends *how* you use it - see the patterns in the kernel for not so bad ones;-) And - as others wrote - rewrite the code without 'goto' and look into it after 3 months and decide which version is more readable/understandable.
BTW that holds for all programming "style advices" (starting from "when should i factor out a function" over "how large should a function should be" and "too few or too many comments" to ...). It is like everywhere else: If the guideline is trivial to check, it is probably silly anyways.
The big goal in (99,9% of) software development is: You want source code to be as easy to read and understand as possible - and nothing else!
Coding style guidelines are just that: guidelines in that direction but never necessary nor sufficient to guarantee that (so the occasional violation for good reason - which one writes into a comment;-) - is not evil).
Bernd
This is an interesting article about the history of goto being considered harmful; and how Dijkstra’s paper about it was misunderstood. http://videlalvaro.github.io/2015/02/programming-myths.html Luis
participants (6)
-
Anuz Pratap Singh Tomar -
Bernd Petrovitsch -
Luis de Bethencourt -
Martin Knappe -
Navy -
Sudip Mukherjee