5 ms·
I still remember my horror when I saw the pain of something as simple as string concatenation in Objective-C. The community literally has to write special micro
by Xeoncross 4y ago
I still remember my horror when I saw the pain of something as simple as string concatenation in Objective-C. The community literally has to write special micros to make joining a string palatable [1]. I salute the brave souls who made OSX/iOS apps before Swift.
NSString *myString = @"Hello";
NSString *second = [myString stringByAppendingString:@" World"];
[1]: https://stackoverflow.com/questions/510269/shortcuts-in-objective-c-to-concatenate-nsstrings/16862414 https://stackoverflow.com/questions/510269/shortcuts-in-obje...
- ardit33 4y agoAgreed. But once you get some macros (or categories) then Objective-C becomes a great language. Is both elegant and very powerful as it is has the perfect balance between a static and a dynamic language. Swift on the other had is a language where wonks decided to throw every silly feature they thought off to the point that is becoming more complex than even Scala. I wished Swift was just a modernized Objective-C with a normal non bracket syntax and some extra facilities that it was missing. It is such a wasted opportunity
- nightowl_games 4y agoObjective-C is powerful yes, but elegant? Not sure. It can do everything but I always felt like it was basically the opposite of elegant.
- richarddaystrom 4y agoI would describe it as practical
- mpweiher 4y agoAgreed, and that's kind of where Objective-S started: https://blog.metaobject.com/2019/12/the-4-stages-of-objective-smalltalk.html https://blog.metaobject.com/2019/12/the-4-stages-of-objectiv...
- teakettle42 4y ago> Swift on the other had is a language where wonks decided to throw every silly feature they thought off to the point that is becoming more complex than even Scala. What features in particular do you think represent unjustified complexity? Swift is not my favorite, but I wouldn’t consider it to be a “kitchen-sink” language, and it’s certainly less complicated than Scala.
- astrange 4y ago"guard" is kind of useless but it's minor.
- zapzupnz 4y ago`guard` has its time and place, which is to refactor pyramids of doom away. The number of nested `if let` statements I've seen, and written, is upsetting.
- astrange 4y agoPerl's "unless" syntax, or allowing "if"s at the end of a statement, also solve this and are more flexible.
- zapzupnz 4y agoI don't think they quite address the same thing. `guard` isn't just an `if` statement that looks for the negative of a Boolean; it also forces handing control flow out of scope in a consistent way — you cannot write a `guard` statement without also deciding how you'll exit the scope if the Boolean is false. The developer not being made to do so, and just embedding conditions within conditions, is how the pyramids of doom are constructed to begin with.
- TazeTSchnitzel 4y agoNSString *foo = [@"foo" stringByAppendingString:@"bar"]; It's not that bad.
- Xeoncross 4y agoI must be missing something here. "foo" + "bar"
- ranger_danger 4y agoIt's more like compile-time concatening of literals like in C when doing const char *s = "foo" "bar";
- tialaramex 4y agoWhile this syntax is indeed popular in several languages it's deeply weird, it's like how English uses apostrophe S to do two very different things at the end of words. The Add operation has a whole bunch of properties and this not only isn't most of them, it's arguably not any of them, the main argument for + as a string concatenate operator is "Well, people seem to like it". Which, I guess I/O streams doesn't even have that going for it, but it's a pretty terrible rationale. Look, just grow string interpolate and write f"{foo}{bar}" or whatever as much as you like don't abuse unrelated operators.
- lolinder 4y agoWhatever you feel about operator overloading, it's way better than the Objective-C version, even the version floated by the defenders. String interpolation is even better, sure, but the + alternative isn't that bad. As you noted, natural language does the same kind of overloading and we seem to cope with it just fine.
- richarddaystrom 4y agoI use: id foo = [@"foo" cat:@"bar"]; I have a category method added to NSString: - (id)cat:(id)arg { return [self stringByAppendingString:arg]; }
- aaaaaaaaaaab 4y agoActually, I'm glad its a method with a long-ish name. Operator-overloaded string concatenation makes something that can ruin your performance look innocent. Also, I've never heard of any serious iOS/macOS dev complaining about this.
- Apocryphon 4y agoIt's not an issue with autocompletion and Objective-C's verbose naming conventions tend to be more meaningful than those in J2EE, at least.
- wpearse 4y agoI can’t speak for other ObjC devs back when the iPhone SDK was released; but I remember just being so grateful that I had any tooling to develop for iOS that I was more than happy to overlook any ObjC weirdness/clunkiness. ObjC is very simple (often confused with archaic), but also wildly complex. Once you wrap your head around the ObjC way of doing things, everything makes sense, is very consistent, and you could develop very effectively.
- hugey010 4y agoI never felt like this was too terrible... NSString* result = [NSString stringWithFormat:@"%@ %@", @"Hello", @"World"];
- MontyCarloHall 4y agoI honestly can’t tell if the posts defending Objective C syntax are serious or not. Poe’s Law in action? Compare with "Hello " + "world" or if you hate operator overloading "{} {}".format("hello", "world") f"{foo} {bar}"
- astrange 4y agoYour example is worse than stringWithFormat: for the same reason C++ iostreams are worse than format strings. It's hard to control once you want anything more interesting than + and it's not localizable. Complaining that a function call's name is too long doesn't matter at all.
- zarzavat 4y agoIf you think that function name length doesn't matter you haven't written enough ObjC yet. It matters. Source code shouldn't read like Tolstoy. ObjC itself is not responsible, it's NeXT and Apple's fault for perpetuating that abominable naming convention for much too long. As for localisation, only a tiny fraction of strings in a software program are user visible. I don't think we should be designing language syntax around that edge case.
- astrange 4y agoI'm sure I've written more than most other people currently alive. ObjC is one of the most readable languages around. That's mostly because of the param:value syntax, which is much better than C-like syntax because you can see the parameter names. But the long method names aren't a problem once you have autocompletion. They also make it clearer what the best name for a method is - if you call something fmt() you start needing to make up equally clever short names for everything else, and it becomes less principled. For algorithmic string building, I'm not sure how often you want + (aka appendString:), appendFormat: or componentsJoinedByString: are more flexible.
- wpearse 4y agoIt’s been almost a decade since I did any ObjC, but I’m fairly certain that you want to use NSMutableString here :p And, don’t forget to release your alloc’d objects (or use an auto release memory pool). There was no automatic memory management then!