DEV Community

Discussion on: Pitch me on Swift

Collapse
 
dillonmce profile image
Dillon McElhinney

Swift is a strongly typed and compiled language, which gives you some fairly robust guardrails against a whole class of bugs. You never get an unexpected null value. A function will never return a String when you were expecting an Int. Basically what TypeScript does for JavaScript only built in at the language level. And because it is compiled, you can get some surprisingly accurate error messages about what you're doing wrong and often you can get one or more suggestions on how to fix it.

So you get a bunch of benefits from the type system, but it doesn't have a lot of the downsides of most statically typed languages. It has excellent type inference so in many cases you don't have to declare the type. It has a solid generics system and virtually everything is designed around a hierarchy of protocols so you can easily reuse logic between types. If you use it long enough you'll probably run afoul of the type system at some point, but in many cases this is a good sign that you're headed down a rabbit hole and you should rethink your logic/architecture, because there is a better solution to the problem you're trying to solve.

Swift is incredibly extensible/customizable. You can create custom operators. You can rename types. You can extend any type. You can provide custom logic for checking equality (==) between two items of the same type, or two items of different types. You get sensible defaults, but at almost all levels of the language if something doesn't work the way you want it to, you can hook in and change it (this applies to the Swift language, not necessarily Apple's frameworks for iOS/MacOS/etc)

And best of all, you don't have to care about any of this stuff if you are new to Swift or new to programming in general. A helloWorld program in Swift will look virtually identical to one in JavaScript. As you learn, have questions, and want to try things you can learn about a given topic and add it to your repertoire

Swift is fun to read. Code in Swift can be very concise and clear. You can see everything you need to know, and it isn't cluttered up with stuff you don't care about. Semi-colons are unnecessary. White-space mostly doesn't matter so you can lay out your code in a way that is clear. You have separate argument and parameter labels in functions so you can write signatures that make sense at the call site and in the implementation. swift.org/documentation/api-design...

Swift has a great community. It is a relatively young language that is still changing and growing over time. Improvements are constantly being made. There are many people out there who love to learn and tinker and teach about what they are learning. It is, at least from my experience, a very encouraging community full of people who are trying to do and be better. Who want to see people succeed. Who are willing to offer help/mentorship/guidance/etc to new folks and people trying to get started.

And, so you don't think I'm blinded by my love of Swift, there are definitely still some rough edges. Strings for instance are handled in a technically correct way, but the interface is really hard to understand without doing a couple hours of reading about characters and Unicode and whatnot. Not super beginner friendly. The asynchronous/concurrent code story has been pretty rough until very recently and the kinks are still getting worked out. And SwiftUI, which is a common framework for new people to pick up right now as it is intended to be simple to learn and use, is a great facade that hides a lot of barely working complexity. It uses many of the complicated language features and works great until something breaks and then you get nonsensical error messages.

Collapse
 
gualtierofr profile image
Gualtiero Frigerio

Nice reply and I agree on the fact that Strings manipulation is one of the worse aspect of the language.
The only downside I see is that the development of the language is driven by Apple so you end up with features that are necessary for some specific frameworks, like SwiftUI that required the introduction of Result Builders for example and although you can write server stuff with it is mostly confined to Apple platform.
In the end, like it a lot especially compared to Objective-C. I started iOS development more than 10 years ago and the codebase I worked on are improved a lot since the introduction of Swift.