The project I'm working on right now requires messages be exchanged between a client, an iOS app written in Obj-C, and a server, written in Python.
I needed to write code that escaped messages being sent between the two, which required me to write an identical string encoding method for both of them.
Here's the Python implementation:
It's pretty easy to read (I have comments that explain what it does and gives example input/output despite that) and it follows my rule of not having lines longer than 80 columns.
Then there's the Obj-C implementation:
It seems to me that the important parts of my code are now buried and lost (what's getting replaced by what in what?) How things are related is now lost (it was obvious in the Python code that everything was wrapped in pipes - now you have to look to the final line to see something is wrapped in pipes, and work your way back to realize that fullEscaped is everything else.)
Plus the lines are too long, leaving me with one of two options:
1 - I could wrap, and have @"//" be right under @"/" and @"/|" be right under @"|", although the line will still be over 80 characters long in this case. Plus it looks ugly as sin.
2 - I could just violate my rule and require scanning across lines.
Of course, I could solve my problem by just writing a sub function in C, which internally uses stringByReplacingOccurrencesOfString:withString:, but occupies 3 columns instead of 47 columns with the name of the function.
Heck, I'm thinking about that name right now - it SUCKS. The world string is used thrice in the name and it conveys absolutely nothing each time. This name would convey the same amount of information, be easier to read, and take up fewer columns:
replaceOccurrencesOf:with:
I suppose I might be misplacing my blame. Obj-C has plenty of flaws (@selector(method: )? They honestly couldn't come up with any better way of passing selectors around?) but this post mostly laments how terrible the built in NS classes are... so it's more about the standard Obj-C library than the language itself.
I needed to write code that escaped messages being sent between the two, which required me to write an identical string encoding method for both of them.
Here's the Python implementation:
Code:
def encodeName(rawFilename):
return '|' + re.sub('|', '/|', re.sub('/', '//', rawFilename)) + '|'
It's pretty easy to read (I have comments that explain what it does and gives example input/output despite that) and it follows my rule of not having lines longer than 80 columns.
Then there's the Obj-C implementation:
Code:
NSString *encodeName(NSString *rawFilename) {
NSString *halfEscaped = [rawFilename stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
NSString *fullEscaped = [halfEscaped stringByReplacingOccurrencesOfString:@"|" withString:@"/|"];
return [NSString stringWithFormat:@"|%@|", fullEscaped];
}
It seems to me that the important parts of my code are now buried and lost (what's getting replaced by what in what?) How things are related is now lost (it was obvious in the Python code that everything was wrapped in pipes - now you have to look to the final line to see something is wrapped in pipes, and work your way back to realize that fullEscaped is everything else.)
Plus the lines are too long, leaving me with one of two options:
1 - I could wrap, and have @"//" be right under @"/" and @"/|" be right under @"|", although the line will still be over 80 characters long in this case. Plus it looks ugly as sin.
2 - I could just violate my rule and require scanning across lines.
Of course, I could solve my problem by just writing a sub function in C, which internally uses stringByReplacingOccurrencesOfString:withString:, but occupies 3 columns instead of 47 columns with the name of the function.
Heck, I'm thinking about that name right now - it SUCKS. The world string is used thrice in the name and it conveys absolutely nothing each time. This name would convey the same amount of information, be easier to read, and take up fewer columns:
replaceOccurrencesOf:with:
I suppose I might be misplacing my blame. Obj-C has plenty of flaws (@selector(method: )? They honestly couldn't come up with any better way of passing selectors around?) but this post mostly laments how terrible the built in NS classes are... so it's more about the standard Obj-C library than the language itself.