How to write good “If” statements?

Writing a good code is an art and it can be useful in so trivial things that most developers often ignore. Usually, amateurs are programmed to write an ‘if’ statement which is always accompanied by an ‘else’ . However, it is important to realize that code quality can significantly be improved if else is omitted. For instance, consider this code.


function getUser($id){
  if($id < 1){
   $user = [];
  }else{
   //some big codes
  }
  return $user
}

Here some big codes can be so big that it will return statement will be lost in it. Better would be:


function getUser($id){
  if($id < 1){
   return [];
  }
  //some big codes
  return $user
}

Tell your network!

How to truncate docker logs?

Docker logs are organized inside their respective container folders. However, with a single command you can truncate all the docker logs as following:

[code lang="bash"]sudo truncate -s 0 /var/lib/docker/containers/*/*-json.log[/code]

Tell your network!

Why do you need a self-invoking function in JavaScript?

What is a Self-Invoking function?

A self-invoking function is called immediately after definition. For example look at the following codes:

[code lang="javascript"]
(function(){
    // Called as soon as inserted
    console.log("I am running");
})();
[/code]

Why do you need a self-invoking function?

A self-invoking function is an anonymous function and thus all the variables defined inside it are scoped and hence you keep the namespace polution out of it.

[code lang="javascript"]
(function(){
    var i = 0;
    console.log(i); //prints 0
})();
console.log(i); //prints undefined
[/code]

In JavaScript, scopes are “function-level

Tell your network!