4 Ways Apple’s Swift Enhances Safety

calendar Updated July 09, 2024
Glib Dobzhanskiy
Solutions Architect
4 Ways Apple’s Swift Enhances Safety

In Recent years, we have seen an uprise of new programming languages, and even new generations of programming languages. Throughout the web, programmers are hyping up these new languages, such as Rust, Go and Elm.

Joining in on the fun, Java world has also actively discussed new languages for JVM like Scala, Clojure, and Kotlin. The main point of this discussion is bringing functional flavor and add some sugar (syntax sugar I mean) into Java.

Apple can’t stay away from this trend, either. Realizing that Objective-C has become too mature, the company began adding improvements like ARC and Block syntax, but it was not enough – the language simply looked too verbose. The new hardware on which the code would run had also become multicore and programming for multicore architecture requires safety parallel programming.

So a year go on WWDC, Apple unveiled a new programming language: Swift.

Swift is a multi-paradigm language. Built upon OOP paradigm at the same time it has many functional concepts. But also it implement one more important concept – Swift is a safe language. But what does that mean?

Speaking in terms of computer science, Swift is both a type-safe and memory-safe language. By definition, type safety means that the programming language itself discourages or prevents type errors. At the same time, type memory safety aims to avoid vulnerabilities dealing with buffer overflows, dangling pointers, uninitialised pointers, and more.

That certainly sounds appealing, because these types of errors are most common in a software development. But they are also difficult to find and debug due to the fact that  they are runtime errors.

So how can Swift help programmers avoid these errors? Apple’s new programming language uses a variety of tools, but let’s focus on the most important ones:

Optional types

Definitive initialization

Arrays bounds checking

Arithmetic overflow checking

Optional Types

Option type is not an exclusive Swift feature; there are option types in Rust, for example. According to Wikipedia, option type can be used as the return type of functions which may or may not return a meaningful value when they are applied (https://en.wikipedia.org/wiki/Option_type).

A more colorful way to describe optional type is to imagine it as a box that wraps around our meaningful value, meaning we can unwrap this value right before actually using it. Now, we can declare that our function both receive and return optional value.

Boxes appearance

In other words: get a box and return a box. We don’t care whether this box is empty or contains some type of value., because we know that a function will always get a box. To get real value, we should open (unwrap) the box.

This analogy is a close approximation of the powerful monad conception in Haskell (http://learnyouahaskell.com/a-fistful-of-monads) Maybe Monad in Haskell is a value with a context of possible failure attached.

Unwrapping is always required. We can force this unwrapping by adding ! operator, but the most most powerful approach is to use conditional unwrapping with optional chaining.

For conditional unwrapping, Swift has incorporated an if let construction alongside with ? operator. This process is also called optional binding:

if let constantName = someOptional {
  statements
}

Here’s what the above code means: if someOptional contains value, unwrap it, set its value to constantName, and execute the conditional block.

Optional chaining allows to unwrap inserted optional values. Unwrapping will stop on the first nil (failure) value, and this construction will eliminate numerous if statements. As a result, your code becomes more compact and safe.

if planning?.then(analyzing)?.then(coding)?.then(testing)?.then(packages) {
 nbsp;statements
}

Definitive Initialization

In programming, variables are only safe when we can be sure that the correct value is inserted the moment we actually use it. On the other hand, uninitialized variables are the source of possible errors. Swift implements definite initialisation technique to resolve this source of potential problems. Through it, a compiler checks that each variable should be initialised before using every time during compilation.

Arrays Bounds Checking

Each array in Swift has first and last methods that return optional values. This method ensures that your variable is always within the defined range, and will always return a value, even on an empty array. Of course, these values should be unwrapped as usual in Swift.

Arithmetic Overflow Checking

Integer overflow occurs when the result of operation is a value that is too large to be represented in a current type. Programming code now works on a wide variety of CPUs and even GPUs that all work through different representations of numeric data types, so keeping in mind this potential problem is rather important.

Swift has safe arithmetic operators (&+, &-, &*)  for such cases. These operators will silently discard all overflows, which could be especially useful if you want to truncate bits that don’t fit the current type without throwing error.

All 4 of these Swift techniques allow engineers to fetch out errors as early as the design stage, and improve their code’s accuracy. Option type conception requires the most time to understand and apply in software development, but definite will save debugging time in future. Thanks to these features, Swift is among the safest and error-free programming languages currently available.

Got a project idea?

Master of Code designs, builds, and launches exceptional mobile, web, and conversational experiences.


















    By continuing, you're agreeing to the Master of Code
    Terms of Use and
    Privacy Policy and Google’s
    Terms and
    Privacy Policy




    Also Read

    All articles