loading...
Cover image for KISS The JavaScript

KISS The JavaScript

nijeesh4all profile image Nijeesh Joshy ・3 min read
                       " KEEP IT STUPID SIMPLE "

I used to see this quote and say "When I can Write Some Awesome Code Why Should I Keep it simple after all it is my code I have comments Every where so no problem for me to understand it later"

So as a programmer just think for a second what was the best practice yiu have taken when you write code.Is is Commenting every now and then?, Spliting it into modules ?, or may be reducing Repetition.

Every one writes codes in their own way but there is a way which can be applied to any way is 'Keep it simple stupid'.

You can get two meanings from the same quote which is

  1. 'Keep it simple, Stupid'
  2. 'Keep it simply stupid'

I will Go with the Second one because i don't like anybody calling me stupid.

I will say why you should kiss (Sounds a little awkward but it sounds funny).You may be an Einstein you can write codes that only you can read and understand.But the way our mind works is really funny you may be in your Einstein mode when you wrote your code when you have to rework on the code after a few days may be months or years your mind will be on the trump state. So instead correcting the code you wrote you will be wasting a lots of time trying to make sense of what you wrote (Getting dejavu ?).

You may be thinking all code i write is stupid i am not smart enough to write complex code. Let me tell you why this is probably not the case.According to me an awesome code is something which can be understood by even a dumb person (Smart enough to read code).Keeping things simple ironically is not that simple.

According to me write codes for the dumper self of you not for others to understand.

When it comes to JS things can get real bad real soon.If you have coded in JS for some time you would have heard about something called as callback hell. If you don't know this is an definition of callback hell

Seen the movie Inception? The movie talks about dreams within dreams within dreams, each layer with its own scope but can affect the previous layer of dream - if there is one. The worst scenario is that if you get lost, then you have no way of knowing where you are and where you came from.

This is an example of a call back hell from callback hell


fs.readdir(source, function (err, files) {
  if (err) {
    console.log('Error finding files: ' + err)
  } else {
    files.forEach(function (filename, fileIndex) {
      console.log(filename)
      gm(source + filename).size(function (err, values) {
        if (err) {
          console.log('Error identifying file size: ' + err)
        } else {
          console.log(filename + ' : ' + values)
          aspect = (values.width / values.height)
          widths.forEach(function (width, widthIndex) {
            height = Math.round(width / aspect)
            console.log('resizing ' + filename + 'to ' + height + 'x' + height)
            this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
              if (err) console.log('Error writing file: ' + err)
            })
          }.bind(this))
        }
      })
    })
  }
})

This code here looks real messy it can be avoided using promises learn more here.

Using promises will make you code pretty much easy to read and maintain. You will feel more happy seeing the code which is neatly organised.


awesome_function()
    .then(do_this)
    .then(do_this_too)
    .then(here_is_one_more)
    .error(oh_crap)

See writing code with promises is almost looks like synchronous programming.So think of a scenario where you have to correct the code some one else written if it is written in the first way you will be pulling your hairs out and if its the second way even a stupid person can understand that it does.

I have talked here about call back hell where i should have talked more generally.In my opinion call backs are the easiest things to make your code messy So refactoring them will you leave you with a good and neat code.

Happy coding ...

links

1.KISS for programming
2.Callback Hell
3.Promises
4.Callbacks
5.Promises

Discussion

pic
Editor guide