You can reduce the quality of photos to speed up the loading of your site
NET 5., the successor to .NET Core 3.1. And .NET Framework 4.8., Intends to bring a new cross-platform development experience to .NET developers. Offer. This brings new good features. Of course, in this article you will not learn everything about .NET 5., but you can focus on 5 to get a better understanding of what is happening.
If you own a website or work on a customer website, then you need to know the importance of SEO. Without proper SEO strategies, you can not get high rankings in Google.
Success in a media channel such as organic search depends on the content. In particular, the production of useful content has the ability to rank. In its recent algorithm updates, Google has focused heavily on promoting great content and natural links, and penalizing poor content with unspecified links.
Environmental variables are excellent. However, everything is costly, and these variables, if used carelessly, can have detrimental effects on our code and programs.
If environmental variables help us write more secure code and configure programs more easily in different environments, how can they be a bad thing?
Environmental variables are global and external, through which application developers are able to inject configurations and manage them where they are more difficult to adapt.
We all know as developers that global situations can be bad for our applications. In this article, we will focus on the two main drawbacks that we encounter most when dealing with environmental variables:
Poor inflexibility / testability
Readability / Comprehension of code
How to use environmental variables correctly
Similarly, how we deal with global variables or global patterns (such as singleton) that are applied in bad places is our favorite weapon of injecting dependency.
It's not supposed to be exactly what we do for code dependencies, but the principles are the same. Instead of using environmental variables (dependencies) directly, we inject them into places that are actually used (callsites). Reverse this relationship from "dependent callsites" to "required callsites".
Dependency injection solves these problems using the following:
Allows developers to easily inject configurations during testing.
So how do we apply these principles?
We use a Node.js example to see how we can refract a base code and eliminate individual environmental variables.
Let's say we have a simple program with an endpoint that queries for all TODOs in a PostGres database. Here is our database module with separate environmental variables:
And this module is injected into our HTTP controller through the program input:
Looking at the entrypoint file above, we have no way of defining the program needs for environmental variables (generally configuring the environment globally).
The first step in improving the code already specified is to identify all locations that use environmental variables directly.
For our special cases above, it is very straightforward because the base code is small. But for larger code, you can use tools like eslint to scan all locations that use environment variables directly. Just set a rule, such as banning environment variables (like node / no-process-env from eslint-plugin-node).
Now it's time to eliminate the direct use of environmental variables from our application modules and consider these configurations as part of the module requirements:
These configurations are generated only from the point of entry of our application:
Given the point of entry, it is quite clear what the environmental conditions of our program are now. This prevents potential problems with environmental variables that are forgotten to be added.
But you may have a question here:
Why not use a central module / file?
We have seen several solutions to this problem using a central location to plot these values (such as the module / config.js file for Node projects).
But this method is no better than using the environment variables provided by the program runtime (like process.env) because everything is still somewhat global (a configuration object used throughout the program).
In fact, it could be even worse because now we are introducing another place for the code.
In this article, we tried to show you with a simple example how to use environmental variables correctly. Always keep in mind that misuse of these variables can have consequences for your program