Refactor Code, Refactor Mindset

Nick Rizk
4 min readJul 30, 2015

--

Originally posted on nicolasrizk.com — February 17th, 2015

After receiving my letter to join the Flatiron School’s iOS Development Program, I immediately cleared all expectations for what was to come in the next few months. I wanted to come in with a “tabula rasa” mindset and instead allocate my efforts to focus solely on preparing for the program.

I’ll start the story on Day 3, when we had to channel our inner Alan Turing to work on a lab that encrypted and decrypted messages using ASCII codes (Imitation Game was one of my favorite films of the year). As seen below, everything on a keyboard can be traced back to a certain number, or ASCII code. For example, “a” is equivalent to 97 while “A” is 65.

A voice inside my head kept reiterating “just make it work,” and to worry about it looking pretty later. I ended up “making it work” with a convoluted mess of conditional statements (20, to be exact) and went to the instructor’s table to talk it over. I told them that for the last 45 minutes I’ve been using my time searching for one measly closing bracket that wasn’t allowing my method to run. Instead of offering advice on how to find it, they advised that it’s time to practice refactoring my code now that it passes all the tests.

What I learned: the difference between Day 3 me and Day 4 me was huge. Rather than 20 if-statements, the next day my method was 20 lines in total, which I have pasted below (just for fun I also posted my original version at the bottom of this post, but feel free to ignore that):

[code language="objc"]
//
-(NSString *)encodeWithMessage:(NSString *)message andOffset:(NSInteger)key
{</code>
NSMutableArray *splitMessage = [self splitMessage:message];NSRange upperCase = [self caseifyRange:65];
NSRange lowerCase = [self caseifyRange:97];
NSMutableString *updatedMessage = [[NSMutableString alloc] init];key %= 26;if (key == 0) {[updatedMessage appendString:message];} else {for (NSInteger i = 0; i 122 && isLowercaseLetter) || (asciiCode && isUppercaseLetter)) {
asciiCode -= 26;
}
}
NSString *answer = [NSString stringWithFormat:@"%c", asciiCode];
[updatedMessage appendString:answer];
}
}
return updatedMessage;
}
[/code]

The difference between the actual code is clear, but it stems from an even greater distinction: my mindset. In the first, I aimed to regurgitate everything that I had learned during the Pre-work and first 48 hours on campus. Whatever I thought would help I’d just spew onto Xcode. My aim first and foremost was to make it work, but I had taken that to the extreme with the 20-conditionals monster that was currently sitting in my .m file.

My thought process in writing the second was quite different. I created a “floor plan” on paper, and thought in terms of what I needed to build, and what materials were most necessary in building what was needed. The tools I began to harness were separate methods that helped the main one create the eventual output. Once I had my tools set up I began to insert them in with some logic that would eventually get me closer to the desired outcome.

It was an eye opening experience not because the new method is written in the most efficient way anyone could write it, but because I cognizantly experienced a change in my thinking, all in less than one day. At that point, I could only feel excited for what was to come in the next 24 hours.


NSMutableArray *splitMessage = [self splitMessage:message];
NSMutableString *updatedMessage = [[NSMutableString alloc] init];
for (NSInteger i = 0; i < [splitMessage count]; i++) {</code>
NSString *answer;int asciiCode = [splitMessage[i] characterAtIndex:0];int modifiedCode = asciiCode + key;int largeNumberKey = key % 26;if (key % 26 == 0) {answer = splitMessage[i];
[updatedMessage appendString:answer];
} else if (key <= 26) {if (asciiCode 64 && asciiCode != 91 && asciiCode != 92 && asciiCode != 93 && asciiCode != 94 && asciiCode != 95 && asciiCode != 96) {if ([splitMessage[i] isEqualToString:[splitMessage[i] lowercaseString]]) {if (modifiedCode > 122) {
int alteration = modifiedCode - 26;
answer = [NSString stringWithFormat:@"%c", alteration];
} else {
answer = [NSString stringWithFormat:@"%c", modifiedCode];
}
}
else if ([splitMessage[i] isEqualToString:[splitMessage[i] uppercaseString]]) {
if (modifiedCode > 90) {
int alteration = modifiedCode - 26;
answer = [NSString stringWithFormat:@"%c", alteration];
} else {
answer = [NSString stringWithFormat:@"%c", modifiedCode];
}
}
[updatedMessage appendString:answer];
}
else {[updatedMessage appendString:splitMessage[i]];
}
}
else if (key > 26) {
int largeNumberCode = asciiCode + largeNumberKey;
if (asciiCode 64 && asciiCode != 91 && asciiCode != 92 && asciiCode != 93 && asciiCode != 94 && asciiCode != 95 && asciiCode != 96) {
if ([splitMessage[i] isEqualToString:[splitMessage[i] lowercaseString]]) {if (largeNumberCode > 122) {
int alteration = largeNumberCode - 26;
answer = [NSString stringWithFormat:@"%c", alteration];
} else {
answer = [NSString stringWithFormat:@"%c", largeNumberCode];
}
}
else if ([splitMessage[i] isEqualToString:[splitMessage[i] uppercaseString]]) {
if (largeNumberCode > 90) {
int alteration = largeNumberCode - 26;
answer = [NSString stringWithFormat:@"%c", alteration];
} else {
answer = [NSString stringWithFormat:@"%c", largeNumberCode];
}
}
[updatedMessage appendString:answer];
}
else {[updatedMessage appendString:splitMessage[i]];
}
}
}return updatedMessage;

--

--

Nick Rizk

Always learning along the way. I write about the journey here and vent about it on Twitter.