DEV Community

loading...
Cover image for πŸš€ Productivity Tips πŸš€ for every Javascript Programmer πŸ’»

πŸš€ Productivity Tips πŸš€ for every Javascript Programmer πŸ’»

sudarshan
πŸ’» π™Έπš—πššπšžπš’πšœπš’πšπš’πšŸπšŽ Β’Οƒβˆ‚Ρ”Ρ πŸ’»
・3 min read

Here is a interesting fact, do you know which is the worlds most hated and most loved programming language .......? πŸ™„

(anyways) You are right it is ❀ JAVASCRIPT ❀

Someone who is working in javascript from a certain period of time can easily describe the advantages of using javascript over some another languages like PHP and Python.
for ex.

πŸ”°πŸ”°πŸ”°

  1. Async nature of javascript
  2. Interpreted instead of Compiled
  3. Supports Promises and Closures and many more.

But, every thing comes with it's own cost. If someone tried coding in javascript without understanding the execution context and behavior of javascript, then (I think) it's the end game.

For avoiding this kinds of situation, here is the list of best practices, which anyone can follow to avoid the future mistakes.

πŸ‘‰ one thing, I want to mention is, this may not be worlds best article for best practices, but I will definitely make it as good as possible.


πŸ”³ Understand Scoping of Variables :

Understanding the scoping of variables is pretty important while coding. Variables declared with let are specifically blocked scope. Whereas, if you used var then you can hoist it anywhere in your code, but using var should be avoided as much as possible (suggested by many !).

But, I escalate this suggestion only when there is nothings seems working or scenarios where, I have to manipulate the global variables based of the events like

  • If exception occurs print log and terminate programme using
process.exit()
Enter fullscreen mode Exit fullscreen mode

etc...


πŸ“’ Getting Known To This :

I often find the behavior of the this is pretty confusing in javascript for me. Coming from the background of java and
PHP, I assumed that I will definitely feel at home if I used it but, then I was pretty confused when getting started with the React or ES6. After several weird things, I left it and then approached the programming in javascript using functional
approach.

It is still pretty confusing for me to know how this works, but if you wanna use it then know it perfectly.


🍭Create Pure Functions :

This is the principal everyone should follow in their programming paradigm. Creating pure functions and causing less side effects on the external world, makes the function more reusable and robust. If function is no longer making any side effects on the global context or the data present outside the body of the function, then it can be easily swapped out, reused and manipulated with ease

image


😷 Never Trust API Response Blindly:

Handling the API responses blindly can easily lead us to the end game or late night forceful fixes. Always handle the API response with null coalescing operators or default initialization of the variables.

for ex .

image

Know more about Null Coalescing Operators Here


πŸ’ͺ Use IIEF Whenever Possible :

IIEF stands for " Immediately Invoking Function Expression". It's comes pretty handy if you want to make your isolated from the global context. Take a look at here

image

IIEF keeps the variable inside the context and avoids the hoisting.


😎 Use Promises Instead of Callback Only Approach :

Using Promises is pretty standard practice now. But, anyone using callback-only approach must take a look at here.

Introducing promises cured the some major deficiencies in the javascript. for ex

  • Inversion of Control trust loss

etc.

Handle promises gracefully and enjoy the javascripting


🏡 Final Thoughts 🏡

Using javascript with modern practices could be the experience which any programmer never wanna leave, but it can be nightmare for someone who tries to get rid of the basic and directly jump into the advanced stuff.

Thanks for reading πŸ™

Discussion (6)

Collapse
milesmanners profile image
Miles Manners

JS is not async by nature. Without using service workers, it is single-threaded.

JS is also not interpreted (usually). It is JIT compiled in all modern browsers.

I'd also argue that understanding JS grammar is important to writing good code. Not understanding expressions, for instance, leads to slapping semicolons everywhere until they hold no meaning aside from, "It runs."

But, all this said, the other suggestions are fairly good. I think using IIFE more than once is getting a bit too clever, but it does help prevent scope pollution from var and function unnusual circumstances.

Collapse
darkwiiplayer profile image
DarkWiiPlayer

These days JITted languages are also considered "interpreted". Strictly speaking, almost no language is interpreted anymore these days, so playing semantics doesn't really help anyone there.

Collapse
sudarshansb143 profile image
sudarshan Author

Liked your reply !

Collapse
darkwiiplayer profile image
DarkWiiPlayer

1. Async nature of javascript

That's more of a downside, really. Javascripts Async-Model has been the source of much headache and only recently with async/await it seems like it's reached a level where most programmers are comfortable with it.

2. Interpreted instead of Compiled

This applies to both PHP and Python too, as well as a whole lot of other scripting languages. And it's not even an advantage in the first place; it's a trade-off. Interpreted javascript will never be as fast as well-written C code, and even JIT-Compilers tend to only beat idiomatic C code in very special cases.

3. Supports Promises and Closures and many more.

Promises are not really a language-feature; they could easily be implemented as a library on top of any language that supports some of objects and uses callbacks for asynchronous code.

As for Closures, javascript sadly stores values separately for each closure, so a whole bunch of techniques that use closures with shared state doesn't work.


Understand Scoping of Variables

This is the one category where no other language can beat javascript in how ridiculous it is. function declarations get hoistet, but class declarations don't. var is function-scoped, while let is block-scoped. <script> scripts share a scope across an HTML document, but <script type="module"> scripts have their own scope.

Once you understand all the implications of these differences, they become mostly noise, but for someone learning the language, that's unnecessarily complicated.


Use IIEF Whenever Possible

Yikes! Don't do that! Blocks {} exist for a good reason, and thanks to let, you probably won't have to wrap things into functions, specially if you're using strict mode.

let a = 20
{
   let a = 30
   console.log(a)
}
console.log(a)
Enter fullscreen mode Exit fullscreen mode

Use Promises Instead of Callback Only Approach

Better yet: use async/await. Unlike callbacks, promises are way over-engineered and way too complicated. They can do a lot for you, but they can also do a lot to ruin your day. Even if you think you've understand how they work in detail, the next programmer maintaining your code might not.

Collapse
siddharthshyniben profile image
Siddharth

Oh no! There is a bug in console.log! It randomly removes "c" from a string! "Darius Kinacde" became "Darius Kinade"!!!

Collapse
sudarshansb143 profile image
sudarshan Author • Edited

It's toooooooooooooo savage πŸ˜‚πŸ˜‚

but, your πŸ‘observation skills are on fire πŸ”₯πŸ”₯πŸ”₯πŸ”₯