The Lazy pattern, a 2 min Javascript tip you can take for life.

Pedro Mendonça
Team40
Published in
4 min readOct 9, 2018

This will be a quick one, I will show you something you should always do in every single one of your JavaScript projects from now on (Node or Browser). You can simply do it and leave this article, or you can read the whole thing and hear why I think this should be done 100% of the time.

Do not ever use `console`, put the following line of code at the top of all your next projects and proceed to use, `log()`, `warn()` and ‘error()’.

Okay if you are still here is because you want some answers. Let me start explaining what this line does if you are not familiar with the destructuring notation. What this simple line of code does is it renames the ‘console.log’ function to a function called ‘log’ and so on for the ‘warn’ and ‘error’ functions.

Being lazy is not that bad…

The reason I decided that this is a requirement in all my code is because of what I personally call the Lazy Programmer Pattern. Laziness in computer science unlike anything else in life is highly regarded as something useful. Smarter people than me have created impressive concepts based on the idea of ‘laziness’ such as lazy evaluation and lazy loading. Yes, you read that right laziness is good.

Let me explain, you may think that laziness refers to not doing anything and being unproductive. However, there is a much better way of looking at it. Laziness is to avoid doing something until it is of extreme priority and doing whatever it takes to work less even if that means a small initial investment. The following quote often gets thrown around in association with Bill Gates or Walter Chrysler, I don’t have evidence for who actually said it, but I like it so I will drop it in here anyway.

Whenever there is a hard job to be done I assign it to a lazy man; he is sure to find an easy way of doing it.

Back to the code

If you are starting out as a programmer you probably have never really cared or even thought about how you log(print) things. Logs are there to serve as a way for you to understand what is happening. However, if you are working in a company or deploying a complex system with multiple parts, developers often define ‘Logging conventions’. These can vary from simply putting a timestamp on your logs to more complex ones which tell you where the log is coming from and even write all output to multiple log files instead of printing it to ‘stdout’. Some logging conventions straight out may say NEVER log as it may slow down your program. Here is a more in-depth article if you want to see a more detailed example of why you may wanna use JSON logs.

If later in the future you have to implement one of these conventions you don’t want to have to replace every single call of console.log, console.warn and console.error in your code with your newly created functions, what you want to do is to delete that line and define your own functions. This way if you need all your logs to be written in a file just simply create a function called log and do everything you need to do there. I am not saying that line has to stay in your code base forever but rather it serves as a placeholder to save you time in the future.

The principle of the Lazy pattern is to be lazy. Yes, that doesn’t explain anything but here is an example. If there is a small chance lets say 1% that I will have to implement a standard to my logs and therefore have to replace all my console.log statements with a new custom log function, I should do that straight away. Refactoring takes a long time and it is even worse in a dynamically typed language such as JavaScript. Writing that line of code is probably faster and easier for you than to actually find the Refractor or Find & Replace function in your IDE of choice. A truly lazy person will put the extra 10 seconds to write that line just so you can free up the minutes of refactoring later on.

Ok, I hear you. You tell me that you are the true lazy programmer and you are so lazy that you cannot even type out that single line of code. And yes this is after all your hacky script where you will never implement a logging convention. Well, let me present you this last argument. The line I have shown you counting spaces and the semicolon which I did not even bother typing is 36 characters long. Every single time you call any of the 3 types of log you are saving yourself typing ‘console.’ that is 8 saved characters. So if you write at least 5 ‘console.logs’ while writing your script(yes this counts if you typed them up and deleted them later on) then you are already saving keystrokes and that is something.

--

--