tag:blogger.com,1999:blog-6112936277054198647.post7877096907711500974..comments2024-03-12T00:42:06.642+10:00Comments on Who-T: Improving code readability through temporary variablesPeter Huttererhttp://www.blogger.com/profile/17204066043271384535noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-6112936277054198647.post-2466734468514536262011-12-17T07:28:00.145+10:002011-12-17T07:28:00.145+10:00Not just Ada. Python, R, and Perl (sort of), off ...Not just Ada. Python, R, and Perl (sort of), off the top of my head, all support named parameters.<br /><br />In C, I've seen people use empty #defines to annotate parameters.Rafihttps://www.blogger.com/profile/17862631469261785571noreply@blogger.comtag:blogger.com,1999:blog-6112936277054198647.post-66792695844285665572011-12-04T22:16:37.877+10:002011-12-04T22:16:37.877+10:00Named parameters is a functionality I miss in C......Named parameters is a functionality I miss in C...<br /><br />In Ada, you could write:<br /><br />foo (f => mystruct,<br /> check_device => !dev->invalid,<br /> max_devices => 0,<br /> emulate => list_is_first_entry(dev->list));<br /><br />And in case of API change, the compiler would bug you.Davidhttps://www.blogger.com/profile/15876576308245391285noreply@blogger.comtag:blogger.com,1999:blog-6112936277054198647.post-72524635736534935152011-12-03T10:03:50.364+10:002011-12-03T10:03:50.364+10:00Eh, I'm going to have to second the author on ...Eh, I'm going to have to second the author on this one. Well written code should be self-documenting. Let the compiler do its job. Temporary variables of this sort are nearly always optimized out (I've never seen an instance where they weren't). If you need a comment (or multiple comments) to explain a single line of code, you should probably rewrite it.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6112936277054198647.post-70921329404917471162011-12-03T06:45:22.385+10:002011-12-03T06:45:22.385+10:00I always just do:
foo(mystruct, /* check_device =...I always just do:<br /><br />foo(mystruct, /* check_device = */ !dev->invalid, /* max_devices = */ 0, /* emulate = */ list_is_first_entry(dev->list));Luke Hutchisonhttps://www.blogger.com/profile/07119186060702104432noreply@blogger.comtag:blogger.com,1999:blog-6112936277054198647.post-47516846702274850712011-12-02T22:59:44.551+10:002011-12-02T22:59:44.551+10:00So, you're trying to add extra information to ...So, you're trying to add extra information to the code in order to make it more understandable to the humans who are reading it. Why not just ... use comments?<br /><br />foo(mystruct,<br /> !dev->invalid, /* check_device */<br /> 0, /* max_devices */<br /> list_is_first_entry(dev->list)); /* emulate_pointer */<br /><br />If I saw your second version of the code, I'd assume that it was a left-over wart from some previous code, where, e.g. calculating the value of the check_device flag was a lot more complex, perhaps conditional on something, and the clean-up didn't quite go as far as it could. Then I'd submit a patch to finish tidying things up.<br /><br />If you want to document the code for humans, use a comment. That's what they're there for. And they're meta-self-documenting about the fact that they are documentation.<br /><br />Yes, comments can be overused. But just because the apocryphal /* increment i */ is as pointless as the flamboyant over-use of "goto" is nightmarish, that is no reason to completely ban either comments or goto. Both have their place, and should be used where appropriate.Anonymousnoreply@blogger.com