JavaScript Techniques
Tags: javascript, javascripttechniques, programming
Published: 2023-05-23 20:08:45

Programming techniques are concepts that are applied in software programming. Generally,  are good practices that help not only to perform the code but also make it more readable, in addition to other benefits. Next, will be exemplified by some more common techniques in software development.

## Currying

Created by Moses Schonfinkel and Gottlob Frege and independently by Haskell Curry, it’s a technique that consists of transforming a function that takes many parameters into a chain of function calls that take only one parameter. That’s basically it. In a way, it reminds a little of Fluent Interface (a technique created by Martin Fowler and Eric Evans for building classes that favor obtaining less expensive and more readable code).

Here’s a simple example of applying this technique.

Without the application of the technique.

![Without Currying](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83vlss0x2glc5j9qq1p8.png)

With the application of the technique.

![With Currying](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0f355i8zfefu3unqy5to.png)

## Guard Clauses

Also known as Early Return or Bouncer Pattern, it consists of an early boolean evaluation of preconditions in order to verify whether the execution of the program continues in that execution flow. It doesn't go much beyond a series of if conditionals, however, in the case of this technique, it’s done in a flatter way, the code source cleaner and more readable.

Here’s an example of code without the use of Guard Clauses and later refactored using this technique.


![Without Guard Clauses](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tso4dg5qrxrm9xne7f43.png)

With Guard Clauses applied.

![With Guard Clauses](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3fndenq9br5q2ivdktx.png)


## Object Literal

That’s a technique that aims to replace the switch condition structure, making the code more readable, also avoiding some problems with the use of this type of programming instruction. It’s nothing more than an object with its properties and their respective values.

Here’s an example of code without the use of Guard Clauses and later refactored using this technique.

![Without Object Literal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf2vlstsjyhd0cg0zaqn.png)


With the technique.


![With Object Literal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q613tx57w2nssjiq756j.png)

## Closure

It’s a function that can reference variables outside its scope, but variables around the lexical context that surrounds it. It’s declared inside another function and you can extend the scope of variables beyond the local scope, but not to the global scope, in other words, the inner function will have access to the variables of its immediately outer function.

See image below.


![Closure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89bhbc9815zuly044m5s.png)

There are many other techniques out there, worth looking into and learning more. Those presented in this article are just some of them. Well, that 's it! See you guys. 👋👨‍💻