How to spot if something is written by an AI?

Identifying whether or not something was written by an AI can be a challenging task, as artificial intelligence programs have become increasingly sophisticated and are able to mimic human writing to…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Embracing Strong Types in Swift

Swift is a strongly typed language. This means seeing a particular type allows you to make some reassuring assumptions that you just don’t get from your previous partners.

Consider the following bit of Objective-C:

This looks simple enough, but there’s a very important question to ask: “What format is the data?”. There’s no way to know without anxiously hunting around the codebase (ideally an hour before the deadline, in a cold sweat). What’s worse is there aren’t any real protections if you handle the data type incorrectly or if the type changes in future. There’s also a the horrible chance that the array is nil, because Objective-C is the worst.

In Swift, it’s much simpler; you will immediately know if you’re dealing with [Int], [Float], or [MyFunkyStruct] the moment you have your fingers on the array. If the data model changes the code will fail at compile time, and you can easily deal with it. You can also guarantee the result will not be nil, because if it could be it’d be an Optional, and the compiler would order you to deal with that.

Now consider a bit of beautiful Swift:

The code looks good, and you look good. The trouble is: everything below does NOT look good, and is perfectly valid:

Everything above is perfectly acceptable to the compiler, but it might not be acceptable to your data model. If you’re encoding this identifier as a json, csv, or to use with a server, there may be formatting restrictions.

The worst way to let your team know how this should work is to use comments (slightly worse than doing nothing at all), as these can be misinterpreted and are very rarely updated as the code evolves. The best way is to make sure your compiler goes red when you enter something wrong.

Now, why on Earth am I writing a blog post about this? There’s a trivial solution:

Well thank you for falling into my trap, my foolish inner monologue. This works fine if you’re after a quick an easy hack, but it’s not a long term solution. There are likely going to be other places where identifiers are created and validated, and adding isValid to every instance can quickly become annoying, and is incredibly easy step to forget if someone else is working with your code.

Swift is a strongly typed language. Embrace it! 🤗

The best way to ensure that only valid data is passed into a function is to create a type that can only exist in a valid form.

With this, those reassuring assumptions I mentioned earlier hold true. It’s impossible to pass in an invalid Identifier, as it literally can’t be created in an invalid format. Plus, unlike our Objective-C counterparts, you can’t pass in a nil and expect to get away with it. The cherry on top: it’s super easy to extend the Identifier type with methods that ONLY affect identifiers, and not ALL Strings.

** 2019 UPDATE **

There is one additional advantage you get from this identifier struct: autocomplete sanity. If your identifier is a string, it’s perfectly valid to identifier.reversed() or identifier.contains("e"). This, of course, is rather minor, but preventing stuff like this appearing makes your types nicer to work with. Moreover, you CAN write functions that only apply to your Identifier type. Using strings would make it impossible to write these special functions, without having needing it to apply to all string properties in your project.

String used as identifiers do have their advantages, and occur a lot as a result. However, remember that when you work as part of a team the time others will spend reading your code is far greater than the time you spend writing it. Developers need to be able to comprehend what’s happening as quickly as possible, and the best way to do that is to have the compiler do your bidding.

Thanks for reading,

Chris

Add a comment

Related posts:

Que bien se siente

Me acuerdo perfectamente como me solía sentir en aquellos tiempos. Me acuerdo que solo quería desaparecer. Me decía “si algo me pasa, realmente no me va a importar” porque lo único que quería era que…

Best Color combinations for office decoration

The office is the place that employees spend most of their daytime. Thus, it should be a place where they will feel relaxed, productive and motivated. A place where they would enjoy spending most of…

True Bond over Instant Gratification

With the nature of the world at a standstill because of the coronavirus pandemic, more and more people have been watching new shows that have arrived on the various streaming services. One show that…