When not to code it from Scratch: Backend Edition
Tags: beginners, architecture
Published: 2020-01-12 17:47:53

<!-- wp:paragraph -->
<p>After spending some extensive time developing the backend for a personal project, as well as gaining some experience from work, I felt that it was time to add the missing part of a <a href="https://byteschool.io/post/when-to-avoid-coding-something-from-scratch-frontend-edition" target="_blank">earlier post </a>. This series is mainly focused on teaching new developers what parts of their software they can outsource to libraries or services, so they don't reinvent the wheel.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Backend development is crucial for many applications, because it holds and manages one of the companies most precious resources: data. This means that even more  caution is required when working on these critical areas of the software. Fortunately, frameworks and libraries can be leveraged to save time and lead to more secure code. What parts you can delegate to third party tools or libraries will be discussed in this article, but ultimately that choice is up to you!</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Request and Response Handling</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Most backend frameworks usually come with at least some routing functionality that lets you define how the application can be addressed with a request, as well as the ability to return a response. There is generally no need to spend any time on your own implementation, but I'd suggest comparing how several frameworks or libraries implement this and picking one based on your preferences</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>User Authentication and Authorization</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>User permissions are critical to any application, as they guard sensitive information. Often, frameworks come with their own user functionality features, depending on how oppinionated the given framework is. These usually handle the following things for you, so you don't need to code them from scratch:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Database model for the user</li><li>Methods for basic user actions (registration, login, password reset)</li><li>Session management to verify if a user is logged in</li><li>Permission classes that let you differentiate between different types of users (for example admin vs regular user)</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>If you are going to code the registration  and login manually, make sure you encrpyt passwords using an hashing library!</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are often libraries that offer additional functionality on top of the one provided by your given framework (for example "all-auth", a framework that builds on top of the python backend framework django). They are designed to provide out of the box solutions for user related patterns that are common, but maybe not so crucial that they are required as part of the base framework. I would suggest looking for a third part library if you are trying to do the following things:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Account verification via email, recaptcha or another service</li><li>Two factor authentication</li><li>Token Generation for API</li><li>Authentication via third-party accounts (Google, Facebook, etc.)</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Serverside Rendered Templating</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Most backend frameworks offer ways to generate HTML content dynamically using data from your database. This is standardly done using a templating language like jinja or thymeleaf. Most frameworks I know ship with this built in, I can't think of a reason to code this from scratch if you opt for SSR.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Database Queries</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A lot of frameworks work with so called ORMs or allow you to easily add one. ORM stands for object relational mapping and let you map your database tables to objects for the given language that you are working in. They also provide more simple ways of accessing the data. While this can make development much more convenient, there is tradeoff in the additional overhead from using an ORM. If you prefer raw SQL, then you should still use a library to handle the database connection and sanitize your queries against SQL Injections.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>API Payload Serialisation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When sending data between the client and server, you need to choose a data format that is used. API serialisation frameworks allow you to convert objects in your given programming language to common data formats, like JSON or XML.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>API Endpoint Generation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When you are working with GraphQL, creating the endpoint logic from scratch can be pretty tedious. Libraries are popping up that make this much easier.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Even when using REST, you can often leverage "routers" that allow you to define a few generic methods (update, create, get, etc.) you want to support for a given object and the router generates an implementation for you.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>The entire backend...?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Sometimes, your website does NOT need a backend! When is this the case? Well, nowadays API services make it possible to create apps that use third party data combined with frontend code. In addition, there are content management systems like Wordpress that, with the help of some plugins, might also be able to fullfill your needs. Finally, there are emerging BaaS (Backend as a Service) like Firebase that can be used when you aren't doing anything too fancy or unique and want to iterate quickly. This can include things like authentication, which can easily be outsourced to a third party like Google or Facebook using OAuth (notice how dev.to has done this by only allowing login with Twitter or GitHub).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I hope you enjoyed this short and sweet overview of some of the ways that you can cut corners when writing your backend code!</p>
<!-- /wp:paragraph -->