More Problems with Object Designs
Wednesday, April 3rd, 2019This morning I was playing around with the CryptoQuip solver, and realizing that the mapping code I had could be made a little simpler if I used the fact that ASCII character representations are all numerically ascending, and that makes character math something to use - as opposed to simple look-ups. For instance, if we look at the existing code for +createPatternText::
{ const char *src = [[text lowercaseString] UTF8String]; NSUInteger len = [text length]; const char *ascii = "abcdefghijklmnopqrstuvwxyz"; char pattern[255]; for (NSUInteger i = 0; i < len; ++i) { } pattern[len] = '\0'; }
the look-up on line 8, using the const char array defined on line 5, is really just an offset calculation, and can be replaced with:
{ const char *src = [[text lowercaseString] UTF8String]; NSUInteger len = MIN([text length], 255); char pattern[255]; for (NSUInteger i = 0; i < len; ++i) { } pattern[len] = '\0'; }
and the result is exactly the same. But now, we're not creating the const char array on the stack, on each call, and the offset into the array is the same addition as we are doing here. Simpler.
But when we go to Swift, we see that the beauty and elegance of the underlying data is being clouded with the all-too-common object oriented design of Character.
var pattern: String { get { let ascii: [Character] = ["a","b","c","d","e","f","g","h","i","j","k", "l","m","n","o","p","q","r","s","t","u","v", "w","x","y","z"] var ans = "" let src = Array(self.lowercased().utf8) for c in src { ans.append(ascii[src.firstIndex(of: c)!]) } return ans } }
Sadly, we can't simplify line 9 because the Character object doesn't allow for construction based on an ASCII value. Sure, it has all kinds of nice interrogation methods to test and see if it's ASCII, or uppercase, or a number... but you can't actually do the simple math of adding 1 to 'a' and getting 'b'. That is a shame, and it's not something you can easily add because the Character class would need a new -init: method.
So while I really like parts of Swift 5, I think they didn't really think about all the uses that ObjC fits into when making the new classes. It's a process, I get that, but it's causing folks to write code around these issues, and that's a mistake. No one will go back and fix their code when, and if, they add a way to do this.