November 24, 2023
A small story about relationship and coding
As a married couple, my wife and I are constantly trying each other’s field to be able to better understand each other. Lately, as I am trying to do more housework, my wife is beginning her journey to programming.
To help my wife, I set up her MacBook with Swift Playground. It has a cartoony interface and simple objectives like collecting gems and toggle switches, it makes learning more like playing a game. And it introduces you to the basic concepts of programming, great for starters.
Then came last week, she was introduced to the concept of function.
For those of you who are unfamiliar with programming, a function is “a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.” (the definition taken from Wikipedia).
You can turn right by turning left 30015 times (or 3 times)
When she completed the task, I helped review her code as always.
The problem has 4 lines, each has 2 gems and 2 switches, and expects the learner to make use of function and loop.
She has a function named goToCollectGems, which is looped to run 4 times. So far so good.
But that function do: tell the character go to the nearest gem’s position, collect the gem, go the the next gem’s position, collect it (so far so good); then turn around, go back to starting position, then go to the nearest switch, then toggle the switch, then go toggle the next switch, then back to starting position, then go to the beginning of the next line.
I told her that it is not good to make a function do more than what is written in its name. She was confused to hear that, she said: “But I expect you guys to know better than what you are told to do”. That is when something suddenly clicked for both of us.
- Ah, no wonder you always lack the ability to understand my expectations. - said my wife.
- Now I know why you always group a lot of tasks to your function. - I told her my realization.
We are both not wrong, we just have different ways of thinking. She, being my wife, expects me to understand and do everything with only a hint from her. I, as a programmer, expect everything to be clearly and exactly defined, so they can be applied and followed correctly.
I explained to her that functions are to be reused, and should do exactly what it is expected to do, no more, no less. “Maybe next week you will call that function again, and because you have forgotten what it does, you will then make the character move unnecessarily”, I said, she agreed.
Her code after fixing my review (it reminds me of my high school time)
I also told her next time when she tells me to “go out for dinner”, I will also budget the time and money for moving, having dinner, strolling around, shopping and watching a movie.
- Yeah, that’s the way - she said.
A snippet of “my code” after fixing her review
Image source: https://www.freepik.com/