Swift & Network programmability, a good combo? An introduction.

Swift is commonly known by iOS and MacOSX Software developers as Apple introduced the language in 2014 for MacOSX, iOS and Linux application development.
In my role as software engineer I’ve used different programming languages to build small tools, solutions or prototypes. For network programmability I’ve used Java as my primary language. I have my reasons, which I might share later in another post.

Network Programmability on the web pretty much evolves around Python. Is Swift mature enough and powerfull enough to be used for programming the network? Time to write up my experiences in a blog series. The first post is an introduction to Swift.

Warning! There is some Swift Code in this blog post. It is ok that you do not understand the code yet, I will explain what the small piece of code is doing. In another post I will explain the fundamentals of Swift

The Swift programming language is currently at version 4.2. Swift itself is open source since 2015 and there is already quite some code and examples on Github and the Internet. Swift is an object oriented language that takes its heritage from Obj-C, the language that was used before to develop on macOSX and even earlier the Next system
Swift is a compiled programing language, e.g. the code you write is compiled into binary code that can be executed on the device. But it is possible to run swift in an interpreted mode, which is called Swift Playground. It allows you to quickly test out code and refine your code without recompiling every time you make a small adjustment. Swift itself is designed and build around a three key concepts, that will help you as a software engineer.

Safe

Swift is designed with safety in mind. That can sometimnes be very annoying to you as a developer, but in the end it really provides more efficient code with less bugs. An example:

A few words of explanation. First of all, there are no semi-colons necessary in Swift. You can type them (old habits can die hard), but they’re not needed. This code example assigns a variable myString with the value “Hello Reader” and then tries to assign a new value and print the value of the variable. As you can see, there is no type decleration necessary, Swift (compiler) recognizes that this is a string. So no need to type that.

However, talking about safety and type-safety, this code will not compile and the compiler will provide an error telling you that the variable myString cannot be changed. And the compiler is correct. The keyword let tells the compiler that the variable is not adjustable. So you cannot assign a new value to myString.
The code will be executed if I change the keyword let to var:

This type safety is convenient to prevent a value to something which actually should not occur. Another type safetycheck is the usage of optionals. In general, if you define a variable, Swift checks if you have set a value to that variable before you call the value. So the following code will provide an error

The Swift compiler will complain about the fact that you want to print out a variable without a value. If you do want to make a variable optional, you can use the ? To specify that the value might not be set.

And still the compiler will give an error. Because I’m using an optional variable, but I haven’t mate it explicit in the print. You need the ! for that. The following code will compile

However, the output will be nil, because there is no value to the variable. So even with this strict type and value checking, you can get a nil value. Usually in code, you test for a nil value and if the variable exists, you execute some lines of code. 

There are many other mechanisms inside Swift that help you to reduce errors on types, values and other attributes. 

Fast

Speed is of course important, specifically as Swift aims to replace C and objective-C. I will not go into all the details of how it was tested, but I ran into this presentation given by Ian Portridge at Jax2017, a Java Developer Conference where he showcased the possibilities of server-side swift. If you know Java a bit, this presentation also gives you a good introduction into Swift as well.  Within this presentation Ian shared some test results of programming languages

[Best_Wordpress_Gallery id=”14″ gal_title=”Swift-Speed”]

The diagrams above speak for themselves, Swift is fast. And I agree with that, an app that I created runs through 800 kb of configuration lines in under a second.

Expressive

I know quite some programming languages and I do like programming languages that are powerfull but also so easy to use that it allows me to quickly convert my flow-thoughts (what should the program do at which moment) in a quick and easy way. That’s probably one of the reasons why I have a preference for Java (nice object-oriented), Omnis (a fourth generation english like programming language or FileMakerPro (database solution with powerfull scripting options). 

It is clear that Swift has taken the things from other programming languages that work and leave those that do not work. That makes it difficult to transition sometimes (between versions) but the language is evolving in the good direciton. Let me share some things that describe the expressivity of Swift.

Safety & if statement
As I mentioned, if there is a variable, Swift’s safety ensures that the variable must have a value. Unless you make the variable optional with the question mark. But what if that variable doesn’t have a value and you use it. You get an error. So a common programming principle is to first check if the variable is set, in Java this would be.

// Check if the variable is set
if myvariable != null {
  // check if the variable has value 10
   if myVariable == 10 {
      System.out.print("Variable is 10")
   }
}

Within Swift, these two if-statements can be combined into a single statement:

In this Swift statement, I use the “let” keyword to assign the value of myVariable to myNewVariable. And only if that is succesfull, the check is done on myNewValue. And I can print do what I need. This expression helps you to minimize the nesting of if-statements. 

Returning two values from one function
Another example that I do like is returning a tuple of values instead of a classic value. Traditionally you would need a special class, create that class and then return the value. In swift, I just specify that I want to return a tuple of values and return it. I can then check on both values with the return. This mechansim provides quite some flexibility in returning for example an error code (Integer) and the response of a REST-API call or some other value. The code would be

If I execute this function, I can check on the result value and then process the response. And I can use that result and response label in my code, so the code remains readable.

Array’s
I also found that the Array object within Swift is actually very powerfull (that is because of generics, but that is truely an advanced topic). It is really easy to search through arrays within Swift. I will give just one example:

So this short for loop immediately has a check and matches on the where statement, so that the for loop is only entered for every command (within the ciscoConfigLines) that matches the where clause. For me that is really expressive.

Another example is to make an array of classes that each can have other attributes as well. It is really easy to loop through those arrays and execute tasks on the value.

Summary

Swift is a mature programming language, it is very popular for IOS and MacOSX development and has become one of the top programming languages. Swift also has an extensive framework for user interfaces and network communications, so the combination could have you automating steps of your network and do that with drag-and-drop from an iPad!
Swift runs on Windows(haven’t tested it myself), Linux and Mac. I prefere to develop in Xcode, the Integrated Development Editor (IDE) developed by Apple. But you can use any editor you like, and use the command-line compile and interpretation tools.

In a next post I will explain some basic language constructs, such as variables, controls and loops within Swift.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
25 − 6 =


This site uses Akismet to reduce spam. Learn how your comment data is processed.