Creatively Concise Coding

July 12, 2024 | Categories: coding



We had an outstanding day learning about parameters and coding to solve the puzzle lessons in Apple's Learn to Code 2 Swift playgrounds this week. I was excited to teach these lessons because I remember how powerful and helpful I found parameters to be in writing concise code when I was learning to code. As a regular part of our Swift coding lessons, I will have a student AirPlay his/her playground so that we can discuss how the student solved the Swift playground puzzle. I do this because I constantly reinforce that there is no one person that will always have all of the right answers or will always have the best, most concise, most expressive code. Consequently, we (including me) have something to learn from analyzing the code written by each other. The dissection of the code and problem-solving strategies used by the student displaying his/her work are some of the best teachable moments I have. I thought the "Placing at Specific Location" lesson/playground would be a really great lesson as it requires passing multiple parameters into a function. I was right. It was a great lesson/playground for precisely that reason. This playground gives students the opportunity to create two functions and use the

world.place

function to pass in three parameters:

(expert, at Column: Int, row: Int)

so that the students can then solve the puzzle by creating a function for the expert to move forward, turn, collect the gem, etc. After creating the functions, students can then solve the puzzle in seven lines of code. This is a typical, expected outcome for this puzzle that follows the spirit of the lesson. Here is an example of a typical answer below:

 

Swift Playgrounds for iPad: Placing at a Specific Location screencast on YouTube

 

However, one of my students, Elliot, decided to take the lesson objective - solve the puzzle - literally and solve the puzzle in as few lines of code as possible. Many of my students take pride in solving the puzzles as efficiently as possible. Elliot took the challenge of code efficiency to a level I had not expected. He decided that the most efficient way to solve the puzzle was to use the new

place

method in combination with the new

atColumn: and row: parameters

to place his expert character on the exact tile of the first gem and have his expert collect the gem. He then had his expert character move to the next gem and repeat the process for all three gems.

 

A screencast demo of Elliot’s code on YouTube

Take a look at Elliot's code here.

Elliot finished coding the playground extremely quickly and showed me his solution. I said, "Brilliant!" He used the code available to him to solve the playground in a unique, efficient, and unexpected manner. Our class was then able to have a discussion about meeting the strict requirements of a specific assignment and learning to program to solve current problems while keeping our code general enough to be used in other playgrounds or situations. I do not think that Elliot's answer was anticipated as a possible solution by the teams at Apple that created this playground; however, working with teenagers often provides adults with a unique way to look at a problem and how to solve it. It is then our mission as educators to make sure that our students understand that you cannot take a shortcut to solve all of your problems (coding or not) no matter how clever, concise, and funny they may be.

 

See a screencast demonstration video to show you how you can record your screen in the Swift Playgrounds app.