Abstraction is the key to simple code.

image


Is there a way to improve your skills 10 times? Is there any magic secret that - if you only knew this - would open for you a whole new world of skill and productivity in software development?



This is where the doubters think, “You can't cut corners here! Everyone has to practice to become good! " And this is true, but what do experts practice to speed up software development, and is there one key thing that can make a huge difference?



Yes! There is!



But even if I share it with you - even if I detail it for you - it may take you 10 years to grow up and fully appreciate its simplicity.



At least that's what happened to me. This was taught to me in plain English by my high school programming teacher. I went through the process of applying it step by step using some code examples. And this really happened only after 10 years. But now, through experience, I deeply appreciate this lesson, and although I know that you cannot really appreciate it at first glance, I am going to share it with you.



This secret is the key difference between average performance and 10x performance. By leveraging this secret, you can be orders of magnitude more effective.



You can write code that is more reusable and less likely to break when new requirements are introduced and changes occur in the code.



The secret to being 10 times more productive is mastering abstraction. Many developers regard “abstraction” as a dirty word. You might hear advice such as “don't abstract too early” or Zen Python's famous “explicit is better than implicit,” implying that the concrete is better than the abstract. And all these are good tips, depending on the context.



But modern applications use a huge amount of code. If you print the source code for the top 10 modern apps, these stacks of paper will compete with the height of skyscrapers, and software maintenance costs a lot of money. The more code you create, the more it costs.



Abstraction is the key to simple code.



Correct abstractions can make your code more readable, adaptable, and maintainable by hiding details that are not important to the current context and reducing the amount of code needed to do the same job — often multiple times.



“Simplicity is subtracting the obvious and adding meaningful.”

- John Maeda: The Laws of Simplicity


Abstraction is not a one-way street. It is formed by two additional concepts:



  • Generalization - removing repetitive parts (obvious) and hiding them behind abstraction.
  • Specialization - Applying abstraction for a specific use case, adding only what should be different (meaningfully).


Consider the following code:



const doubleList = list => {
  const newList = [];
  for (var i = 0; i < list.length; i++) {
    newList[i] = list[i] * 2;
  }
  return newList;
};


There is nothing wrong with the code, but it contains many details that may not be important for this particular application.



  • It includes details about the container data structure (array) being used, which means it will only work with arrays. It contains the dependency of the state form .
  • It includes iteration logic, which means that if you need other operations that also need to visit each item in the data structure, you will also need to repeat very similar iteration logic in this code. This induces repetition , which can violate the DRY principle (do not repeat yourself) .
  • It includes an explicit assignment rather than a declarative description of the operation to be performed. It is verbose .


None of this is needed. All this can be hidden behind abstraction. In this case, it is such a versatile abstraction that has changed the way we build modern applications and reduced the number of explicit for loops we need to write.



"If you touch one thing with deep awareness, you touch everything."

- Thih Nhat Han


Using the map operation, we can shorten the code to a one-liner, removing the obvious (the parts that we will probably repeat in similar code) and focusing on the meaningful (just what should be different for our use). Example:



const doubleList = list => list.map(x => x * 2);


Junior developers think they need to write a lot of code to get more value.



Senior developers understand the value of code that no one needed to write.



Imagine yourself a programmer who has popularized the use of the map operation in programming languages ​​such as JavaScript. Map abstracts away from such details as the type of data that you map, the type of data structure that contains the data, and the iteration logic needed to list each data node in the data structure. This has increased the efficiency of every application I have created over the past decade.



Jeremy Ashkenasmade several of these operations popular in JavaScript, and paved the way for many of the great syntax shortcuts that we now take for granted in JavaScript when we first used them in CoffeeScript. He created Underscore, which spawned Lodash (still the most popular functional programming tool in JavaScript) and Backbone, which popularized the MVC architecture in JavaScript and laid the foundation for Angular and React.



John Resigmade jQuery that was so popular and influential that it formed the largest collection of re-encapsulated JavaScript modules (jQuery plugins) until after a few years standard Node modules and ES6 modules appeared. The jQuery Selector API was so influential that it became the foundation of the modern DOM API. I benefit from this API almost daily when I test React components .



The right abstractions are powerful levers that can make a big difference in performance. Abstraction is not a dirty word. Modules, functions, variables, classes are all forms of abstraction, and the whole reason that they exist is to simplify abstraction and composing abstractions.



You cannot create complex programs without abstractions. Even assembly language uses abstractions — names for instructions, variables for memory addresses, code points for jumping to subroutines (such as function calls), and so on. Modern software is a layer of useful abstractions, and these layers give you the edge.



"Give me a lever long enough and a fulcrum that I can lean on, and I'll move the whole world."

- Archimedes


The key to simplicity: the secret we are looking for is how to reduce the amount of code we create - how to do much more with much less. When you master this, you will become a programmer 10 times better. I guarantee it.





image



Learn the details of how to get a sought-after profession from scratch or Level Up in skills and salary by completing SkillFactory paid online courses:






Read more






All Articles