Should I use an intermediate temporary variable when adding to an NSString?

It works - it compiles - but I just wanted to check if this would be considered good practice or something to avoid?

NSString *fileName = @"image"; fileName = [fileName stringByAppendingString:@".png"]; NSLog(@"TEST : %@", fileName); OUTPUT: TEST : image.png 

Better to write with a temporary variable:

 NSString *fileName = @"image"; NSString *tempName; tempName = [fileName stringByAppendingString:@".png"]; NSLog(@"TEST : %@", tempName); 

just curious.

+7
coding-style objective-c cocoa
source share
4 answers

Inside, compilers usually break your code into a representation called "Single Static Assignment", where only one value is assigned to this variable, and all the operators are as simple as possible (compound elements are separated on different lines). A second example follows this approach.

Programmers sometimes write like that. It is considered the clearest way of writing code, since you can write all statements as basic tuples: the operator A = B C. But it is usually considered too verbose for code that is “obvious”, so it is an unusual style (outside of situations where you trying to make a very cryptic code understandable).

Generally speaking, programmers will not be embarrassed by your first example, and this is considered acceptable if you no longer need the original fileName . However, many Obj-C programmers encourage the following style:

 NSString *fileName = [@"image" stringByAppendingString:@".png"]; NSLog(@"TEST : %@", fileName); 

or even (depending on the horizontal space on the line):

 NSLog(@"TEST : %@", [@"image" stringByAppendingString:@".png"]); 

i.e. if you use only one variable once, do not name it (just use it in place).

However, if you adhere to the Single Static Assigment approach, you should not use tempName as the name of the variable, since it does not explain the role of the variable - instead, you should use something like fileNameWithExtension . In a broader sense, I usually avoid using “temp” as a prefix, since it's too easy to start calling all “temp” (all local variables are temporary, so it makes little sense).

+12
source share

The first line declares an NSString literal. It has a repository that lasts the entire life of the process, so it does not need to be released.

Calling stringByAppendingString returns an autoreclosed NSString. It also should not be released, but it will continue until it proceeds to the next leak of the auto-advertisement pool. Therefore, when assigning the result of calling stringByAppendingString back to the fileName pointer in this case is excellent. In general, however, you should check what your object's lifespan is and process them accordingly (for example, if the file_name was declared as a string that you own, you will need to free it, so you will need to use temporary settings).

Another thing to check is if you do something with the file name after this fragment - for example, holding it in an instance variable - in this case you will need to save it.

+5
source share

The only difference is whether you need a reference to a literal string or not. From the POV memory management and the POV creation object, it really doesn't matter. One thing to keep in mind is that the second example makes it easier to debug. My preferred version would look like this:

 NSString *fileName = @"image"; NSString *tempName = [fileName stringByAppendingString:@".png"]; NSLog(@"TEST : %@", tempName); 

But in the end, it is only a matter of preference.

+2
source share

I think you're right, this is really to your preferred style.

Personally, I like your first example, the codes are not complicated, and the first version is short and convenient before our eyes. Theres too much “language” to hide what it does in the second example.

As noted, memory management does not seem to be a problem in the examples.

+1
source share

All Articles