5 ms·
The critical difference is that in swift your have to override safe behavior by using special syntax. This means you can search for it to fix it, you can have c
by flipgimble 8y ago
The critical difference is that in swift your have to override safe behavior by using special syntax. This means you can search for it to fix it, you can have compiler/linters enforce certain rules, you can catch it easily in code reviews. No professional team I've worked on would allow forced unwrapped code except in special well documented cases.
The default code you write in swift is safe. You spend less time worrying about its correctness, which means you can focus on solving higher level problem and less time debugging.
When people call swift pedantic, for example, and I take time to ask for the specifics, I mostly get "not familiar to me", or "I'm used to shooting myself in the foot and unwilling to consider alternatives". So my patience has run out.
- fauigerzigerk 8y agoIf only there was a way to catch array index out of bounds.
- scarface74 8y agoI don’t know Swift, so I don’t know what you’re implying, but I only know of three ways to deal with an array out of bounds runtime issue.... 1. Don’t check for it at runtime and the runtime writes to memory that it shouldn’t like C. 2. Throw an exception and prevent memory corruption. 3. Don’t ever work with indexes and just use iterators. Is there a fourth way that it can be handled in other languages?
- saagarjha 8y agoYou could have the subscripting method check at runtime and return an Optional.
- zoul 8y agoextension Collection { subscript (safe index: Index) -> Element? { return indices.contains(index) ? self[index] : nil } } And now you have the choice: array[i] // traps for invalid indices array[safe: i] // returns nil for invalid indices
- valuearb 8y agoLol, I put that code in my last project, a 150,000 line codebase built by lazy contractors, as just one of the tools to clean up the errors from their objective c like approach to software quality.
- blub 8y agoInterestingly put, "the Objective-C approach to software quality". There's something in the psychology of a large number of C, Objective-C and C++ programmers that makes them overappreciate execution speed and underappreciate memory safety in particular, but also validation and correctness in general. One of my goals now is to outline safe ways of using C++ for various projects and I'm not too optimistic. In fact I'm unsure what we'll end up with.
- pjmlp 8y agoQuite true, hence why I say that the problem is human and not technical. Those of us that come from Wirth languages always put quality and delivering what the program is expected to do before going crazy with optimizations. I always got the feel that many in the C communities write code as if they were micro-optimizing every single line, just because. And those that care about type safe driven programming in C++ usually have background in safer languages.
- fauigerzigerk 8y agoI don't think Swift has a way to catch all runtime errors that others may have introduced in a reliable way. Perhaps because it's not a huge problem in iOS apps, but for long running multi-threaded applications this is a problem.
- fauigerzigerk 8y agoI always have a choice if I'm the one writing the code. But what I want is a way to catch all errors that others may have introduced in code I have no control over. That includes arithmetic overflow as well.