Blue Protoman
Well-Known Member
- Joined
- Mar 6, 2010
- Messages
- 4,117
Assignment's due, handed it in today. Hopefully next time I'll get to make something about the Pandora.
But that would be lyingI would have went with some thing like, "Coke: It's not worse than urine."
Example of a good exception to this rule: http://lxr.linux.no/linux+*/drivers/input/mouse/sermouse.c#L2452) if 'grep "goto" foo.c' prints anything out, you get zero by default
Example of a good exception to this rule: http://lxr.linux.no/linux+*/drivers/input/mouse/sermouse.c#L2452) if 'grep "goto" foo.c' prints anything out, you get zero by default
Just pointing out that in this type of error handling (I think most often seen in kernel driver intialization code) it is really (IMO) the cleanest solution.
i got criticised for defining and using a DEBUG_PRINT("blah blah"); macro, as it was 'unecessary bloat'
Our code base at work would give that lecturer a thrombo.
I disagree. I don't want to turn this thread into a design patterns discussion (god forbid) but it's really not the cleanest, far from it. A nightmare to maintain, and it's usually the sign of quick and dirty code that hides some other bugs, potentially nasty. You can always refactor to use very simple control flow constructs, and that is the cleanest solution.Example of a good exception to this rule: http://lxr.linux.no/linux+*/drivers/input/mouse/sermouse.c#L2452) if 'grep "goto" foo.c' prints anything out, you get zero by default
Just pointing out that in this type of error handling (I think most often seen in kernel driver intialization code) it is really (IMO) the cleanest solution.
Do that function without goto and without duplicating cleanup code for me, please.I disagree. I don't want to turn this thread into a design patterns discussion (god forbid) but it's really not the cleanest, far from it.
What is wrong with duplication? I'm not talking of duplicating the code itself, only the methods calls. A first refactoring step would be:Do that function without goto and without duplicating cleanup code for me, please.
...<snip>...
err = serio_open(serio, drv);
if (err)
{
serio_set_drvdata(serio, NULL);
input_free_device(input_dev);
return err;
}
err = input_register_device(sermouse->dev);
if (err)
{
serio_close(serio);
serio_set_drvdata(serio, NULL);
input_free_device(input_dev);
return err;
}
return 0;
...<snip>...
try {
serio_open(serio, drv);
input_register_device(sermouse.dev);
} catch (SerioOpenException e) {
serio_set_drvdata(serio, NULL);
input_free_device(input_dev);
throw e;
} catch (RegisterDeviceException e) {
serio_close(serio);
serio_set_drvdata(serio, NULL);
input_free_device(input_dev);
throw e;
}
...<snip>...
err = serio_open(serio, drv);
if (!err)
{
err = input_register_device(sermouse->dev);
if (!err)
return 0;
serio_close(serio);
}
serio_set_drvdata(serio, NULL);
input_free_device(input_dev);
return err;
gotos are fine if they are used correctly and urjaman linked you to such example. Like Linus Torvalds said once in such discussion, you've been brainwashed by uni CS people.I disagree. I don't want to turn this thread into a design patterns discussion (god forbid) but it's really not the cleanest, far from it. A nightmare to maintain, and it's usually the sign of quick and dirty code that hides some other bugs, potentially nasty. You can always refactor to use very simple control flow constructs, and that is the cleanest solution.Example of a good exception to this rule: http://lxr.linux.no/linux+*/drivers/input/mouse/sermouse.c#L245
Just pointing out that in this type of error handling (I think most often seen in kernel driver intialization code) it is really (IMO) the cleanest solution.
My 2c.
I've also been brainwashed by 13 years of professional coding experience (not to mention the previous 10 years as an amateur), and throwing names will not change anything. I've been proven many times that writing clean, maintainable code is the good thing to do, and goto defeats that.gotos are fine if they are used correctly and urjaman linked you to such example. Like Linus Torvalds said once in such discussion, you've been brainwashed by uni CS people.I disagree. I don't want to turn this thread into a design patterns discussion (god forbid) but it's really not the cleanest, far from it. A nightmare to maintain, and it's usually the sign of quick and dirty code that hides some other bugs, potentially nasty. You can always refactor to use very simple control flow constructs, and that is the cleanest solution.Example of a good exception to this rule: http://lxr.linux.no/linux+*/drivers/input/mouse/sermouse.c#L245
Just pointing out that in this type of error handling (I think most often seen in kernel driver intialization code) it is really (IMO) the cleanest solution.
My 2c.
That's not dupe code!I don't consider your "improvements" maintainable code - actually it made it less maintainable as someone needing to add something to the body would have to take care not to forget to update each duplicated error handling case you added. Not to mention longer overall time wasted to read and understand that dupe code.
Now you lost me . It is a kernel device driver. It is written in C. (Why not C++: http://www.tux.org/lkml/#s15-3)refactoring to collaborator objects
That is just because I guess that you havent written a lot of linux kernel code. When I see:And I disagree with the longer overall time to read and understand it. When I see a goto, I already feel a headache starting, and I know there is going to be unexpected side-effects.
if (!sermouse || !input_dev)
goto fail1;
When I see a goto, I already feel a headache starting, and I know there is going to be unexpected side-effects.
// implementing a simple if( <int> == <int> ) / else statement
ifstatement:
cmpl %eax, %ecx // compare two numbers
je thenblock // if equal, jump (or: "goto") label "thenblock"
elseblock:
// code here
jmp done // jump (or: "goto") over "thenblock" to "done"
thenblock:
// code here
done:
// continue execution
What side effects? You act like it will jump to random point of your program or a different process even while it's 100% defined and predictable. I suppose you never coded asm?And I disagree with the longer overall time to read and understand it. When I see a goto, I already feel a headache starting, and I know there is going to be unexpected side-effects.
I don't know how is that better code, you device various workarounds just to avoid that 4 letter word. In this case it doesn't look too bad yet, but add a couple of nesting levels and it becomes horrible. Writing a cleanup method for every error case is still duplication, like it or not. With super-cleanup method you just shift that nested monster to another place and add unnecessary complexity.And what about my third piece of code above? No "duplication", no goto. Only nested ifs. Personally, I find it almost equally headache-inducing, as I much prefer controlling the flow in a simple way and refactoring to collaborator objects when nesting level becomes too high.
Of course, but it's done in a verified, controlled and presumably tested way. In other words, its behaviour is predictable.Oh, please. I'll admit I am not yet a professional programmer and I only have about 10 (5 once I really got serious) years' experience, but when I see someone arguing something like this, it makes me incredibly angry at the widespread ignorance.
Could you please find the x86 instruction for me that corresponds to an IF statement? No, you can't, because it does not exist.
All real code is, in some way, shape, or form, eventually either compiled directly into assembly, or translated indirectly into machine code. Neither of these two things (which are really the same thing) have complex "control structures." A basic if-else statement, for example (probably unnecessarily cumbersome; I am just learning assembly and it is early morning for me):
Thanks for the insult, but I'm afraid you misunderstood me. When I say pretty, I'm talking about producing an easy-to-maintain, simple, and high quality code. I don't care for clever finds to save one line of code (what for? Is memory that expensive these days?)Arguing pointless things such as how pretty code looks is utterly stupid. Especially when you are talking about repeating code just so your code looks pretty.
I'm going to be the Pacifist, probably. Sorry if you don't get my intent and all you see is a desire to produce "pretty" code, but I'm working on applications that have been alive for 5-10 years with a team of 12 people interacting daily with it (none of them were there at the beginning of the development, and our source control repository is at revision 48000, each one of them properly built into a continuous integration server) and if you don't have strict guidelines and your development practices, you fail because it becomes quickly unmaintainable. I've seen that over and over on "clever" code.Taking systems programming this quarter has introduced me to thinking on the hardware level-- and when you're thinking about hardware, you care about two things: getting something done, and doing it in as few instructions as possible. You are suggesting we replace a brilliant little piece of C with something you feel is more "sensible and proper." A useful analogy to think about would be the Revolutionary War. Are you going to be the damn British, prim and proper, lining up in human brick formation to die? Or are you going to do the sensible thing: go guerilla, hide behind trees and take fewer casualties?
Of course, but it's done in a verified, controlled and presumably tested way. In other words, its behaviour is predictable.Oh, please. I'll admit I am not yet a professional programmer and I only have about 10 (5 once I really got serious) years' experience, but when I see someone arguing something like this, it makes me incredibly angry at the widespread ignorance.
Could you please find the x86 instruction for me that corresponds to an IF statement? No, you can't, because it does not exist.
All real code is, in some way, shape, or form, eventually either compiled directly into assembly, or translated indirectly into machine code. Neither of these two things (which are really the same thing) have complex "control structures." A basic if-else statement, for example (probably unnecessarily cumbersome; I am just learning assembly and it is early morning for me):
Thanks for the insult, but I'm afraid you misunderstood me. When I say pretty, I'm talking about producing an easy-to-maintain, simple, and high quality code. I don't care for clever finds to save one line of code (what for? Is memory that expensive these days?). When I'm proud of my code because I know it does what intended for, in a repeatable and tested way, and is maintainable and understandable, I call it pretty.Arguing pointless things such as how pretty code looks is utterly stupid. Especially when you are talking about repeating code just so your code looks pretty.
I won't. I'm French, btw.You're being British. Stop it.
It will if someone else modifies it later. Have you never shared code with someone else? Or worked in a team of more than 1 person? And yes I did a lot of assembly. But even though it's very interesting to understand how everything works internally, that's as much a computer language as the wind is a human language.What side effects? You act like it will jump to random point of your program or a different process even while it's 100% defined and predictable. I suppose you never coded asm?
Nothing is hardcoded. I explained my reasons for it.Some people just have "goto=evil" hardcoded in their heads and just deny everything else about goto without even thinking about it.
Agreed, and that's what I said. That's not the word goto that I want to avoid, that's everything that breaks the normal flow of the program. I equally hate break or switch statements, they are just goto in disguise.I don't know how is that better code, you device various workarounds just to avoid that 4 letter word. In this case it doesn't look too bad yet, but add a couple of nesting levels and it becomes horrible.
No it's not!Writing a cleanup method for every error case is still duplication, like it or not. With super-cleanup method you just shift that nested monster to another place and add unnecessary complexity.
Well I never pretended to have the ultimate truth in me (If I sounded this way, I'm sorry). I'm just giving my opinion, and I'm perfectly fine with you having your ownOf course everyone has their own ways to get the job done and I'm fine with that. But if goto causes nightmares to you it doesn't mean it does the same for others, so stating goto is bad and other ways are always better is simply incorrect.