‘Hello world!’ in Swift, Enterprise Edition

or Pictorial Overengineering

Nikita Lazarev-Zubov
CodeX

--

Photo by ThisisEngineering RAEng on Unsplash

Let’s start with the classic Hello-world piece of code which is, for instance, provided by Xcode when you create a command line tool:

print("Hello world!")

It works, though it’s not good enough for a serious enterprise. Let’s try and focus on maintainability and extendability this time. First, let’s wrap the call with a function:

func printHelloWorld() {
print("Hello world!")
}
printHelloWorld()

Not bad for a start. However, it’s not good that the output is hardcoded inside the function implementation. Let’s pass it as an argument to make it more configurable:

func printMessage(_ message: String) {
print(message)
}
printMessage("Hello world!")

Better. Let’s then make the solution more object-oriented-y and introduce an object that is responsible for the output:

struct MessagePrinter {
func printMessage(_ message: String) {
print(message)
}
}

let printer = MessagePrinter()
printer.printMessage("Hello world!")

Hmm, the change doesn’t seem very useful, since the struct only wraps the method. Let’s make the output message a dependency:

struct MessagePrinter {
let message: String…

--

--