android 揭示动画_如何使用意图揭示函数名称使代码更好

android 揭示动画

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!

“发现功能JavaScript”BookAuthority评为最佳新功能编程书籍之一

Code is a way to communicate with developers reading it. Functions with intention-revealing names are easier to read. We read the function name and can understand its purpose. The function name is our tool for expressing intent on a piece of code.

代码是与阅读代码的开发人员进行交流的一种方式。 具有意图揭示名称的功能更易于阅读。 我们阅读了函数名称,并可以理解其用途。 函数名称是我们表达代码意图的工具。

Let’s look at a list of operations done in a functional style with the use of anonymous functions.

让我们看一下使用匿名函数以功能样式完成的操作列表

function getTodos(users){
  return todos
    .filter(todo => !todo.completed && todo.type === "RE")
    .map(todo => ({
      title : todo.title,
      userName : users[todo.userId].name
    }))
    .sort((todo1, todo2) =>  
      todo1.userName.localeCompare(todo2.userName));
}

Now check the same functionality refactored using functions with intention-revealing names.

现在,检查使用具有意图公开名称的功能重构的相同功能。

function isTopPriority(todo){
  return !todo.completed && todo.type === "RE";
}

function ascByUserName(todo1, todo2){
  return todo1.userName.localeCompare(todo2.userName);
}
  
function getTodos(users){
  function toViewModel(todo){
    return {
      title : todo.title,
      userName : users[todo.userId].name
    }
  }
  return todos.filter(isTopPriority)
              .map(toViewModel).sort(ascByUserName);
}

Function names give clarity to the code. With a good function name, you just have to read the name — you don’t need to analyze its code to understand what it does.

函数名称使代码更清晰。 有了一个好的函数名,您只需要阅读名称即可—您无需分析其代码即可了解其功能。

It’s widely estimated that developers spend 70% of code maintenance time on reading to understand it.

据广泛估计,开发人员将70%的代码维护时间用于阅读以理解它。

Kyle Simpson in Functional-Light JavaScript

功能轻型JavaScript中的 Kyle Simpson

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!

发现功能JavaScript被称为 BookAuthority最好的新功能编程书籍

For more on applying functional programming techniques in React take a look at Functional React.

有关在React中应用函数式编程技术的更多信息,请查看 Functional React

Learn functional React, in a project-based way, with Functional Architecture with React and Redux.

通过带有React和Redux的功能架构 ,以基于项目的方式学习功能性React

Follow on Twitter

在Twitter上关注

翻译自: https://www.freecodecamp.org/news/how-to-make-your-code-better-with-intention-revealing-function-names-6c8b5271693e/

android 揭示动画

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值