Obj-C string construction
-
So I was looking at some code I wrote a few years back, when I was first learning objective-C. I found many places where I did something like this:
char buffer[100];
sprintf(buffer, "the value: %d", someValue);
NSString* string = [NSString stringWithCString:buffer];of course, with more useful text and values. I think there may have even been some times when I malloc'd buffer rather than having it on the stack... for those who aren't familiar with obj-C, this could be more sanely/appropriately written as:
NSString* string = [NSString stringWithFormat:"the value: %d", someValue"];
-
So I was looking at some code I wrote a few years back, when I was first learning objective-C. I found many places where I did something like this:
char buffer[100];
sprintf(buffer, "the value: %d", someValue);
NSString* string = [NSString stringWithCString:buffer];of course, with more useful text and values. I think there may have even been some times when I malloc'd buffer rather than having it on the stack... for those who aren't familiar with obj-C, this could be more sanely/appropriately written as:
NSString* string = [NSString stringWithFormat:"the value: %d", someValue"];
I constantly observe the same type of constructions with the MFC CString, when the programer does not realize there is a .Format() function to do the same thing. I also crack up at the use of strlen and its variants to detect if the string is empty. How about just chekcing the first array slot for a zero! Or maybe they weren't after any kind of performance after all...
-
I constantly observe the same type of constructions with the MFC CString, when the programer does not realize there is a .Format() function to do the same thing. I also crack up at the use of strlen and its variants to detect if the string is empty. How about just chekcing the first array slot for a zero! Or maybe they weren't after any kind of performance after all...
I have known programmers who say that readability is more important than small performance gains. This doesn’t sound too bad until you realize that by “readable” they mean “using what they are familiar with” and not learning new, faster and better ways of doing things. Also the performance of their code is abysmal.
Just because the code works, it doesn't mean that it is good code.
-
So I was looking at some code I wrote a few years back, when I was first learning objective-C. I found many places where I did something like this:
char buffer[100];
sprintf(buffer, "the value: %d", someValue);
NSString* string = [NSString stringWithCString:buffer];of course, with more useful text and values. I think there may have even been some times when I malloc'd buffer rather than having it on the stack... for those who aren't familiar with obj-C, this could be more sanely/appropriately written as:
NSString* string = [NSString stringWithFormat:"the value: %d", someValue"];
The real horror here is that you don't check for the possibility of buffer overrun.