Why Teach Swift?

July 12, 2024 | Categories: Swift



 

I was at a conference last July, and one of the other coding teachers said that in order for him to teach coding using Swift in his country (Poland) that he would have to justify, " Why Swift?" Initially I thought, "Well, why not? Apple has created this new language. They make great hardware and software for us to use when we teach." Now that I have had some time to reflect upon that simple, but powerful question, I think that I have a much better, more well-reasoned answer. Note: The following may be a little nerdy at times, but I will do my best to be in teacher mode the entire time so that the explanations are a simple and understandable as possible.

 

New

Swift is a new language that is only in it's third year of public existence and use. It is a modern language that has the benefit of being able to incorporate some of the language features and syntax of other languages such as Haskell, Rust, Java, Ruby, C#, and Objective-C1 that computer scientists have evolved in the past 40 years. This means that Swift has, as a starting point, the power and constructs of these languages with the addition of newer features and ideas in computer science such as promoting Swift as a Protocol-oriented programming language.

 

More importantly for teachers, our students will grow, learn, and develop as coders/programmers as Swift continues to develop and mature as a language. Our students will be able to contribute to the evolution of the language and its features by subscribing to and participating in the Swift Evolution group and mailing list. Current Swift programmers are already making substantive suggestions and contributions to the development of Swift. One such example was the suggestion by Erica Sadun to remove i++ as a way to increment a variable because this is a holdover from C and Swift offers us a better way to increment a variable by using addition assignment operator +=.

 

Expressive

An expressive programming language means, for me as a teacher, that it is a readable language. This is one of the great features of Swift. It is, in my opinion, the most readable programming language that I have ever seen.

print("Hello world!")

Just as one might imagine this line of code will print out the statement: 

Hello world!

codePhrase.append(Character("!"))

This little bit of code will add the exclamation point to the end of the codePhrase text.

orangePortal.isActive = false

This code snippet (from the  app Learn to Code 2: Types: Corners of the World tells us that the orange portal in the puzzle is not active, so that Byte can walk across it without being transported to another part of the puzzle.

 

Since Swift is such an expressive language, it has some built-in advantages over other languages such as Java.

  1. It will be easier for students to write Swift code because it is similar to how they have written English in their Language Arts classes for years.

  2. Swift is easy to read. Thus, one will be able to comprehend what the code represents because it is close in its syntax to the syntax to our natural written or spoken English. Consequently, students/developers will more easily be able to determine the purpose of previously written code or code written by another person when working as part of a team.

  3. The expressiveness of Swift will also enable better and more constructive collaborative efforts by students/developers because it will be easier for students/developers to iterate over their own or someone else's code to try to achieve the desired functionality of the code.

 

Open Source

Swift was open sourced in December 2015, which means that Swift code can be ported to other operating systems such as Linux, Android, Raspberry Pi, etc. Currently, Swift has been ported to Linux and an attempt is underway to port Swift to Android . The open sourcing of Swift is really, really exciting for developers. At some point soon students/developers will be able to develop software for all three areas of development - app, server, and web in one language. Previously, a developer would have to learn/know three to five different languages (Objective-C or Java or C#, PHP or Ruby or Go, and JavaScript or Perl, etc.).

 

Swift enables students/developers to develop for the previously mentioned three areas of development (aka full-stack development) in one language! Instead of having cursory knowledge of multiple languages, students/developers will be able to gain a deep knowledge and mastery of one language. I think that the ability to do all development in Swift will lead to students/developers creating some great software that is both powerful and intuitive because they will be able to write and factor their code for the front-end (apps) and back-end (server) to work better/more efficiently together. This greater efficiency will be the natural result of students/developers writing exclusively in one language: Swift. The efficiency gains will be the direct result of the front-end and back-end code being better able to interact with each other because their functions and actions will be written and factored in a similar (if not exact) manner.

 

Safe

There is no such thing as perfect software. Most software developers strive for this goal, but ALLsoftware has bugs. The only question is whether those bugs have been encountered by users of the software. There are a few features in the Swift language to help developers get closer to reaching the goal of bug-free software. Swift accomplishes this in a few ways2. One of which is that Swift (for our purposes as teachers) eliminates pointers (technically Swift just does not expose pointers)3. A pointer is code that refers to (points) to another block or piece of code for data or an action. If that code the pointer is looking for is null (has meaning no value), then the code could (and most likely will) cause unexpected behavior such as a crash. One way to think of this is to imagine you are playing Hide-n-seek with your friends. However, in this version of hide-n-seek you must find your friend Joe in order to change your role from "seeker" to "hider" - and we all know that hiding is much more fun than seeking. Next, your friend Joe must leave the game before you find him and he doesn't tell anyone he is leaving the game. This means that you are looking for someone in the game that does not exist - unexpected behavior. This means that you will keep seeking for Joe until you collapse in exhaustion (a crash) or you will keep looking for him forever (an infinite loop). This is what happens when a pointer's reference is null. This specific problem (and other problems) with pointers is eliminated by language design. One such way Swift eliminates this type of null pointer problem is with a new language feature in Swift: optionals. Optionals are a way for you to tell your program, "Look for Joe, but he might not be part of the game." Joe's participation in the is optional, and if he is not in the game then you could write a conditional statement to say, "If Joe is not part of the game, then find someone else playing the game."

 

A safer language is significant for new learners and developers because this should result in our students/developers spending less time writing unsafe code that results in errors, or when they write that unsafe code they will know immediately. The immediacy of knowing that the student/developer has written unsafe code (or not writing in the first place because of the design of Swift) will directly result in the student/developer spending far less time debugging their code. Less time debugging will then result in less frustration and a better coding learning experience. Consequently, students/developers will be able to devote more time to creating more awesome and interesting features and thus will be a better experience for end users.

 

Great Tools and Curricular Resources

Swift Playgrounds app for iOS

Apple has developed some awesome Swift teaching tools for both iPad and Mac. On iPad the Swift Playgrounds app has fun and challenging puzzles that engages students in learning Swift without being stressed by having to only having code presented to them and thus immediately having to learn syntax. Alternatively, students are engrossed in visual puzzles that draws them into learning Swift by actually touching and physically manipulating the code through the touch interface of iPad. This is not to say that the Swift Playgrounds app is simple or just a game. The Swift Playgrounds app for iPad scales up to introduce more complex topics such as loops, variables, parameters, and eventually even the dot.syntax and types. Most importantly, the Swift Playgrounds app empowers teachers to introduce Swift and computer science to students as young as late-elementary and middle school in more genuine coding experience that resemble what working developers do everyday.

 

Swift Playgrounds on iPad screencast on Vimeo

 

App Development with Swift

The App Development with Swift curriculum (Teacher) (Student) and tools is a Mac-based approach to teaching Swift to high school students who are ready for a completely code-based approach to learning Swift. All of the Swift language concepts are presented in Swift Playgrounds on the Mac in a simple, easy-to-use interface. High school students get a deeper-dive into Swift, plus students will have the opportunity to get to learn . If I were to develop a Swift curriculum for Mac, this is exactly what I would have done. Students learn Swift in Swift Playgrounds so that they can focus on learning the language before they are presented with the full Xcode. Xcode is really powerful, but complicated software and delaying the students exposure to it will necessarily prevent a layer of complication that is not beneficial when learning a new programming language. Students do eventually work up to developing an app in Xcode by the end of the course.

The Swift curricular materials created by Apple are fantastic! They enable teachers with little or no computer science or coding experience to start teaching Swift to their students with just a little bit of learning and preparation. Furthermore, teachers with previous experience in coding or computer science will be able to provide their students an even more rich and engaging coding education experience by following and augmenting these materials with their own materials.

 

App Development with Swift Curriculum screencast on Vimeo

 

Swift has a thriving and vibrant developer community that can be seen in the previously mentioned Swift Evolution group and Swift Evolution mailing list discussions. The response and excitement to the introduction to Swift has far exceeded Apple's expectations[^3]. This kind of excitement in a language coupled with success of Apple in selling products that are in high demand translates to great Swift developer demand now and in the future in the tech and related industries. I teach in career/tech, and those high-excitement and high-demand career fields are exactly what we look for when we advise students. I cannot wait to see what our students, the next generation of developers, will create in the future!

 

References:

  1. Chris Lattner (June 3, 2014). Chris Lattner's Homepage.
  2. Lattner, Chris. "People Don't Use the Weird Parts." Interview. Audio blog post. The Accidental Tech Podcast. Marco Arment, 17 Jan. 2017.
  3. Swift (programming language)