IBActions, Doing Nothing and Errors


#1

My main question is, if I want IBAction to do nothing do I let a conditional just run through with a !=? Or is best practice to always catch an error even if you don’t want to return a message with it?

As an example here is a bit of code for the delete button from, rtamesis in this thread http://forums.bignerdranch.com/viewtopic.php?f=160&t=3105. Anyhow, if [itemTableView selectedRow] is -1 it just falls through. So is that the way you leave the function if you don’t care about messaging the user or is certain doom lurking in the shadows?

[code]- (IBAction)deleteItem:(id)sender
{
if ([itemTableView selectedRow] != -1) {

[todoItems removeObjectAtIndex:[itemTableView selectedRow]];

[itemTableView reloadData];

[self updateChangeCount:NSChangeDone];
}
}[/code]


#2

If I want an action that doesn’t do anything, I usually leave it empty except for a comment that lets people know “This method was intentionally left empty”:

- (IBAction)ignoreSendComplaintButton:(id)sender { // noop }

“noop” is short for “No Operation”


#3

Got it. thanks. So is something like the following conventional:

[code]- (IBAction)deleteItem:(id)sender
{
NSInteger selectedRowNumber = [itemTableView selectedRow];

if (selectedRowNumber == -1) {
    // noop
}
else {
    [todoItems removeObjectAtIndex:selectedRowNumber];
    [itemTableView reloadData];
    [self updateChangeCount:NSChangeDone];
}

}[/code]


#4

No, usually you’d just negate the conditional and leave out the else block:

[code]- (IBAction)deleteItem:(id)sender
{
NSInteger selectedRowNumber = [itemTableView selectedRow];

if (selectedRowNumber != -1) {
    [todoItems removeObjectAtIndex:selectedRowNumber];
    [itemTableView reloadData];
    [self updateChangeCount:NSChangeDone];
}

}[/code]