To many specialists the question in the title may seem absurd. After all, when we talk about splitting software into frontend and backend, the message from the start is that the frontend is for the interface and the backend is for logic. So where does the idea of swapping roles come from, and why explain this at all?
The source is, of course, life and observation of various projects encountered in professional, academic and private settings. It happens that programmers who know the frontend better (and in the form of a specific framework, such as React) join projects that also require developing backend code, and when they run into difficulties, they choose the solution they feel more comfortable with. That is not always a sensible choice, meaning one that weighs the pros and cons of different approaches and ends with the right, future-proof option. Instead, some follow the line that since they know the frontend better, they will do as much as possible in the frontend. After all, what matters is the result, right? And when it comes to logic and calculations in general, this is usually a mistake.
Many beginners are left with the question: why exactly is it a mistake? And this is not only about saving entered data to the database, where the backend is often the obvious choice. It is less clear where to put things like calculating age from a date of birth or taking a percentage of two values. Let's talk about it.
Advantages of the frontend and the backend
Let's say it once more:
- the frontend is the part of the software responsible for the interface, displaying information to the user and directly handling the events they trigger. It is "what you can see",
- the backend is the part of the software the frontend communicates with to get data or save it. It is "what you cannot see".
In the world of applications, especially web ones (but not only), the frontend usually runs directly in the user's environment, while the backend is hidden on a server that nobody (except the administrator) can access directly. This has far-reaching consequences, because it means that:
- the frontend is faster at operations that use the user's hardware, since it has access to it. It can also take different forms depending on the user's preferences, but it is exposed to, among other things, "hacking",
- the backend is more secure (hidden in a place the user cannot enter) and is a single source of truth, but for that reason it is also burdened with requests from frontends running on many users' devices.
In the past, when monolithic applications reigned, that is, ones built on a single piece of software living on a server, the architecture was simpler, but systems were more heavily loaded and harder to scale. They also limited the choice of technology to one, and we know there are libraries and frameworks that are better for specific uses. Finally, splitting into two parts simply brings benefits in project management and software development, and makes it possible to assign responsibility for individual parts.
Performance
Imagine we fetch a user's date of birth, but we have to display not only that but also the age derived from it. Of course, this means taking the current date and subtracting the date of birth. The mechanisms for this exist on both sides of the technology split, but in most cases the recommendation will be to do it in the backend. Why?
For a single value it makes little difference. But imagine that programmers get used to doing this kind of small calculation mostly in the frontend. The backend will indeed be quite efficient, since it will only pass on raw data and will not compute extra information. But the browser on the user's side, which usually has weaker hardware than the server, will perform operations that, in large numbers, simply slow the page down. You can fight this with various techniques involving lazy loading, asynchronicity and so on, but that is heroically solving a problem we brought on ourselves.
Users today are used to applications loading instantly, websites in particular. This is because access to fast Internet is much wider than it used to be (I do not want to say universal, since not everyone is lucky enough to have it), and so the expectations of web users have grown, especially toward e-commerce services and news portals. Sure, the longer the backend processes data, the higher the chance that an irritated user will close the page, but we will cause the same thing by shifting the load to the frontend. It can even be worse, because that work moves to the browser engine, which can affect the device of the person trying to open the page.
But it is not only about "occupying" the computer. Browsing the Internet works in such a way that when browsers display a website, they download it to the user's computer. They do it faster the smaller the files that make up the web page. That is why optimizing frontends is so important, especially for frequently viewed, profit-driven sites. Adding extra logic code makes pages larger and slower for the browser to fetch and display.
In addition, using the backend for calculations often encourages techniques that speed things up, such as caching. If an application often has to display users' ages and many people ask for it, we quickly realize that age can be calculated once, updated at the right moments (e.g. at night, using cron), stored in the database and returned as an already calculated value. The frontend can use a cache too, but separately for each user and not necessarily as quickly.
The date of birth example is quite clear, though in reality "cute", because subtracting two dates is no challenge for any modern computer. Still, the principle can be extended to more demanding calculations, such as summing up a thousand objects, searching for a phrase in many long texts or other complicated pieces of logic. Then the user's device may really "break a sweat".
It also has to be said that server performance is not always an argument for the backend. If we know the server side will be under very heavy load, we cannot afford to duplicate it for the time being, and extra code on the client side will make no difference, then perhaps it is worth taking that step. An example is online video games, which have to run exceptionally fast and smoothly, because even milliseconds can matter in a battle between players. There, only the absolutely necessary operations run on the server, while visualization and elements that can tolerate some "desynchronization" run on the player's computer. That is why in shooters like Valorant, during so-called lag, you sometimes move across the map and the game pulls you back to your previous position: the backend sends the last agreed position of the player, even though the frontend (the application on the user's computer) has already calculated the path further on.
Code readability
When most applications were still built in a monolithic architecture (e.g. in PHP), it mattered a great deal how much code sat in the view files. Ideally there should be only HTML structure there, with the necessary additions from the given language that insert already prepared data. The reason was fairly simple: the more logic was "stuffed" into views, the less readable the code became. Various template engines helped (and still help) with this, but if someone really wants to, they can make such a mess in the view layer that the controllers and logic classes are bored stiff.
Introducing JavaScript frameworks on the frontend allowed a bit more, since they are naturally split up and separate the view logic from the view itself. But if you are not careful, you can write a frontend in this architecture too in a way that is unreadable and hard for other programmers to navigate. The more long, logical fragments it contains, the greater the chance that we will call the code "ugly" and that it will be judged poorly during a code review.
It also matters because of programmers' specializations. Frontend developers usually deal with different logic than backend developers, because client applications today are complicated enough that, colloquially speaking, there is plenty going on in them, and a lot of code has to be fitted into various classes and components. If we add elements from the backend on top of that, finding your way around the application can become a daunting task.
Someone might now ask: if logic code can cause confusion, doesn't it create a mess on the backend side too? Well, it certainly can. But the backend architecture naturally makes room for such code and for a proper internal split, so it can be written there neatly and coherently. It is possible in the frontend too, but because of its purpose it is harder to maintain there.
Security
In my opinion this is the key reason why logic should be on the backend side, or at least why the information entered by the user should be verified there. I checked, and ChatGPT says so too.
Imagine adding products to the cart in an online shop. The cost of the items is summed up and the user can proceed to the order form. Most users will not even think they could modify the price, but there are also people who know that web browsers let you inspect many aspects of the displayed page, including its code, both HTML and JavaScript. What is more, that code can be changed, modifying not only the look but also the behavior of the site. So you can guess what would happen if someone edited the algorithm that calculates the cost and the backend did not check it.
That is why the server side, protected from direct modification by users, should be the place where the key elements of logic live. Above I mentioned the example of a video game, where only such a critical area sits on the server that conducts and referees the match played by the players. It is precisely because someone changing the colors of banners or shrinking the window is no problem, while modifying, say, the number of health points or other elements that could affect the outcome is not allowed.
Of course, some calculations matter less for an application's security. For example, in most cases nothing would happen if someone modified their age. Still, it is natural to keep logic in one place, and the backend is much better suited to that than the frontend because it is hidden from the user. And age itself can matter too, for example in operations that require being 18.
A uniform environment
If someone does put logic on the frontend side, they most often use the most popular language supported by web browsers, JavaScript. And a particular technology has the property that it sometimes changes or is even replaced. In the dynamic world of the Internet this matters even more.
Of course, the probability that logic written in JavaScript will not be handled the same way for every user is not very high, but it exists and is not negligible. All it takes is one newer construct for a somewhat older engine in a non-updated browser to fail to cope and either report an error on the page (causing fury and disbelief in the user) or, worse, cause a so-called silent error, meaning the application works but with a mistake in the calculations, which can have terrible consequences.
Can't this happen on the backend too? Of course it can. But since as an IT team we usually control the server on which this "logical" part of the application is hosted, we also have a say in updating engines, software and configuration. As a result, the server has a single configuration, and it is known what we can allow ourselves when writing code and which technology or language constructs will be interpreted correctly on the machine that all the frontends use.
Besides, we can go further and anticipate the user turning off JavaScript in the browser. This is possible, and although it is hard today to find a complex application that works properly without JS, people who are particularly careful about their security still use this option. If our logic was written in JS on the frontend side, well, then we have a problem.
Testing
This point does not matter as much as it used to, because there are now testing frameworks focused on frontend technologies. Still, automated verification of software is more often associated with the backend and is sometimes simply easier there. And the code that gets tested most often is exactly the logic code.
Summary
Does all this mean that you absolutely cannot put logic on the frontend side? Not quite. There may be situations where doing so will not cause significant harm to a project. Still, it should certainly not be the rule, because the place for calculations (especially the "heavy" ones) is on the backend side, and the two main arguments are security (a topic that has come up in every possible way in recent years) and performance (which even marketing pays attention to). So if you work as a so-called full stack programmer but historically feel closer to the JS frontend, think twice before putting logic code there. It may be worth warming up to the server side in this area.
Best regards and thank you, Jakub Rojek.


