如何编写代码(无需阅读)?

Do we need to read code before editing it?

在编辑代码之前,我们需要阅读代码吗?

The idea isn’t as wild as it sounds. In order to safely fix a bug or update a feature, we may need to learn some things about the code. However, we’d prefer to learn only that information. Not only does extra reading waste time, it overcomplicates our mental model. As our model grows, we’re more likely to get confused and lose track of critical info.

这个想法并不像它听起来那么疯狂。为了安全地修复一个错误或更新一个功能,我们可能需要学习一些关于代码的东西。然而,我们更愿意学习这些信息。额外的阅读不仅浪费时间,而且会使我们的心理模型过度复杂化。随着我们的模型越来越大,我们更有可能感到困惑并失去对关键信息的追踪。

But can we really get away with reading nothing? Well, let’s see how close we can get.

但是,我们真的可以什么都不读就离开吗?好吧,让我们看看我们能有多接近。

Find the Entrypoint

寻找切入点

If we’re refactoring code, we already know where we need to edit. Otherwise, we’re changing a behavior that has side effects. In a backend context, these behaviors would usually be exposed APIs. On the frontend, this would usually be something that’s displayed on the screen. For the sake of example, we’ll imagine a mobile application using React Native and Typescript, but this process generalizes to other contexts (as long as they have some concept of build or test errors; more on this later).

如果我们正在重构代码,我们已经知道我们需要编辑的地方。否则,我们将改变一个有副作用的行为。在后端环境中,这些行为通常是暴露的API。在前台,这通常是在屏幕上显示的东西。为了举例说明,我们将想象一个使用React Native和Typescript的移动应用程序,但这个过程也适用于其他情况(只要他们有一些构建或测试错误的概念;后面会有更多内容)。

If our goal was to read a lot of code, we might search for all hits on RelevantFeatureName. But we don’t want to do that. Even if we weren’t trying to minimize reading, we’ll run into problems if the code we need to modify is called AlternateFeatureName, SubfeatureName, or LegacyFeatureNameNoOneRemembersAnymore.

如果我们的目标是阅读大量的代码,我们可能会在RelevantFeatureName 上搜索所有的点击。但我们并不想这样做。即使我们不是想尽量减少阅读,如果我们需要修改的代码被称为AlternateFeatureNameSubfeatureName 、或LegacyFeatureNameNoOneRemembersAnymore ,我们也会遇到问题。

Instead, we’ll look for something external: the user-visible strings (including accessibility labels—we did remember to add those, right?) on the screen we’re interested in. We search vario...

开通本站会员,查看完整译文。

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.132.0. UTC+08:00, 2024-09-21 10:53
浙ICP备14020137号-1 $访客地图$