开源日报 每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,坚持阅读《开源日报》,保持每日学习的好习惯。
今日推荐开源项目:《Ckplayer》
今日推荐英文原文:《Always Aim to Program for Simplicity》

今日推荐开源项目:《Ckplayer》传送门:项目链接
推荐理由:Ckplayer 是一款在网页上播放视频的开源软件,支持mp4,flv,m3u8及rtmp协议的直播,支持移动端,PC端。主要特点是:开源,功能强大,不依赖其它插件,适合二次开发。
今日推荐英文原文:《Always Aim to Program for Simplicity》作者:Zachary Minott
原文链接:https://medium.com/better-programming/always-aim-to-program-for-simplicity-49e434a424bd
推荐理由:不够简洁的代码就是写完以后不想再看一遍。

Always Aim to Program for Simplicity

Complexity can destroy the infrastructure of your code and kill your program

“Programming culture is broken on a global scale.” — Jonathon Blow
Stop trying to overcomplicate everything.

Just because you are faced with a complex problem doesn’t mean that it requires a complex solution. Unfortunately, many developers still write code that only they can understand or is very difficult for essentially anyone to understand.

That’s not how it’s supposed to be. Your programs should always take the simplest and most intuitive approach — not something that appears chaotic at first glance with overcomplicated logic, unnecessary levels of abstraction, and a broad purpose.

For instance, take this metaphor of a tree.

The Tree Metaphor

When you look at a tree, all you can really see are the leaves. But when you stand underneath a tree, you observe a new perspective that wasn’t seen before.

You can see exactly how the tree is structured and how every single branch is connected.

There is the base of the tree that serves as the foundation the tree stands upon.

From the base, larger branches of the tree emerge. Each of those branches then has even smaller branches connected to it, and even smaller branches connected to those smaller branches.

From those tiny branches, you can see how little twigs output little leaves, flowers, or maybe even fruit.

That’s how you should be able to perceive the code of your program — like a little network of different classes and functions where you can easily intuit what is connected to what, the purpose that each of those components serves, and what they are expected to output.

If your code executes efficiently and well enough, it’ll be able to grow leaves and fruit that the users you are targeting are able to enjoy.

Poorly written code, on the other hand, will cause the system you created to crumble, incapable of scaling even further or offering nothing fluid and tasty for your target audience to enjoy.

Reliability and Scalability Are Dependent on Simple Code

It appears that programmers lost their path and are straying further and further away from what engineering is really supposed to be.

With engineering, you’re supposed to make complex structures that appear simple and nimble — building a solid foundation for the programs that you write to stand upon.

The problem is that many programmers forget to build that solid foundation and think more in surface-level terms of just needing code that does X rather than diving deeper and creating code that can do X but is also readable, easily documented, well-designed, and scalable.

Just like the tree, the programs that you write need to be built with many small components (the branches) that can be beautifully formed together in creative ways to achieve the bigger solution (the tree as a whole).

If you just have a monolithic, overcomplicated mess of code that only you can understand, then you’re running the risk of not surviving the evolving features that the future will present.

Simplification of code doesn’t mean that you just need code that runs — despite that paradoxically seeming like the simpler approach.

It means that you must always keep the code that you write small, nimble, and composable.

More than anything, it means writing code specifically for the problem you are trying to solve and having to delete and refactor the code you write to be even smaller and simpler.

You need to first make it work, make it right, make it fast, and then reiterate that same cycle until you have the simplest solution.

Once you get to that simple solution, only then should you be considering the design principles you want to implement.

The founder of C++, Bjarne Stroustrup, states that in order to make code scalable and reliable:
“The first step is to simplify the code, have less code, and have code that is less likely to go wrong. It is not by runtime testing everything. It is not by big test frameworks that you are using [although we still do that]. The first step is actually to make sure that when you want to express something, that you can express it directly in code rather than going through endless loops and convolutions in your head before it gets down to code. When you express things directly, you can maintain it, it’s easier to find errors, it’s easier to make modifications, it’s easier to test it, and lo and behold, it runs faster.”

Remember That Complexity Kills Your Program

Imagine if a tree was an entanglement of complete randomness, where branches are coming out of the leaves, the base is in the middle of the tree, and the branches support the overall structure.

Obviously, that tree wouldn’t exist or even be capable of standing a chance against nature and gravity. It would crumble and fall apart in an instant.

Complexity can be seen in the same way, where although computers are extremely fast nowadays, complexity will kill everybody.

As genius game developer Jonathon Blow states:
“[Complexity] makes everything more complicated, makes users unhappy on multiple dimensions because they have to deal with more complex things, and because things work less well. It lowers the threshold of what we can even accomplish.”
Part of the problem lies in the way programming is taught today. People are taught frameworks rather than really understanding what code actually does and what it can actually be used for.

In that way, people are really learning how to use programs and frameworks that do all the work for them — like Unity and React — and are in turn making programmers used to simply following instructions rather than learning how to think. That doesn’t really teach them how a computer processes your code and how that memory is being allocated.

I didn’t really transcend to becoming a better programmer and designing optimal solutions until I learned what code is really doing under the hood.

Only after understanding those things did I really understand why it’s important to simplify everything that I write. It got me to really aim to design code in its most optimal and scalable fashion rather than just brute-force writing code that works and takes up mounds of memory and processing that would slowly chip away at the life force of my program over time.

Final Thoughts

You should always be aiming to code for simplicity. Always program to solve the problem that you are currently tackling and do it in a way that allows scalability.

You should be able to look at a piece of code and immediately discern what that piece of code does and how it is used in your overarching solution.

That being said, whatever code you write initially is just the first draft. You need to obsessively refactor your code and deconstruct your code to its simplest structure and purpose.

Do that and you’ll save yourself a lot of grief in the future and will be recognized as a better programmer because of it.


下载开源日报APP:https://openingsource.org/2579/
加入我们:https://openingsource.org/about/join/
关注我们:https://openingsource.org/about/love/