DEV Community

Pablo Cavalcante
Pablo Cavalcante

Posted on • Updated on

JavaScript Techniques

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

With the application of the technique.

With Currying

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

With Guard Clauses applied.

With Guard Clauses

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

With the technique.

With Object Literal

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

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. 👋👨‍💻

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Closure

It’s a function that can reference variables outside its scope...

ALL functions can reference items in their surrounding state (lexical environment) - whether a function is defined inside another function is irrelevant. Also, a function is not a closure... a closure is the combination of a function bundled together (enclosed) with references to its lexical environment (see MDN).

It isn't correct that a closure is some kind of special function - it is a distinct thing from a function, and every function has an associated closure.