Re: Proper switching and casing.
Re: Proper switching and casing.
- Subject: Re: Proper switching and casing.
- From: Alex Zavatone <email@hidden>
- Date: Thu, 09 Apr 2015 17:05:57 -0400
Sorry that I sent this before the main observation.
Considering the implications of this, should Xcode's compiler flag these "unbreak-ed case statements" with a warning? Currently, it doesn't do that.
Is there any reasonable case where you'd want a case condition to not have a break statement after it?
Thanks,
Alex Zavatone
On Apr 9, 2015, at 4:53 PM, Alex Zavatone wrote:
> I'm looking through the new code that I've inherited and getting to know and love it and I'm running across a few new circumstances I've never seen before.
>
> With that said, I just noticed a switch statement with empty case conditions where the variable being checked will be 0, and the execution point/program counter hits both the case statement for 0 and for 1.
>
> It looks something like this:
>
> switch (myObject.myInt) {
>
> case 0:
> {
> // There is no code at all within these parens. This is empty.
> }
>
> case 1:
> {
> // Important stuff happens here
> }
> break;
>
> case 2:
> {
> // More important stuff happens here
> }
> break;
>
> default:
> break;
> }
>
> I was really surprised as all our code within case 1 was getting executed when myInt == 0 and when myInt == 1. Then I noticed that case:0 was put in for a placeholder condition. Since it was entered as a placeholder, it was entered without a break statement after it. In the case of myInt == 0, the program execution just continued down into the case of 1 code block and happily executed it.
>
> Who says learning new code isn't fun?
>
> Cheers,
> Alex
>
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden