python运行时不要闪退

Long ago a wise old developer gave me a piece of advice that I didn’t fully appreciate until very recently.

很久以前,一个明智的老开发人员给了我一些建议,直到最近我才完全意识到。

We were in a code review looking at some feature that required the program to output a list of letters from A-Z (think of a list of contacts with a set of buttons that allow you to skip down to names starting with a certain letter — that kind of thing).

我们在一次代码审查中查看了一些功能,该功能需要程序从AZ输出字母列表(请考虑带有一组按钮的联系人列表,这些按钮可让您跳至以某个字母开头的名称-这种类型的东西)。

So, along came some young hot-shot. (OK — it was me.) I decided that rather than just hard-coding an array of all the letters, it would be easier to write a for loop that iterated from 65–90, then use the values to generate the letters from their char codes.

因此,随之而来的是一些年轻的热门人物。 (好吧,是我。)我决定,不只是对所有字母的数组进行硬编码,编写一个从65–90迭代的for循环,然后使用值从它们的字母生成字母会更容易。字符代码。

The JavaScript equivalent would be something like:

相当于JavaScript的类似:

for (let i = 65; i <= 90; i++) {  letters.push(String.fromCharCode(i))}

The wise old developer looked at me and asked why I hadn’t just hard-coded the array. It wasn’t as if the alphabet was going to be different from one session to the next. So why bother calculating it every time?

明智的老开发人员看着我,问我为什么不对数组进行硬编码。 好像字母在一个会话与下一个会话之间不会有所不同。 那么,为什么每次都要计算呢?

I was aghast. “You can’t really expect me to hand type every single letter like some child. I am a professional software developer! I have algorithms and data structures, and a math co-processor for heaven’s sake!”

我吓坏了。 “您真的不能期望我像每个孩子一样手写每个字母。 我是专业的软件开发人员! 我有算法和数据结构,还有一个数学协处理器,这是天堂!”

“Fine,” he said. “Then use them at design-time to generate the array for you, and then copy/paste it into the production code.”

“很好,”他说。 “然后在设计时使用它们为您生成阵列,然后将其复制/粘贴到生产代码中。”

And then he said this:

然后他说:

“Avoid doing at runtime what you can do at design time”

“避免在运行时执行设计时可以执行的操作”

Now, let’s be honest. My little for-loop wasn’t about to bring the application to a grinding halt. And today’s machines would crunch through that code so fast, no one would even notice. But as a general principle, it’s sage advice.

现在,说实话。 我的小for循环不会使应用程序陷入停顿。 如今的机器如此之快地处理这些代码,甚至没人注意到。 但作为一般原则,这是明智的建议。

Too often we write code that transforms rarely-changing data from one format to another on every request.

我们经常编写代码,在每次请求时将很少更改的数据从一种格式转换为另一种格式。

Think of all the round-trips to fetch a piece of content from a database that might change once or twice a year, format it, and forward it to the browser, needlessly slowing down our apps.

考虑所有从数据库中获取内容的往返过程,这些内容可能每年更改一次或两次,对其进行格式化,然后将其转发给浏览器,这会不必要地减慢我们的应用程序的速度。

This is especially true for sites that are tied to a content management system.

对于与内容管理系统绑定的网站尤其如此。

This is why I think established players like Wordpress, Drupal, and the like will face a credible challenge in the years to come from static site generators like Gatsby, Hugo, or Jekyll paired with a smooth build process, headless CMS, cheap CDNs, and a fast continuous integration workflow.

这就是为什么我认为,诸如Gatsby , Hugo或Jekyll之类的静态网站生成器,平滑的构建过程, 无头CMS ,廉价CDN和快速连续的集成工作流程。

This pattern has been dubbed JAMstack, which stands for “JavaScript, APIs and Markup stack.” And the results are quite impressive.

该模式被称为JAMstack ,代表“ JavaScript,API和标记堆栈”。 结果令人印象深刻 。

The wise old developer’s advice echoes in my ears: “Avoid doing at run-time what you can do at design time.” And as time has worn on, I’ve realized that this piece of advice has far-reaching implications. Not just for software development, but for life too.

明智的老开发人员的建议在我耳边回响:“避免在运行时执行设计时可以执行的操作。” 随着时间的流逝,我意识到这条建议具有深远的意义。 不仅用于软件开发,而且也用于生活。

Recently, I’ve been reading a great book called “Principles: Work and Life” by Ray Dalio. A central theme of the book is that there are far fewer types of problem than there are actual problems. So if you do the work ahead of time and figure out how you would approach a particular type of problem you’re likely to face, then when it does arrive, you’ll be far better equipped to deal with it.

最近,我一直在阅读Ray Dalio撰写的一本很棒的书,名为“原理:工作与生活” 。 这本书的中心主题是问题的类型比实际的问题少得多。 因此,如果您提前进行工作并弄清楚如何处理您可能会遇到的特定类型的问题,那么当问题确实出现时,您将可以更好地应对它。

In essence, you can make better decisions more quickly by sorting out your approach to different problem types at “design time”, when you’re calmly reflecting on life, instead of at “runtime”, when you’re faced with an actual problem in the moment and panicking.

本质上,当您从容应对生活时,在“设计时”冷静地思考生活,而不是在“运行时”面对实际问题时,通过整理针对不同问题类型的方法,可以更快地做出更好的决策。在当下和惊慌失措。

Dalio implemented this technique by cataloging his approaches as a set of principles. He even went so far as to codify his decision-making process into a set of computer algorithms that he could test against vast amounts of historical data.

达里奥(Dalio)通过将他的方法归类为一套原则来实现了这一技术。 他甚至将决策过程编纂为一套计算机算法,可以针对大量历史数据进行测试。

Given that he’s a multi-billionaire who runs a very successful investment company, I’d say it’s worked out.

考虑到他是一个拥有十亿美元资产的亿万富翁,他经营着一家非常成功的投资公司,所以我想说它已经完成了。

In fact, Wall Street is starting to hire more computer programmers than stock traders. So if you had any doubts that you chose the right profession, there’s more proof that software is eating the world.

实际上, 华尔街开始雇用比股票交易员更多的计算机程序员 。 因此,如果您对选择正确的职业有任何疑问,则有更多的证据表明软件正在吞噬整个世界。

I shared my own advice and lessons-learned in a recent interview on the Developer On Fire podcast which you can listen to here.

我在最近关于“开发人员放火”播客的采访中分享了自己的建议和经验教训,您可以在此处收听。

Episode 299 | Bill Sourour - Paying It ForwardBill Sourour talks with Dave Rael about lessons learned the hard way, making lessons accessible, software consulting…dev.to

第299话 Bill Sourour-向前 付账Bill Sourour与Dave Rael进行了会谈,讨论了以艰难的方式汲取的教训,使课程易于访问,软件咨询…… dev.to

You can learn more about JAMstack at Jamstack.org

您可以在Jamstack.org上了解有关JAMstack的更多信息。

JAMstack | JavaScript, APIs, and MarkupWhen we talk about "The Stack," we no longer talk about operating systems, specific web servers, backend programming…www.jamstack.org

JAMstack | JavaScript,API和标记 当我们谈论“堆栈”时,我们不再谈论操作系统,特定的Web服务器,后端编程…… www.jamstack.org

There’s also a nice round-up of static site generators on the Netlify blog at:

Netlify博客上还有一个不错的静态站点生成器汇总:

Top Ten Static Site Generators of 2017 | NetlifyIn this countdown, we will review the top ten static website generators of 2017 so far.www.netlify.com

2017年十大静态网站生成器| Netlify 在本次倒计时中,我们将回顾2017年迄今为止的十大静态网站生成器。 www.netlify.com

And here’s an article about a specific stack that I recently reviewed and recommended which uses a combination of Gatsby, Contentful, Netlify, and Algolia as an alternative to a traditional CMS for a documentation site:

以下是我最近查看并推荐的有关特定堆栈的文章,该文档使用Gatsby,Contentful,Netlify和Algolia的组合作为文档站点的传统CMS的替代方法:

Gatsby + Contentful + Netlify (and Algolia)Josh Weaver Developer at By the Book, Inc. Enjoys technology, writing and playing music. Can't turn down a decent board…www.gatsbyjs.org

Gatsby + Contentful + Netlify(和Algolia) By the Book,Inc.的Josh Weaver开发人员 喜欢 技术,写作和播放音乐。 无法拒绝一个体面的董事会…… www.gatsbyjs.org

This post originally appeared in the Dev Mastery Newsletter which I send out regularly to thousands of developers all over the world. Sign up below to get more content like this sent straight to your inbox.

该帖子最初出现在Dev Mastery通讯中 ,我定期将其发送给世界各地的数千名开发人员。 在下面注册以获取更多类似的内容,直接发送到您的收件箱。

If you enjoyed reading this, please help spread the word by smashing that applause icon a bunch of times. Thanks in advance!

如果您喜欢阅读本文,请通过多次敲击掌声来帮助宣传。 提前致谢!

python运行时不要闪退