JavaScript Programming Style
... that could be enough for most of the time
If you have spent certain time in searching your preferred JavaScript programming style this post might be of interest to you.
Actually, before picking your style you should define what are your priorities. You know that everything is a trade-off. So, priorities are important. I value time. That is why I strive for simple, maintainable, self-documented, and predictable code.
With these priorities in mind read on. The practices described here might help you to achive them.
# Focus on The High Level Style
Code style is a solved problem. We want to solve actual problems.
My current choice for code style is happiness but I beleive that in long run I could go through semistandard to standard and finally to prettier. What a community right?
Climbing up the “programming style stairs” we could see some valuable practices in Airbnb guide as well as in the JavaScript adaptation of clean code principles.
Yes. Happiness is everything you need to achieve consistently looking codebase. One specific aspect that I would like to discuss is about using ‘const’. Here is my strategy. Use ‘const’ for reference types. Use ‘const’ for primitives that you want to be constants. Use ‘var’ for primitive types that you intend to change over time e.g. counters, flags etc. Why? First of all you should know that ‘const’ is not about immutability. You should use it to be kind to those that still do not know what ‘binding’ and ‘rebinding’ is. You should use it to make your programs less error-prone.
That is everything about code style. Lets focus on the more important thing, namely, the high-level style.
# Favor Maintenance over Performance
This could be inferred from the priorities list above. Still lets do a further discussion.
It always comes the time when someone is trying to understand a piece of code written by you in the past. It does not matter who is this guy — your colleague or yourself. This is hard. In either case. In most of the times. Unless you take care about the maintainability in advance. If you do this you will prevent or reduce the maintenance nightmare. So keep in mind:
- Less code is more maintainable code.
- Simple code with less constructs is more maintainable code.
- More iterations is more maintainable code. Do not stick to your first solution. Do several iterations before you ship. After each iteration your solution become more concise.
Think it that way the more simple and maintainable your code is the more free time you gonna have in the long run!
# Reduce “the consumption” of (syntactic) sugar
Less Constructs = Easier Maintenance. Remember? Now you think about ES6 and the huge number of constructs it has. But, hey, we are lucky of being able to add ES6 features to our set of constructs incrementally and only if we need them. Yes, I like template literals. Yes, I use them everywhere. Why? Because they are useful and make my code more maintainable. So, think twice before you start using certain new feature. What problem do you solve with it? Isn’t it just a sugar added by highly skilled, highly opinionated, performance addicted member of the technical committee?
# Pick You Favorite General Structure
Here we go. As simple as that:
- Declarations on Top of Each Function
Having variable declarations in blocks (if else etc.) are probably micro-optimizations that breaks code readability. Actually, that is why I am not sure I need ‘let’ at all; ‘const’ and ‘var’ are just fine.
- Main Logic on Top of the File After Declarations
- Function Declarations on Bottom (or in case they are reusable in external files exposed as API)
And, yes, probably you should never do this:
var varName = function() {}
Is there a case at all where this construct adds value?
# Create Objects and APIs in Simplest Possible Way
How to create objects?
Get pltod’s stories in your inbox
Join Medium for free to get updates from this writer.
Currently I am trying to avoid classes and OOP style. Yes, I am doing it.
The consequence is that I am not dealing with ‘this’.
Another consequence is that I am not dealing with ‘bind’ function.
Another consequence is that I kind of not dealing with prototype chain resolution, I need only scope chain resolution.
To create objects I use the power of node modules to export a single factory function. I invoke the factory to provide context. As a result I get an API that works in the provided context.
That is everything you need in most of the cases! If it is not clear from this post just watch this amazing talk.
# Make your App Agnostic to Change with Facades
The less our apps depends on third party modules the better. Still we need third party modules for sure. Weaving many small modules is the nature of modern architectures. Using facades is, in most of the times, a good solution to make the application agnostic to change in its dependencies.
Furthermore, If we need to replace specific technology we will not break our program on many places. Instead the needed changes will be isolated in your facade.
Here is an extra tip if want to know how to structure a complete app. Just add to your facades, both, app modules, and mediator between them.
# Shape Your Program like a Set of Pipes
We should imagine our programs like a set of pipes. We should strive for this. Basically we pass data to a pipe, the data is transformed and provided along to the next pipe. But let’s stop here with the metaphor. Let’s mention something real. Streams. You probably need an elevator pitch. Here it is:
Streams in node are one of the rare occasions when doing something the fast way is actually easier.
It is time to finish this.
- Use simple data construct like arrays and object literals
- Use the ‘streams advantage’
- Use less constructs. Remember that “the less powerful the language is, the more you can do with the data stored in that language”
- Use libraries instead of frameworks. Frameworks are just that -frameworks. They lock you data. They make you change your code in a way you would never write it otherwise.
Resources
All the links provided in this post were amazing sources of inspiration and knowledge for me. Thanks! Read the linked materials and you will defenitely learn a lot.





