Development

How Lessons to Speed Up Swift Can Work With Other Programming Languages

19 Jun 2015 12:07pm, by

The Udemy online academy recently published a Top 10 Tips to Speed Up Swift Learning, and here I’m looking in depth at five of them, but in the wider context of including other programming languages, not just Swift.

1. Use Type Inference

The better programming languages have shorter and simpler syntax that makes source code easier to read and understand. For example, Java, though popular, doesn’t have type inference. When you declare a variable you have to specify the type, even if you assign an initial value. Using type inference increases code readability.

The example below is from the AWS SDK for Java on GitHub. Languages that have type inference include ML, OCaml, F#, Haskell, Scala, D, Clean, Opa, Rust, Swift, Visual Basic (from 9.0), C# (from version 3.0) and C++11.

In Java, it’s:

In C# it’s this shorter version:

The IDEs need to be able to infer the type from the right hand side, and, of course, you can only use type inference when there is a value to assign. If you declare fewer uninitialized variables, you’ll have fewer bugs.

2. Value Types Are Passed by Copy

From C onward, many programming languages have passed value types by copy into functions. Value types are usually simple types, like ints and floats. The opposite types are reference types, such as strings and instances of objects. Reference types are often 32 bits long (on 32 bit architectures) and point to (or refer to) a block of memory elsewhere.

When you pass a value type variable into a function call, like in this C example below, the value in variable b is passed in to variable a by copying. Inside the function dodouble(), the passed-in value in a is multiplied by 2, so it has the value 20. The external variable b, though, remains unchanged, and still has the value 10 in the print.

3. Make Things Generic

One of the principles of programming is D.R.Y. — Don’t Repeat Yourself. One way to do this is use generics (aka templates). Say you have a function to sort ints. Instead of writing an almost identical function to sort floats, you convert it to a generic function that works with any sortable type. Then you call the function for a particular type, say an array of floats, and it sorts it irrespective of the type, as long as it’s possible to compare two elements of the same type.

This example works with any value type T, and sorts the array arr in place using BubbleSort. The where T : IComparable restricts the use of this to a type T that implements IComparable. You can add this to any type by adding a method int CompareTo(T t) which compares t with this, and returns -1, 0 or 1, depending on whether the comparison is less than, equal to or greater than. In this example, BubbleSort is called twice, once each on an array of ints and an array of floats. These have implicit CompareTo, so you don’t need to specify it.

Normally, I wouldn’t ever use bubble sort as it’s inefficient, but it serves to illustrate the point.

4. Representing the Lack of Value: Optionals

Swift has a very neat way of dealing with reference types that can be null, and value types that can’t be null. It lets you wrap both types with an enumerate value, called an optional. Some other programming languages support similar concepts. Java has wrapper classes for primitive types, and C# has nullable types. The next version of C# (6.0) has Swift-like checking of nullable types.

This reads in the file program.cs. If this fails, perhaps because the file is locked or absent, then the variable lines will be null and the null coalesce operator ?? will return 0.

It simplifies the syntax, removing the checks for null, and makes for slightly more functional-like programming.

5. Tuples

Languages like Swift, Java, C#, Lisp and Python support tuples. A tuple is an ordered set of values of the same or different types. In C# it’s a class, but unlike a class the fields aren’t named. It’s also not like a List, as the fields aren’t the same type.

A common use is to return a 2-tuple from a function call, with one of the elements being an error code and the other a computed value. Or a tuple can hold a single data set, with the components representing individual fields. In C# there are predefined constructors for one to seven values, with an eighth parameter that can hold more tuples, making, in theory, any number of tuples possible, albeit a bit clunky.

In many languages tuples are immutable, though in Swift you have to specify it using let for read-only instead of var.

Conclusion

These programming tips all apply to Swift, but all of them were drawn from existing programming languages. It’s quite often the case that having mastered one programming language, others are easier, as you’re just filling in the gaps.

Feature image: “Star trails at Hopkins Prairie, Ocala National Forest” by Ryan McKee is licensed under CC BY-ND 2.0.

A newsletter digest of the week’s most important stories & analyses.