Ten important key points, which need to know as a javascript developer
In words “Any fool can write code that a computer can understand. Good programmers write code that humans can understand”. When a programmer writes code, then he/she try to fix styling (such as curly braces, indentation, nesting levels, and function placement) and describe how and why the code works. And also resolve caching system, cross browsing testing, and error handling. So today I am going to talk about these key points.
01. Curly Braces
Curly braces are important when building logic or write objects. Novices in programming often use them wrongly. Novices tend to use curly braces in newline corresponding keywords. But the best practice is the opening brace on the same line as the corresponding keyword.
Example:
const abc = {'hello', 'hi'};
if(condition){
////
}
Sometimes no need to use curly braces as a single-line construct.
Example;
if(condition) console.log('Hello');
02. Nesting levels
Nested code is not to read code. One or 2 levels of nesting are probably the max that can be tolerated. So try to avoid too much depth nesting. To reduce nesting, we can use continue
We can write
for (let i = 0; i < 10; i++) {
if (!cond) continue; ... // <- no extra nesting level
}
03. Function placement
You can declare or organize a function in three ways. 1. Declare a function above the code, 2. Declare a function below the code, and 3. Declare a function when you need to use it first. But the best practice is to declare a function below the code. Because, when a human read the code, they understand what it does.
myFunction(n,m);function(n,m){
////
}
04. Comments
Comments are clarified how and why code works. But unorganized and more comments is a bad practice. Single line comments starting with //
and multi-line comments start with /* ... */
.
//const abc = 1234; single line comment
const abc = 4567;/*
for(i=0; i<10; i++){
////
}
*/ multi-line comment
Comments are really needed for explaining long-time-thought solutions
Error Handling
You are not a good programmer, until able into error handling. So error handling is a imporatnt part of any programming language. You can handle error in many ways. I am going to show, how can handle error by using try...catch
.
05. try…catch
try...catch
is a runtime error handler. And works for a valid javascript code. If code is syntactically wrong, then it not work. try...catch
is construct two main blocks. try...catch
work synchronously.
try {
// code... //block-one
} catch (err) {
// error handling //block-two
}
1. Execute first (try
) block.
2. If there were no errors, then catch (err)
is ignored: the execution reaches the end of try
and goes on, skipping catch
.
3. If an error occurs, then the try
execution is stopped, and control flows to the beginning of catch (err)
. The err
variable (we can use any name for it) will contain an error object with details about what happened.
Usually it’s used to decode data received over the network, from the server or another source.
Caching
Caching is a quickly data access system. Basically, caching reduce server response time. When you think about caching, then you resolve some challanges. Such as where and why do cach data.
06. Client Caching
Client-side caching duplicates the data of previously requested files directly within browser applications or other clients. Client cache is the most efficient type of caching, because it allows browsers to access files without communicating with the web server. The system controls client caching by modifying the HTTP headers of file request responses.
07. Server Caching
A server cache is a type cache that’s related to site caching, except instead of temporarily saving content on the client side, it’s stored on a site’s server. Server caching is also fully handled and amistered on the server without any involvement of the end user, or a browser. Object caching, CDN caching, and Opcode caching are also server caching
08. Hybrid Caching
Hybrid Caching is combine of client caching and server caching. This caching reduce data cost of either side.