posted February 26, 2014 02:48
Yeah...I really hate one line if statements. Technically they work in many languages, but it's so risky to write code like that lest you make a stupid mistake (like Apple did). As such, I always* use statement blocks.

As for the goto, I suppose I'd need to see the whole section of code, but I'd like to think that it could be better done with a function (i.e. handle_error(foo)), a return (maybe?), or a variable that is acted on before the return. (i.e. error_type=foo; /*more code[...]*/ /*at end: */ if (error_type) {handle_error(error_type))

I'm a Perl guy, and even though 'next' is kind of 'dirty,' it's oh-so-handy during certain file read scenarios. I'm sure there are some legit uses of goto, but barring better source code practices, maybe Apple should have their goto permissions revoked for awhile.

*Never say always...but I'm fairly certain...

--------------------There are three things you can be sure of in life: Death, taxes, and reading about fake illnesses online...Posts: 9305 | From: Westchester County, New York | Registered: May 2001
| IP: Logged

posted February 26, 2014 15:17
I bracket all my if/while/for/etc blocks. And it's easy to point and laugh at this code. But I'm not convinced curly braces would have avoided this. Certainly it would be less likely, but you could still end up with this:

quote:Originally posted by Ugh, MightyClub: Any decent human code reviewer would pick that up, but I doubt a static analysis tool would catch the code smell.

Any decent static analysis tool would have flagged the last 'if' as unreachable code, with or without the brackets.

The trouble with static analysis tools is they're usually too picky, programmers get sick of fixing 'problems' that aren't problems at all, so they stop using the analysis tool.

But I'm confused, the code snippets above would *always* fail, isn't that the exact reverse of what happened in the Apple case?

[update] Just checked the code. 'fail' frees a buffer then returns the err value, which would be 0 (indicating no error, i.e. success)

--------------------If you watch 'The History Of NASA' backwards, it's about a space agency that has no manned spaceflight capability, then does low-orbit flights, then lands on the Moon.Posts: 10631 | From: Melbourne, Australia | Registered: Oct 2002
| IP: Logged

posted February 28, 2014 00:37
The compiler should have flagged this as unreachable code. At work we compile C++ with warning == error. Anything less is going to come back to bite you one day.
Posts: 2893 | From: 5 to 15 meters above sea level | Registered: Jun 2000
| IP: Logged