在不使用返回值的情况下执行映射操作是否是反模式? [英] Is performing a mapping operation without using returned value an antipattern?

查看:93
本文介绍了在不使用返回值的情况下执行映射操作是否是反模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个列表,我想使用映射函数向其中添加一些值:

Lets say i have a list and i want to add some values into it using a mapping function:

const arr = [1, 2, 3, 4, 5];
const anotherArr = [];

我使用功能性方法进行此操作:

I do this using a functional approach:

arr.map((item) => anotherArr.push(item);

这是否是反模式/错误的逻辑-也就是说,不对任何东西使用映射操作返回值?有什么好的资源吗?

Is this an antipattern / bad logic - that is, not using the mapping operation return value for anything? Are there any good resources on this?

(我知道这种逻辑很愚蠢,我只能复制列表-这不是我要问的重点)

(i know this logic is silly and i can just copy the list - that is not the point of my question)

推荐答案

是的,这是一种反模式.尽管您可以辩称这不是事实,但这只是简单的滥用.我之所以称其为反模式,是因为出于某些原因 经常发生,人们经常在不使用.map的情况下使用它们.

Yes, this is an anti-pattern. Although you could argue it isn't and it's just plain misuse. I'd call it an anti-pattern because for some reason there is a frequent widespread incidents of people using .map when they shouldn't.

该术语来自数学,您可以在其中映射

The term comes from mathematics where you can map from one category to another. For example, shapes (X) to colours (Y):

(图片来自Wikipedia)

(image from Wikipedia)

该术语在计算机科学中也有明确的定义,其中是执行此类转换的高阶函数.在JavaScript中,它是一种数组方法,用法很明确-您可以将一个数组的内容转换为另一个数组的内容.给定数组X = [0, 5, 8, 3, 2, 1],我们可以使用.map方法对其应用x => x + 1.

The term is also well established in computer science where map is a higher order function doing this sort of conversion. In JavaScript, it's an array method and has clear usage - you transform the contents of one array into another. Given array X = [0, 5, 8, 3, 2, 1] we can apply x => x + 1 to it using the .map method.

(图片来自维基百科)

(Image from Wikipedia)

这不仅仅是实现的具体细节,而且范围更广-.map惯用的,如果滥用,会使代码更难于阅读和理解.让我们做一个分步示例:

This is more wide-reaching than just the specifics of the implementation - .map is idiomatic and if misused makes code harder to read and understand. Let's do a step-by step example:

我们需要一个映射函数来表达元素之间的关系.例如,可以通过以下功能来表达将字母转换为字母的位置:

We need a mapping function that expresses the relationship between elements. For example transforming a letter to its position in the alphabet can be expressed via the function:

function letterToPositionInAlphabet(letter) {
  return letter.toUpperCase().charCodeAt(0) - 64;
}

因此,通过此函数映射字母数组将为您提供每个位置的数组:

So mapping an array of letters via this function will give you an array with each of their positions:

function letterToPositionInAlphabet(letter) {
  return letter.toUpperCase().charCodeAt(0) - 64;
}

const letters = ["a", "b", "c"];

console.log(letters.map(letterToPositionInAlphabet));

映射操作是一个习惯用法,也是理解代码的一部分.如果您看到someArr.map(someFn)设置了期望值,那么就很容易了解发生了哪种操作,而无需知道数组或函数的内容.当您看到letters.map(letterToPositionInAlphabet)时,要弄清意图是不重要的-获取某些字母的字母位置.这是自记录代码,除非另有说明,否则我们可以假设代码是正确的.

The mapping operation is an idiom and part of understanding the code. If you see someArr.map(someFn) sets up expectations and it's easy to understand what sort of operation is happening, without needing to known the contents of either the array or the function. When you see letters.map(letterToPositionInAlphabet) it should be trivial to understand what the intent is - get the positions in the alphabet of some letters. This is self-documenting code, we can assume the code is correct unless proven otherwise.

但是,将.map用作.forEach会破坏预期的含义,并且可能会使阅读混淆.有这个

However, using .map as .forEach is breaking that intended meaning and can be confusing to read. Having this

function playerToPlaceInRankList(player) {
   const position = lookupPlayerRank(player);
   positionsArr.push(position);
}

/* many lines later */

players.map(playerToPlaceInRankList);

/* more code */

看起来像的那行执行映射也立即看起来是错误的,因为返回值被忽略了.要么不需要该行,要么您必须检查playerToPlaceInRankList的作用以找出此处实际发生的情况.仅阅读应直接的代码和自我记录的代码,这是不必要的精神负担.

The line which seems like it performs mapping also immediately looks wrong because the return value is ignored. Either that line is not needed, or you have to examine what playerToPlaceInRankList does in order to find out what is actually happening here. That's unnecessary mental load for just reading what should be a straight forward line and self-documenting line of code.

这同样适用于使用其他方法,例如.filter.find.every.some等.不要仅因为它们遍历数组而使用它们,如果您想要的不是它们打算这样做.

The same applies to using other methods like .filter, .find, .every, .some, etc. Don't use those just because they iterate over the array, if what you want is not what they are intended to do.

这篇关于在不使用返回值的情况下执行映射操作是否是反模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆