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.


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

查看所有标签

猜你喜欢:

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

Flask Web开发:基于Python的Web应用开发实战

Flask Web开发:基于Python的Web应用开发实战

[美] Miguel Grinberg / 安道 / 人民邮电出版社 / 2014-12 / 59.00元

本书不仅适合初级Web开发人员学习阅读,更是Python程序员用来学习高级Web开发技术的优秀参考书。 • 学习Flask应用的基本结构,编写示例应用; • 使用必备的组件,包括模板、数据库、Web表单和电子邮件支持; • 使用包和模块构建可伸缩的大型应用; • 实现用户认证、角色和个人资料; • 在博客网站中重用模板、分页显示列表以及使用富文本; • 使用基于......一起来看看 《Flask Web开发:基于Python的Web应用开发实战》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具