Announce at the beginning or along the way?

I would say that it is pretty much style / readability, although I can see almost all objective-c / cocoa in the "METHOD_002" format. I'm just wondering if "METHOD_001" would be considered a bad style, there are advantages to having all the declarations at the top of the method, but again there are disadvantages in terms of readability if you do not declare objects where you use them?

METHOD_001

-(IBAction)dateButtonPressed {

    NSDate      *dateSelected;
    NSString    *dateString;
    NSArray     *dateItems;
    NSString    *alertMessage;
    UIAlertView *alert;

    dateSelected = [datePicker date];
    dateString = [[NSString alloc] initWithFormat:@"%@", dateSelected];
    dateItems = [dateString componentsSeparatedByString:@" "];
    alertMessage = [[NSString alloc] initWithFormat:@"Date: %@ Time: %@",  [dateItems objectAtIndex:0], [dateItems objectAtIndex:1]];
    alert = [[UIAlertView alloc] initWithTitle:@"You Selected" 
                                       message:alertMessage 
                                      delegate:nil 
                             cancelButtonTitle:@"OK" 
                             otherButtonTitles:nil];
    [alert show];
    [alert release];
    [alertMessage release];
    [dateString release];

}

METHOD_002

-(IBAction)dateButtonPressed {

    NSDate *dateSelected = [datePicker date];
    NSString *dateString = [[NSString alloc] initWithFormat:@"%@", dateSelected];
    NSArray *dateItems = [dateString componentsSeparatedByString:@" "];
    NSString *alertMessage = [[NSString alloc] initWithFormat:@"Date: %@ Time: %@",  [dateItems objectAtIndex:0], [dateItems objectAtIndex:1]];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Selected" 
                                                    message:alertMessage 
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    [alertMessage release];
    [dateString release];

}

Gary

+3
source share
6 answers

METHOD_002 . METHOD_001, , . C ( ), . ANSI. .

METHOD_002 :

  • . (, for()), . ANSI , , , , .

  • . , "" nil. , NULL . "". , . METHOD_002, , .

  • Extract , . , .

  • "". , , , . , .

  • . , , , "tmpValue", . , , , , , , , . , , .

, METHOD_002:

  • . IDE, . , , , . . .
+5

"METHOD_002". , , .

, "METHOD_001" , , , . , .

+1

METHOD_002. METHOD_001 , , , ASP-, , , ( ...). , , -, strFwdBck, , . " ", , .

, , . .

-(IBAction)dateButtonPressed {
     NSString *dateString = [[NSString alloc] initWithFormat:@"%@", [datePicker date]];
     NSArray *dateItems = [dateString componentsSeparatedByString:@" "];

     NSString *alertMessage = [[NSString alloc] initWithFormat:@"Date: %@ Time: %@",        [dateItems objectAtIndex:0], [dateItems objectAtIndex:1]];
    [dateString release];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Selected" 
                                                    message:alertMessage 
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    [alertMessage release];
}
+1

. , _002. , . , , _001, , .

:

_002:

-(void) someMethod{
    //...some code
    SomeClass *myVar=[assign someValue];
    //...some more code
    if (someTest) {
        myVar=someValue;
    }else{
        myVar=someOtherValue;
    }
}

myVar=someOtherValue;, , . , _001, :

-(void) someMethod{
    SomeClass *myVar;
    //...some code
    myVar=[assign someValue];
    //...some more code
    if (someTest) {
        myVar=someValue;
    }else{
        myVar=someOtherValue;
    }
}

, .

, , , /, , , , .

, , , .

+1

, , , , , , / .

, , - , / . ( google)

0

, 2 1/2 1 , 3

3 , , , . , .

1 2 , -, . , . , , . , , ​​, .

This basically becomes a problem when you have long functions (which you shouldn't do anyway, but this happens), and so you declare a variable 20 lines per function, and then use it again 50 lines and again 20 lines later. Figure out which type was easier if you just go to the beginning of the function and look there ...

However, as you noted, this is a style issue, so the question of opinion ends ...

0
source

Source: https://habr.com/ru/post/1732339/


All Articles