The Secret of Simple Code

栏目: IT技术 · 发布时间: 4年前

The Secret of Simple Code

Is there a shortcut to becoming a 10x developer? Is there some magical secret that — if you only knew it — would unlock a whole new world of software development mastery and productivity for you?

This is where the doubters are thinking “There are no shortcuts! Everybody needs to practice to get good!” And that’s true enough, but what are the experts of software productivity practicing, 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 give it away and spell it out for you in detail— it might take you 10 years to grow into and fully appreciate the simplicity of it.

At least, that’s what happened to me. It was spelled out to me in plain English by my high school programming teacher. I was walked step-by-step through the process of applying it using some example code. And it didn’t really sink in until 10 years later. But now, with the benefit of experience, it’s a lesson I appreciate profoundly, and even though I know it’s a lesson you can’t truly appreciate at first glance — I’m going to share it with you.

This secret is a key difference between average productivity and 10x productivity. Using the leverage that this secret provides, you can be orders of magnitude more efficient.

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

The secret to being 10x more productive is to gain a mastery of abstraction. A lot of developers treat “abstraction” like it’s a dirty word. You’ll hear (otherwise good) advice like, “don’t abstract too early” or Zen of Python’s famous “explicit is better than implicit,” implying that concrete is better than abstract. And all of that is good advice — depending on context.

But modern apps use a huge amount of code. If you printed out the source code of modern top 10 applications, those stacks of paper would compete with the height of skyscrapers, and software costs a lot of money to maintain. The more code you create, the more it costs.

Abstraction is the Key to Simple Code

The right abstractions can make code more readable, adaptable, and maintainable by hiding details which are unimportant for the current context, and reducing the amount of code required to do the same work — often by orders of magnitude.

“Simplicity is about

subtracting the obvious

and adding the meaningful.”

~ John Maeda: The Laws of Simplicity

Abstraction is not a 1-way street. It’s really formed by two complementary concepts:

  • Generalization — Removing the repeated parts (the obvious) and hiding them behind an abstraction.
  • Specialization — Applying the abstraction for a particular use-case, adding just what needs to be different (the meaningful).

Consider the following code:

There’s nothing inherently wrong with the code, but it contains a lot of details that may not be important for this particular application.

  • It includes details of the container/transport data structure being used (the array), meaning that it will only work with arrays. It contains a state shape dependency.
  • It includes the iteration logic, meaning that if you need other operations which also need to visit each element in the data structure, you’d need to repeat very similar iteration logic in that code, as well. It forces repetition which could violate DRY (Don’t Repeat Yourself).
  • It includes an explicit assignment, rather than declaratively describing the operation to be performed. It’s verbose.

None of that is necessary. All of it can be hidden behind an abstraction. In this case, an abstraction that is so universal, it has transformed the way modern applications are built and reduced the number of explicit for-loops we need to write.

“If you touch one thing with deep awareness, you touch everything.”

~ Thich Nhat Hanh

Using the map operation, we can reduce the code to a one-liner by removing the obvious (the parts we’re likely to repeat in similar code), and focusing on the meaningful (just the stuff that needs to be different for our use case:

Junior developersthink they have to write a lot of code to produce a lot of value.

Senior developersunderstand the value of the code that nobody needed to write.

Imagine being the coder who popularized the use of the map operation in programming languages like JavaScript. Map abstracts away details such as the type of data you’re mapping over, the type of data structure containing the data, and the iteration logic required to enumerate each data node in the data structure. It’s improved the efficiency of every app I’ve built in the past decade.

Jeremy Ashkenasmade several such operations popular in JavaScript, and paved the way for many of the great syntax shortcuts we take for granted now in JavaScript by pioneering their use in CoffeeScript. He made Underscore, which spawned Lodash (still the most popular functional programming utility belt in JavaScript), and Backbone, which popularized MVC architecture in JavaScript and set the stage for Angular and React.

John Resigmade jQuery, which was so popular and influential, it formed the biggest collection of reusable, encapsulated JavaScript modules (jQuery plugins) until standard Node modules and ES6 modules appeared several years later. jQuery’s selector API was so influential it forms the basis of today’s DOM selection APIs. I still benefit on a nearly daily basis from jQuery’s selection API when I unit test React components .

The right abstractions are powerful levers that can impact productivity dramatically. Abstraction is not a dirty word. Modules, functions, variables, classes — all of these are forms of abstraction and the entire reason any of them exist is to make abstraction and composition of abstractions easier.

You can’t build complex software without abstractions. Even assembly language uses abstractions — names for instructions, variables for memory addresses, code points to jump to for subroutines (like function calls), etc. Modern software is a layer cake of useful abstractions, and those layers give you leverage.

“Give me a lever long enough and a fulcrum on which to place it, and I shall move the world.”
~ Archimedes

The key to simplicity: The secret we’re after — is how to reduce the mountain of code we’re producing — how to get a lot more done with a lot less. When you master that, you will be a 10x programmer. I guarantee it.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

机械设计实践

机械设计实践

村洋太郎(日) / 王启义/等 / 机械工业出版社 / 1998-08 / 36.00

本书记述了各种设计过程的思考方法和具体作法以及必要的知识和具 体数据。介绍了设计中要决定的内容和相应的制约条件。如功能、机构、 构造、形状、力和强度、尺寸加工工艺、工具、材料、机械要素等。最后 介绍了具体设计实例。本书的目的在于即使不看其他的书和参考书就能设 计出所需要的具体机械。 本书供从事机械设计的有关技术人员及大专院校相关专业的师生使 用。一起来看看 《机械设计实践》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器