The mobile development segment regularly offers market participants new tools. On whether to use such a new concept for programming under iOS, like generics, and at the same time about whether it is time to switch to Swift, we talked with Maxim Sokolov, a mobile development specialist of Avito.
- Please tell us in a few words about yourself and your experience with Swift.- At the moment I work in Avito as a developer of mobile apps for iOS. I have been working in mobile development for about 5 years, and before that I was engaged in various areas, in particular, websites, backend and desktop applications.
We started using Swift in Avito more than six months ago, when the second version of the language came out, feedback from the developer community was accumulated. Earlier in our work, we used Objective-C, but now we write all new code in Swift.
')
- Some developers speak of Swift, as a very "raw" language, not yet suitable for large projects. Can you confirm or deny this opinion?- Swift, of course, has drawbacks, in particular, related to the development environment. Xcode falls from time to time, syntax highlighting is turned off, or the compiler refuses to compile the code. These shortcomings can not be called the reason for refusing to develop large Swift projects. Facing problems, we found a solution. And we have already sent to the production a fairly voluminous project written in this language.
- What prompted you, despite these difficulties, to switch to Swift?“First of all, we understand that Apple is developing and will continue to develop Swift, while Objective-C has not received significant changes for a long time (we will not take into account changes related to Swift compatibility). The transition to Swift now is to some extent a step into the future.
We at Avito, as an experiment, decided to use Swift for one of our projects. And in general, not faced with insurmountable difficulties. Everything began to turn out. The code is read and written easier than in Objective-C. And most importantly, it turns out typed and safe, including through the use of generics.
- By the way, can generics be called one of the arguments in favor of switching to Swift?- In general, yes.
But here too much depends on the business objectives. Firstly, there are not so many developers with extensive experience with Swift. Secondly, there are many projects written earlier in Objective-C, they need to be supported. And in order to write well in Swift, you need to study it, spend resources on it. And these factors are much more important for business. Accordingly, generics are not some kind of unambiguous argument for the migration to Swift. This is just a handy tool.
- How actively do you have to use generics in your work, and what are the main advantages of this concept?- We use generics very actively. This concept helps us to write quality, strongly typed, predictable code. We see the impact of our changes, and this is important for us, because we write with one team several projects at once, and we have a lot of reusable components. Of course, we follow the versioning, but when the next changes are made to the component, we want to know what they will affect. When we use generics, we do not have a dynamic type cast; we see that some model has changed, some requirements for the model, protocols have changed. And possible problems with typing come up immediately in the form of errors. It is very convenient.
Therefore, generics are used everywhere, given that the concept can be applied in various fields: in work with the network, and with data models, and even with UI.
- The concept of generics is not new. Long ago there appeared analogs in other languages, and the ideas of generalized programming have existed for decades. Why, in your opinion, are developers not always (when it is possible) turning to them?- Indeed, generics in one form or another are present in many programming languages for decades. And, yes, developers are not always turning to them.
In the context of the Swift language, in principle, you can do without generics: we can write on Swift and not use generics, if only because we have used Objective-C for years, where there are no similar structures, and now we are trying to project our old experience on Swift. In my opinion, this is the main reason - when you see a new technology for yourself (which was not in Objective-C), you approach it carefully, and sometimes, taking into account experience, you bypass it. However, if you dive into it, it will be much easier to write code. And the code itself will be much better.
- For what tasks, in your opinion, is it best to use generics (specifically within Swift)?- There are quite a few such tasks. Generics help well where there is a certain algorithm that can be reused by working with different types of data. Obvious examples are the sorting of real and integer numbers using a single algorithm (without simply copying the code).
There are more interesting examples, in particular, work with a network or tables in iOS. But without visual schemes and specific code examples, such tasks are quite difficult to disassemble. I will just sort these examples as part of my
speech on Mobius . I will tell you where it is very efficient to use such constructions, and where they can significantly reduce the amount of code.
The most interesting case, in my opinion, is the control for working with UITableView tables. When using tables, we have to do type-casting, but in the report I will try to show how all this can work with the help of generics. Moreover, in this context, generics help to significantly reduce the amount of code.
- Does the active use of generics cause any application problems (with security, etc.)?- We have not encountered such problems.
What difficulties can there be? Using generics, you have to think carefully about the architecture, make decisions, where the use of generics is justified, and where not. You should not try to use this technology where there is no benefit from it. But this is not a problem specifically generics. This is true for any technology.
- Have you encountered in the work with inappropriate use of generics?- For example, if the function should work with only one data type, and it is designed as a generic, it complicates and confuses the code.
But in most cases, we rather had the opposite - we wrote code without using generics, and then we realized that it would be very cool to use generics.
- Are there any restrictions on the use of generics in Swift?- The main limitation I would call the compatibility of Objective-C and Swift: a generic code written in Swift cannot be used in Objective-C.
Thank you for the conversation!As noted above, Maxim Sokolov will raise practical questions about the effectiveness of using the tools proposed by Swift language developers (namely, generics) to build more predictable and secure code as part of his
report at the Mobius conference . The event will be held on June 4 in St. Petersburg.