Adjusting the sails…

Most have heard the quote that I reference in my title.  I remember hearing it at my high school graduation. It’s pithy, memorable, but for a long time, to me, utterly useless.  I had no context to apply it.

That all changed on Memorial Day last month.  For those unfamiliar, the last Monday in May is Memorial Day, a time where we honor the memories of soldiers fallen to defend the United States. For most, it’s a long holiday spent at the lake or a cookout. For others it is remembering loved ones that paid the ultimate sacrifice.

For the past month, I’ve taken a more pro-active role learning delegates and protocols in swift.  Most documentation or how-to videos involved protocols with extension, which provide a default implementation on how to use the protocol.  It’s much harder to find a good tutorial to pass some data from one file to another via a delegate.  I’ve tried to find good examples that help to answer my questions instead of bringing up more.  Much of what I’ve found hasn’t been helpful.  For a couple of weeks, this kept me pretty much at a standstill.

Fast forward to Memorial Day.  It’s a CrossFit tradition to perform the Hero WOD “Murph” on Memorial Day. Michael Murphy was a Navy SEAL who sacrificed himself to get a radio message to help his fellow SEALs.  Part of his story is told in the book & movie “Lone Survivor”.  Murph’s favorite workout when he couldn’t get to a box was a 1 mile run, 100 pull-ups, 200 pushups, 300 air squats, and then finish with a 1 mile run…all while wearing a 20 lb. weight vest.

The-Murph

So I participated in Murph for the first time over Memorial Day. There is nothing glamorous about it, at least from my perspective.  It’s a way to honor his memory as well as others who have fallen to defend the United States.  There is very little fanfare.  We show up, we workout, mostly in silence, and then we encourage others who are still trying to complete the workout.

I’ve been battling shoulder issues for the past few months and was recently allowed to resume so activities that involve the shoulders.  However, I was really worried about the pushups in Murph.  I decided to scale them so as not to face the wrath of physical therapist and endure more therapy (aka torture) at her hands.  So I did pushups from my knees rather than the standard.  At around 120 reps, my shoulders started complaining.  What to do?  End my workout, disappointed in myself and feeling like my contribution towards Murph’s memory didn’t count?   I came really close to calling it.  However, in the midst of the heat, humidity, and mild discomfort (yes, that’s sarcasm), the whole quote about adjusting the sails popped up.  I think there is something spiritual about trying to stretch your physical capabilities.  In this case, I had a moment of clarity.  An “a-ha!” moment.   Rather than just stop the workout, just change what you’re doing.  So I went to sit-ups for the rest of my pushups requirement.   Sure, it’s not the prescribed “Murph” (not that I was anyway as I wasn’t wearing the weight vest), but it’s still a way to honor his workout, and not give up.

So at the hour and twenty minute mark, I finished my final 1 mile run and completed my first Murph.  Of all the accomplishment I’ve had, this wasn’t bound in glory, a sense of accomplishment or any sort of fanfare.  Instead, I felt grateful.  I had the privilege of completing Murph.

The following Tuesday at work, sore from the previous day, I decided to change my approach to learning how to use a delegate pattern with protocols.  Instead of reading and searching for tutorials, I searched for code.  Any sort of delegate paired with a protocol.  How did they use it?  What triggers the delegate?  How do I assign it to View Controller that I need to perform the action.  I stopped waiting to have someone explain it to me, and instead went and found what someone did, and try to explain it to myself.

The end result is that I gained a small foothold in how delegates work.  I’m no expert.  But I was able to accomplish what I wanted, learn to adjust during my learning process, and once again, use a lesson learned in CrossFit in my career.  It never ceases to amaze how I can learn career lessons from a non-career related part of my life.

 

 

What a difference a year makes…

 This week marks a year when I left for Utah to start The Iron Yard Mobile Engineering bootcamp. I remember being worried about not being smart enough or able to comprehend fast enough the knowledge transfer about about Swift, Objective-C and iOS Development. If I’m honest, though, I still worry about that…it’s just the topics are a bit more advanced now than they were then.

Someone asked me the other week, “Would you do it again?”. I answered with an emphatic “YES!”. I took a major chance by quitting a stable job in the hopes of landing one in the tech industry. Corporation, dev shop, non-profit? I wasn’t sure where I’d end up. University never entered my mind though. I’ve now been an iOS Developer for Emory University for 8 months. I’ve helped build two games within the same app. The games could be stand apps in their own right, but we’ve kept them together. We’re also starting the 3rd game this week.

I had the same thought, over and over, as I made the 1900 mile drive out to South Jordan, Utah last year…”What if you make it through the bootcamp, but can’t find a job?” Well, it took a couple months of intense searching, failing (aka learning) at multiple interviews, but that, too, took care of itself.

I’m heading out this week to visit the campus and say my goodbyes to the staff. The Iron Yard – SLC is closing it’s campus so it’s unlikely I’ll cross paths with them very often. I’ll actually be flying out on the same day I left last year, just not with the same worry and anxiety that sometimes overwhelmed me then.

Donuts, catching up with my instructor, seeing the campus director (and beating him at ping pong!), visiting with some of my cohort (if they come visit!), experiencing the gorgeous Utah geography and even seeing my parents, who just happen to be out there as they RV their way through the country are all in my plans. This is definitely a low-stress trip compared to what I embarked on last year.

I don’t think much about the work I put in to get where I am. When you enjoy something, it doesn’t feel much like work. It feels like legos. I used to sit for hours and build stuff out of legos. Whatever my mind could think up, I’d try to build. The same holds true for coding. I put in 60 hours a week at the bootcamp, 60 hours a week during my time being unemployed, and probably still contribute the same both at work, home, and the volunteer organization I’m a part of. The hours fly by. I love learning new topics. Still, had I put the bare minimum in, I’m certain I’d still be searching for that first dev job.

I have a couple posts lined up in the next week or so regarding stuff I’ve been trying to learn so I hope to post soon. I try to let this blogging thing happen naturally rather than try and force something out there.

But of this week, I’m going to enjoy my time in Utah, recharge with fellow coders, eat some fabulous donuts, and reflect on how fun this journey has been.

Balance…a high wire act

I’ve been working for Emory University for about 6 months now. I’ve certainly grown as a programmer, and my confidence in figuring out problems is getting better. Yet, sometimes, I am still overwhelmed at how much I don’t know.

I received multiple calls from recruiters lately. While varied in how much they’ve actually chosen to read through my resume and learn of my experience, most have been genuinely interested and the positions closely match my experience. It’s flattering if I’m honest.

Six months ago I would have jumped at any opportunity. I was unemployed, desperate for a chance to prove my talents. However, after listening to the needs and the timeline of some of these positions, I politely declined. Sure, being employed makes that easier, but given what I’ve learned over the last five months, it has also made me more cognizant of what I can and cannot do (yet).

There is something to be said, though, for the ability to set my work hours to avoid traffic. The lab I work in has gone out of there way to make the office not feel like an office. The office perks are nice.

Ultimately, though, I’m still challenged and intrigued by the work. Why leave a job you enjoy? I spent enough years working a job I did not enjoy to appreciate where I’m at and what I’m doing.

So balance? I work right at 40 hours a week. I’m able to commute without traffic, enjoy the work I do, leave before traffic gets worse, get my CrossFit workout in and still get home before my daughters have to go to bed. If I take another position, I may have to give up one or more of those things…for what? More money? I suppose as I get older, I tend to evaluate more of the opportunity costs of a higher salary than I used to–that is, what am I giving up? Is $5k more per year really worth an extra 2 hours in traffic everyday? Is it worth reducing the amount of time I get to see my daughters play and tell me about their day? Attend and coach their respective volleyball and soccer practices? No. No it isn’t.

That’s not to say I won’t take another position elsewhere at some point, but I have become more discriminating in what it will take for me to make that jump. There is more to life than work (even though I really do love my work).

Leveling up

My lab consists of four developers, all bootcamp graduates. We have 2 iOS developers, and 2 Android developers. The Android developers are converted from other disciplines, so they have some experience in other areas besides Android. Both are very good back-end developers, having built back-end systems for the lab in both Ruby on Rails and Golang.

My supervisor has suggested I do the same so in the little spare time I have, I’m venturing into Golang and Dart to learn more about back-end and front-end development. That rabbit trail has led me into systems programming, which has historically been done in C. On top of that, I’m still playing with Algorithms and Data Structures so that I can be prepared for the eventual job interviews after my time at Emory in concluded.

Make no mistake. I’m not a front-end guy. I’m not really a back-end guy. But the pragmatist in me understands the appeal to a candidate that is more well-rounded. I’m not trying to leave iOS or Swift. I’m just trying to make sure I have an understanding of how the other stuff works in conjunction with iOS.

It can be overwhelming. Do I look at several things or focus on just one and get really good at it? I tend to go back and forth. “This week I only want to focus on A&DS” or “This week I’m going to spend the first 30 minutes of work on A&DS and then my lunch break on Dart.” It’s not particularly well-organized, I admit. Still, I’m feeling my way around and trying to find what works best for me. Organization has never been a strong suit of mine.

Podcast ranting

I listen to a few Swift podcasts while driving to/from work. Most of these are quite good. A few months ago, I thought I’d found a promising one. It was supposed to be geared toward new developers and the topics were one’s I was (still am) currently learning.

I was please with the first few from this one particular podcast, but around the time of the inauguration, it turned to more of a ranting/preaching podcast on what the podcaster deemed as not fair/ or “here’s how it should be.” Initially I defaulted to his judgment because, here I was, a brand new developer, and his first few podcasts were helpful and informative. Over time, I discovered he had 1 week more experience as a developer than I did. Not that being new at development is wrong. I’ve learned some good things from him. But once he began preaching about how new developers should or should not do this/that/the other, I began to wonder why he thinks he’s experienced enough to make these kind of opinions. Most developers I talk to on Slack are constantly having to adapt to an ever changing industry. His advice runs counter to that logic. It reminded me of the pointless faculty meetings I sat in on as a teacher and were told “Do it this way.” Why? I have a better way. I have a circumstance that doesn’t make your “way” the best way. Why should I adopt it/limit myself?

I reached out to him to ask if he was planning to go back to discussing iOS dev because lately I didn’t get much out of his podcast. He said he was, so we’ll see. I say all that to say if someone with 30 years experience talks about how much she’s changed in the industry and had to adapt, why does someone with 8 months experience feel like he’s got it all figured out? I don’t like limitations. I feel it stifles problem solving and makes my job boring.

Algorithms

I’ve started working through a book called How to Solve it by Computer by R.G Dromey. It was written in 1982. It was recommended to me on the iOS Developer Slack forum as a good starter book for bootcamp grads that need to level up on Algorithms logic. It’s a great read. The programming language he uses is Pascal, but aside from that he walks through the steps on how to solve basic problems. So far I’ve worked through how to reverse a number, how to convert a decimal number to any other base system (octal, binary, ternary, etc…) and how to calculate the factorial of any number. It’s enjoyable without being overwhelming, which I what I got when trying to solve problems out of Cracking the Code or Interviewcake.com.

Functional Programming

My previous posts on closures has led me down this rabbit hole called functional programming. I briefly touched on FP in those posts without explicitly calling it that. But as I read through a book on test-driven development (writing tests to make sure functions work the way they’re supposed to), the author is trying to move away from what he calls “writing swift in an objective-c way”…aka…the way I currently understand…

For example:

return string.characters.reduce(0) { $0 + (vowels.contains($1) ? 1 : 0) }

This is the Swift-y way of writing what is basically a for-loop and an if statement.

What I think it’s saying is this: take all the characters in a string and starting with a value of 0, reduce all the values of an input I give into one single value. Basically, this is starting with 0 and if conditions are met, we will increase that value. At the end of the function(closure), we’ll have our final value.

Ok…so what exactly are we combining/reducing/evaluating? Well, we’re reducing the characters in a string, but the closure part of the expression above specifies what will be reduced. In this case, comparing the characters of the string to some vowels. If this character matches with any of the vowels, increase the value that we started with (0), by 1. Otherwise, if the character does not match with any of the vowels, increase the value by 0.

I have left a few things out to reduce for simplicity. I could have expanded on what $0 or $1 mean, but I haven’t come up with a good explanation yet. It makes sense in my head, but I haven’t figured out the best way to put it in writing. Perhaps the next blog post will cover that.

I’m still here…somewhere.

I suppose my attempt at twice weekly blog posts was a bit ambitious. I’ve been rather busy at work trying to finish up the second game on our project and while I have explored the topics I’m interested in: enums, functional programming, protocols, and still more on closures, I’ve yet to have time to write about it.

Last week began the annual CrossFit Games competition. The Open is available to anyone who wants to attempt it and serves as the qualifying stage for Regionals, and eventually the Games itself. If you’ve read my blog in the past, you’ll know how important CrossFit has been for me in both physical and mental improvement, so it’s an exciting few weeks for me.

The Open consists of 5 workouts, one per week that are announced on Thursday evening. Participants have until the following Monday, 8pm EST, to complete the workout and upload their score, verified by a coach.

I completed 17.1 at Friday Night Lights at CrossFit Loganville. Last year, I had to scale every workout as I was new to CrossFit and couldn’t handle most of the movements required. This year, I was able to attempt the prescribed (Rx) workout. Big steps for me!

If that wasn’t enough, I attempted it again on Monday too see if I could do better which turned into 5 more reps that I had Friday night. The physical and mental gains are always encouraging for me. It rejuvenates my desire to improve, both at CrossFit as well as at work.

So in the spirit of that rejuvenation, I will double down on my efforts to blog more regularly. Certainly the twice a week goal is too ambitious, but I think twice a month is an acceptable and attainable goal.

The next topic will be Protocols and Protocol-Oriented Programming in Swift, as I used them in a Weather app I built, but didn’t really quite get them. So I’ll explain what it’s doing so as to clarify to myself how they work.

Cheers!
-Chris

Closures – continued…

As I’m reading more about using closures in Swift, and then reflecting back upon the closures I’ve used, it leads me to more questions. Down the rabbit hole I venture!

Below is a closure I wrote last week. In fact, it’s exactly what has led me down this rabbit hole. I got tired of failing my way through finding a solution with no discernible reason as to why it worked. I was lost in Swift-land with no roadmap, no compass, and no sherpa to guide me through. I still found my way home, but if asked to repeat the performance, I knew I wouldn’t be able to.

UIView.animate(withDuration: 0.75, delay: 0, options: .curveLinear, animations: {self.cursorLabel.frame.origin.x = nextAnswerX }, completion: nil)

So what I was trying to do was get a label to move across the screen as the user answers each question. This animation is part of a function I wrote that will move the cursor if the user answers the question or chooses to move to another question using the left and right arrow buttons that I also have on the screen.

So, what is going on here? The animate function is pretty clear. I want to animate something. It takes several parameters such as the duration of the animation, if I want the animation to delay before it starts, the style of animation I want (in my case I’m using curveLinear), and then the last two parameters are called animations and completion.

Those two parameters take a closure. The completion closure I have set to nil because I don’t want anything to happen after the animation has finished. The animations closure, though, I have used.

animations : {self.cursorLabel.frame.origin.x = nextAnswerX }

As wordy as that looks, I’m grabbing the x-value position of my cursor label (where it currently sits) and setting it equal to the x-value position of where I want it to be. The nextAnswerX is a reference to another label’s frame.origin.x value as I don’t want to alter it’s state. I want a copy of it. I actually made that mistake in the first part of the closure, but in the opposite way. I made an instance of the x value of the cursor and was trying to alter the instance instead of the actual cursor. It’s similar to writing a letter, photocopying it, altering the photocopy and expecting the original to have changed as well. Sometimes we want the state of the original to change, and sometimes we want to leave it alone.

Now the odd part is that this animation closure doesn’t use words like return or in anywhere in there. Doing some research, I’ve found, courtesy of the Apple Swift documents:

Animations : A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL.

So, it looks like since the block takes no parameters or return value…which looks like this:

(String) -> String
or
(String, String) -> Int

We can eliminate that part of the closure syntax when deciding what the view should be doing. My code is finding the frame that houses the label, then finding the x value of that frame, then setting the x value to a new value.

To be honest, that may be the simplest closure I’ve ever written. I was curious how they set up a closure that doesn’t take parameters and this is what I’ve found.

animations: @escaping () -> Void

So the animations parameters is an escaping closure that takes a function that returns no value (that’s the -> Void part). Upon further research, the -> Void part is required even though the closure doesn’t return any sort of value. If the closure just had the (), Swift would think it was empty tuple.

The escaping part means the closure will be called after the method has returned. By that, it means “Hey we might need to use this closure later, so lets hang on it.” This could be because the closure is running on a background thread, or waiting for the user to interact with the screen, or some other reason I haven’t yet discovered. My animation only happens upon a button click, and after looking online, apparently happens after the function is called, so it has to be escaping. I’ll admit, I don’t understand that fully right now.

Update: I saw this in the Apple documentation about closures…

One way that a closure can escape is by being stored in a variable that is defined outside the function.

So if I have a variable holding a closure, and I place that variable into a function that accepts the closure, that function will need to be set up to have an escaping closure so I can access the variable later after the function has been called. I don’t think that’s technically what is going on in my situation as I’ve just written out what I want my closure to do inside the animation parameter, but it is the first time that I’ve seen documentation that makes sense to me on when to use the @escaping syntax.

I’ve now gotten to the point where the information I’ve read is a repeat of other information, so I don’t have anything new to add, but suffice to say I’ll be paying more attention to completion handlers and other parameters that require closures to get a better idea of why some have to be escaping and others do not.

As far as the syntax goes, I understand better what is happening, and the short-hand syntax doesn’t unnerve me as much when I see it. I just need to keep writing them the way I’m comfortable until the shorthand becomes second nature. For now, I’m all about code readability.

#fiyf

Closures – my interpretation

I’m going to try and explain to myself how closures work.  This is a normal departure from my usual blog posts as it will be more code focused–which was the original point of my creating this blog.  So my goal is to twice a week, blog about a specific programming topic I’m trying to learn or use, or would like to use in my programming.

So a closure is a function–without a function name.  I write functions all the time.  They’re easy to understand as the function name generally describes what it does.  For example:

func double(input : Int) -> Int {
        return input * 2
    }

This function takes any sort of integer and sends back a value of that integer multiplied by two.

Now, I can use that function as a standalone by calling it…


  double(8)

and it will return a value 16

What if I want to use that function as a parameter or argument in another piece of code?  What if, for example, I want to take a number and divide or multiply by 4 or add 16 to it?  I could write those individual functions similar to the one above, or I could write one function that allows me to customize what I want to happen to the input on the fly.

If that’s confusing, it certainly has been for me.  I’ve used closures and never really understood them.  The code I used was almost always written by someone else and I trusted it worked.  But as I write them more and more, I have this desire to understand why it works.

So I want a function that I can reuse and that I can alter what happens to the input (for example, an integer) at any time.   Basically,  I want my function to accept a number from the user, and then manipulate that number in any customizable way I want; basically, i want to take that integer, modify it, and return that modified integer to be used elsewhere in the function, and return another value.

func modify(input : Int, byApplying f : (Int) -> Int) -> Int {} 

So the stub of the function I have above (which I borrowed from ‘A Swift Kickstarter’), takes a number from the user which I have named input.  byApplying is a placeholder name that Swift 3 has now incorporated to allow the user to understand the context of the argument.  byApplying is basically a nickname for what follows after it — in my case f (shorthand notation for me, the programmer to indicate a function) of the following data type (everything after the colon).

(Int) ->  Int 

In my instance, my function needs to take an integer type and will return an integer type.  Once that argument has performed whatever I need it to do, it will take that answer, and return at the end of the modify function, which is denoted by the return f(input) below.

The first time I saw this, I was confused.  Wait a minute…I’m returning a parameter from the function?  Short answer is yes.  I’m taking the second parameter of the modify function, putting the first parameter into it and returning the value.

So if we look at the original double function from above, we could do something like this…

modify(3, byApplying double) 

So what is happening, is double is taking the first parameter, 3 , and using it inside of it’s function.  It then returns the answer.  Since double is supposed to take an integer and multiply it by 2, it’ll return the value of  6 .

So, for most people, that would work fine…if all you needed to do was multiply by 2..and there are certainly easier ways to accomplish that.  But what if you needed to customize that double function on the fly?  You still want to take an integer and return an integer, but the math involved may change depending on what you need.

You could write other functions and pass it into the byApplying parameter…for example:

func addSeven(input : Int) -> Int {
    return input + 7
} 

and then call the addSeven function in the modify function…

modify(14, byApplying : addSeven)

and it would return the value of 21.

Or you could write the logic of addSeven or double or divideOneHundred directly into the modify function.  Basically customize it as you see fit.

This is where a closure comes in.  it’s basically a function without the function name.  Instead of naming it, you just wrap what you want the function to do inside  of curly braces { }. You also write it where the second parameter of the modify function should go.

modify(14, byApplying: { (input : Int) -> Int in return input + 7 } )

So what is happening here?  Well, the addSeven function took one argument, input, and returns an Int.  The closure above does the same thing, hence the (input : Int) -> Int part in the first half of the closure.  It matches exactly with the argument and return value in the addSeven function.

The ‘in’ part of the closure is what separates the expression in the first half of the closure with the body in the second half.  Basically, the closure reads: “input is an Integer which returns an integer and I’ll use input’s value IN the following bit of code….which in our case is take the input value and add seven to it and return (send back) that value.

Now, that finally made sense to me once I started using closures more, but it was never intuitive.  I had to use the closure, trust it worked, and then the more I used it, the better I became at figuring out how to ask the question on why it worked–a backwards process to be sure, but sometimes code is like that.  It also didn’t help that most of the closures I saw first looked like this…

modify(14) { input in input + 7 } 

or

modify(14) {$0 + 7} 

tumblr_m5wdv30ps91qb7gbyo1_500
They mean the exact same thing as my earlier closure.  They do the same thing.  They just look different.

So this…   

modify(14, byApplying: { (input : Int) -> Int in returninput + 7 } ) 

is using the two arguments in the original modify function, input & byApplying.  However, in Swift (and probably other languages), if you’re last parameter is a closure, you can just delete the byApplying part, close the parenthesis after the input part and write your closure after it…this is called a trailing closure.

becomes this:  

modify(14) { (input : Int) -> Int in return input + 7 }  

but we can take it a step further.  Since Swift can infer (understand) the data type you are using, in this case an Int that returns an Int, the code…

now becomes this:  

modify(14) { input in return input + 7 } 

Basically, it knows that the input parameter is an Integer.  It also knows the closure returns an Integer so we just lop that bit of extra code off.

If the closure only contains one expression, like ours does..i.e. input, we can also lop of the return part…

now becomes this:  

modify(14) {input in input + 7} 

The $0 part is new to me and I have yet to come up with a good explanation or demo, but it basically just means that $0 is the first parameter in the closure (i.e. input) and lop off the ‘in’ so it becomes:

modify(14) {$0 + 7 } 

So for me, who is just now getting the hang of it, I will probably stick with this

modify(14, byApplying: { (input : Int) -> Int in return input + 7 } ) 

or this

modify(14) { (input : Int) -> Int in return input + 7 }  

because the extra bit of code helps explain to me what is happening.  As I get used to closures, and begin writing my own, then I’ll start using more short-hand syntax.

It’s like any other language you learn.  Slang is picked up after you already understand the language.  I’m going to hold on using the swift slang until I feel I can converse normally in it.

If you don’t understand closures, I understand you pain.  I don’t really understand them either, but I want to understand them, so it does provide me with some motivation to learn them.