DISCOVERY

January 3rd, 2018

Strings in Swift 3 & 4

Swift

Swift 3

Swift 4

Yesterday I began to fix some bugs in my website saintsxctf.com and its corresponding native iOS and Android apps (because as we know a codebase is never complete). I was looking at the Swift code for my iOS app and found a bug where I incorrectly selected a substring from a base64 encoded image. Luckily the fix was easy to implement, but viewing the code made me remember just how confusing the String structure is in Swift.

You would think something as simple as a string would be straightforward, but in Swift there are a few quirks. I wrote the code for my iOS app in Xcode 8 and Swift 3 this fall. Since then Xcode 9 and Swift 4 were released, making String a bit less confusing. I dive into strings in Swift 3 first and then look at what changed in Swift 4.

DISCOVERY

January 19th, 2018

Swift 4 @objc Annotation

Swift

Swift 3

Swift 4

Today I upgraded my iOS app SaintsXCTF from Swift 3 to Swift 4. The process was extremely easy, with many of the automated conversions consisting of API changes and String struct upgrades. However there was one change that had me confused - many of my functions were given an @objc annotation. So what is this mysterious annotation and why was it added to so many of my methods?

The @objc annotation allows for functions to interact with Objective-C code1. Since many of Apple's APIs are built in Objective-C, a lot of my functions were actually interacting with non-Swift code without me even knowing! This communication between Swift and Objective-C is called Interoperability and it enables usage of Objective-C code in Swift and vice versa2.

DISCOVERY

November 14th, 2017

Sorting Lists with Comparison Functions

Java

Java 8

+6 More

In this discovery I look at sorting lists in different programming languages for non-trivial objects. The languages I use are my core languages: Java, JavaScript, Swift, Python, PHP, and C. I've used all these languages in larger projects and wish to stay proficient in them. Throughout this article I show snippets of code in each language, but you can also check out the full code on GitHub. Let's get started!

Java

DISCOVERY

December 7th, 2017

Native Getters & Setters

JavaScript

Swift

+2 More

Creating getters and setters for private class variables is a common practice in many languages. In Java getters and setters are implemented as two methods on an object instance. Getters and setters are often used in the POJO structure. In JavaScript getters and setters have native support in the language itself. JavaScript getters and setters are created with the get and set keywords. They are commonly used for computing properties, as shown in the next example.

Setters can also perform validation on incoming data, such as type checking1:

DISCOVERY

July 29th, 2018

Method Overloading Across Languages

Java

Method Overloading

+6 More

While working with the object oriented paradigm, methods often need to be overridden or overloaded. These similar concepts are often confused by new developers - in my early days of software development it took a long time to remember the differences. Both overriding and overloading consist of creating multiple methods of the same name. The difference between the two is the scope and situation in which these methods are used.

Method Overloading vs. Overriding

Overloading

Overloading is creating multiple methods or functions in the same scope with the same name. For overloaded methods the scope is a class definition. The difference between overloaded methods is the number of parameters - or for a language with explicit type definitions the parameter types. A programming language is tasked with choosing between the different overloaded methods when they are invoked. Invocation processes differ across programming languages.

Overriding

Overriding methods occurs in object oriented programming when a subclass implements a method already defined in the superclass. Everything about the method signature stays the same - including the number of parameters and the return type of the method. When the method is called from a subclass instance, the overridden method is invoked instead of the superclass method.

Languages such as JavaScript use a similar technique to overriding with prototypal inheritance. The JavaScript technique is called shadowing, in which an object lower on the prototype chain has a method that shares the same name as a method higher up the chain. Methods lower on the prototype chain will block - or shadow - methods with the same signature higher on the chain. Shadowing results in methods lower on the chain being invoked.

DISCOVERY

August 16th, 2018

Groovy Closures vs. Lambda & Arrow Functions

Groovy

Java

+4 More

The thing that really intrigued me during my Groovy first impressions posts were closures. Closures are Groovy objects with similarities to Java lambda functions and JavaScript arrow functions (among others). While enclosed in an object, closures behave like a method which can be invoked and passed arguments. Closures can also be assigned to variables and passed as arguments (they are objects after all). In this post I will look at the basic behavior of closures and compare them to similar constructs in other languages (Java, JavaScript, & Swift).

Speaking about closures often causes confusion since its definition varies across languages. For example, in JavaScript a closure describes the ability of a function to remember its lexical scope (the scope in which it was defined in code) even when it’s invoked outside its lexical scope. JavaScript’s closure definition is different than Groovy’s, although in Groovy closures remember their lexical scope as well1. To reduce confusion birthday context is the Groovy idiom for a JavaScript closure.

DISCOVERY

July 11th, 2018

How Do Regular Expressions in Groovy Stack Up?

Groovy

Java

+5 More

Using regular expressions for pattern matching is a task software developers perform on a regular basis. Although regular expressions still differ a bit across languages, they are standardized to the point where they are language agnostic. However, interacting with these regular expressions differs greatly across different programming languages. In my recent ventures into Groovy, I saw a very unique approach to handling regular expressions. I decided to compare the approach in Groovy to approaches in other languages I often use. This article shares my findings.

Language Agnostic

A concept that is independent from any single programming language implementation. Skills that are language agnostic can be applied throughout the software development ecosystem.

DISCOVERY

December 22nd, 2018

How Languages Enforce Multiple Inheritance

Inheritance

Object Oriented Programming

+7 More

I recently read a book discussing multiple inheritance in Python. Python is one of the few object oriented languages that permits multiple inheritance of classes. Many other languages include workarounds for multiple inheritance. For example, Java allows for classes to implement multiple interfaces. On the other hand, PHP allows for classes to use multiple traits. This article looks at programming languages I use and how they enforce multiple inheritance or available workarounds.

What is Multiple Inheritance?

DISCOVERY

August 18th, 2019

Revisiting Type Equality

Java

JavaScript

+15 More

In this article I'm revisiting the concept of type equality. Type equality is a topic that software engineers learn early on in their careers. Similar to any other profession, it's beneficial to go back to the basics for practice. Professional basketball players practice layups before each game. Professional programmers should work at the basics as well. I spent this past week re-learning type equality in 13 different languages. In the process I've reaffirmed my knowledge and gained new insights. The rest of this article discusses my findings.

The Different Forms of Type Equality

DISCOVERY

November 9th, 2017

Closure & Lexical Scope in JavaScript Modules

JavaScript

API

In JavaScript there are multiple module patterns for creating APIs and separating concerns in code (as of ES6 there is also official module syntax in the spec). In the following code I created an API using the revealing module pattern. The name 'revealing module pattern' comes from the return statement at the end of the module - it 'reveals' functions to outside code.

This module provides lyrics for Taylor Swift songs (because who doesn't enjoy some T-Swift!) The return statement is the public API for the module. All interior details, such as the lyric variable, are hidden. This pattern harnesses the power of closure in JavaScript!