📜 ⬆️ ⬇️

How to write the most awful backend for a mobile application


It is known that almost no mobile application is complete without a backend.


If you are a mobile developer, you probably came across such bearded uncles who melancholically pull the logic on a pearl and always write something in the console. Or maybe it was a slouching anime guy with long hair, sucking php with mother's milk.
Anyway, most of them have never come across mobile development, and some consider themselves to be gurus.


Especially for such cases, I prepared a list of harmful tips on how to ruin the backend of your application.


Enjoy reading.
So, if you are a server developer:



When I showed this article to my colleagues, many backend developers also decided to share a couple of important moments:



Useful recommendations


Laughing together at familiar situations is great, but apart from that, I would like to share more effective practices that we use in ourselves. Even when you have to work with external mobile developers, they always thank us for the extremely convenient API and professionalism.


All further tips relate to the backend, but if you are a mobile developer, then it will also be interesting and useful for you to read this. Indeed, in the first place it is you who are interested in changes.


Documentation


This is an interface for a mobile developer. It should be not just informative, but also easy to read and be nice to the eye. It sounds strange, but the easier the document is perceived, the faster and easier it is to work with it, and the less questions arise for you in the process.


The easiest and most convenient option is to use Swagger. Though its initial appearance leaves much to be desired:



But you can easily improve it using the formatter :


It turns out nice and comfortable. Alternatively, you can use Apiary , but you have to separate the code and documentation, which is undesirable, or bother with rendering.


Uniformity


There is complexity in mobile development - many solutions and frameworks are extremely slow. You can not just take and change the format for any one specific request, or it is extremely difficult. As it is impossible to change the name of a specific field only for a specific case: the poor developer will scream in a voice, trying to stick a crutch under it.


Everything should be complete: the same names everywhere, one interaction format (preferably JSON), and so on.


It is especially good if the names of the parameters in the request and response perfectly match the fields of the corresponding classes in the mobile application. It sounds weird, but it makes the life of the developers so simple that they will drag you the chocolates for it from the store.


In some places, simplification comes to the point of absurdity: for example, saving to Realm (mobile database) can be made almost immediately from json. If it is interesting, I’ll tell you separately how we got rid of middleware in a mobile application.


Sample code for saving any incoming objects on iOS:

One generic method for any record in the database from the server. Cool, right?


The same goes for images. Best of all, when the link immediately comes to the picture, which does not need to 'finish'. And according to the same rule - the name of the link should be the same everywhere.


We had a case when it was necessary to search for images in Google for certain information blocks in a mobile application. As a result, we just made a pseudo-link to the image to which the application is accessing, and inside the server it searches for a suitable image in Google and redirects it. And for the application, it looks like the most common piccha, which just takes a little longer to think.


Adequacy


When you work on a server, it’s usual that everything is in the single scope of the request, where it is enough just to open a transaction for writing and push the data into it consistently. Everything is isolated, predictable and linear.


There is no such thing in the mobile application. Everything is spinning asynchronously, and if you need to maintain the integrity of data from different requests, this results in the most complex manipulations with multithreading, infuriating critical sections and the distribution of priorities, so that there is no hint of brakes. No wonder the question about the synchronization of threads in an interview at a mobile developer is one of the first.


Now you understand why mobile developers are trying to ensure that everything comes in a single request? It depends on it, they will go home today or not)


And of course, if you need to load some data asynchronously, then you don’t need to push them into a common heap, you need to understand this.


In the end, do not be lazy to open the design of the application and see what the screen for which you are creating the API consists of. Check with your mobile colleagues, determine how best to give you data and what future requests will depend on them. Maybe in this particular request you need to give a little more information than it seems sufficient. But it will make the subsequent work more convenient and more pleasant on the client. Helping in such trifles, you will be remembered for a long time. And then they will remember with warmth their entire professional life.


In the same paragraph I want to include debug information. If you made a request for a list of comments, then make sure that these comments were there. For you to accumulate the same type of data is a matter of one minute, and for a partner from the mobile department it is a whole exhalation of relief.


Stability


Just a crucial point to which you want to pay attention separately. Always check your API, and even better - let the tests do it for you. Each bug on the backend is ten on the client. Indeed, between the server and the user there are many levels of abstractions, which must be eliminated before blaming the server.


Each bug spends the time of the user, tester, mobile developer, and only then - you. You are responsible for the greatest responsibility, and your mistakes cost the company the most.


As a bonus, I would like to add that it’s great when there is a pretty print, at least for the duration of development. It happens that you need to figure out what came from the server, without looking into the documentation.


And what's nicer to read is:



Or this:



The difference, it seems to me, on the face.
The main thing, do not forget to turn off Pretty Print on the battle server, because it eats resources like not in itself.


Conclusion


I just want everyone to say that the rule is in fact one and quite simple - do not make colleagues grind their teeth from your work.


The next time I plan to talk about how we moved to Go and got rid of a huge piece of business logic on the client, reducing the application binary by more than a third.

Source: https://habr.com/ru/post/331120/


All Articles