用于库中类型转换的 asX 与 toX 前缀 [英] asX vs toX prefixes for type conversions in libraries

查看:22
本文介绍了用于库中类型转换的 asX 与 toX 前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要是一名 Android 开发人员,但我想同样的规则也应该适用于标准 Java 和 Kotlin.

I am primarily an Android developer but the same rules should also apply to standard Java and Kotlin I suppose.

  1. 选择as(例如asLiveData、asStateFlow)或to(例如toString)从一种类型转换为另一种类型的标准是什么?

  1. What is the criteria between choosing as (e.g asLiveData, asStateFlow) or to (e.g toString) for converting from one type to another?

现在是我真正的问题.我制作了几个扩展函数,可以将 ByteArray 转换为十六进制字符串,反之亦然.什么命名法更有意义?

And now my real question. I've made a couple of extension functions which convert a ByteArray to a hex String and vice versa. What nomenclature would make more sense?

String.toByteArrayFromHex 对比 String.asByteArrayFromHex

ByteArray.toHexString 对比 ByteArray.asHexString

推荐答案

我不认为 Kotlin 标准库中的这些命名约定有正式文档,但我注意到一些相当常见的模式.例如:

I don't think these naming conventions in the Kotlin standard library are officially documented, but I've noticed some fairly common patterns.  For example:

  • to()转换一个对象到所需的类型,并返回它.(原版不变,独立于新版.) toString() 当然是最常见的例子.

  • to<Type>()converts an object to the required type, and returns it.  (The original is unchanged, and independent of the new one.)  toString() is of course the most common example.

as() — 返回对象的视图.(不会立即更改原始状态;但是,稍后对对象状态的更改将反映在视图中,如果合适,反之亦然.)例如:asReversed().

as<Something>() — returns a view of the object.  (Doesn't change the original immediately; however, later changes to the state of object will be reflected in the view, and, if appropriate, vice versa.)  For example: asReversed().

()就地改变对象.(通常不返回任何内容.)例如:reverse().

<verb>()mutates the object in-place.  (Doesn't usually return anything.)  For example: reverse().

ed() — 返回对象的变异副本.(原件不变,独立于新件.)例如:reversed().

<verb>ed() — returns a mutated copy of the object.  (The original is unchanged, and independent of the new one.)  For example: reversed().

<xxx>OrNull() — 表示如果没有要返回的有效值,方法将返回 null(而不是抛出异常或返回其他一些默认值).>

<xxx>OrNull() — indicates that a method will return null if there's no valid value to return (instead of throwing an exception or returning some other default).

与所有约定一样,没有什么会强迫您使用这些命名模式——但如果您这样做,它们将更自然地融入现有代码,人们会发现它们使用起来更直观.

Like all conventions, there's nothing forcing you to use these naming patterns — but if you do, they'll fit more naturally into existing code, and people will find them more intuitive to use.

(并非所有库方法都遵循这些约定;例如,map()filter() 是动词,但不会改变它们的接收器.但是,它们早在 Kotlin 之前,他们坚持使用众所周知的现有名称是可以理解的.JetBrains 似乎更强烈地遵守这些约定;例如,在 Kotlin 1.4 中,他们弃用了 max() 函数支持新的 maxOrNull().)

(Not all library methods follow these conventions; for example, map() and filter() are verbs, but don't mutate their receiver.  However, they long pre-date Kotlin, so it's understandable that they stick with the well-known existing names.  And JetBrains seem to be adhering more strongly to these conventions; for example, in Kotlin 1.4 they deprecated the max() function in favour of the new maxOrNull().)

在您的示例中,ByteArray.toHexString() 将是一个非常好的名称,因为它将数组转换为新类型,而不与原始类型保持任何连接.我想任何人看到这个名字都会立即知道它的作用.(无论如何,或多或少;参数或文档必须指定详细信息,例如使用的分隔符(如果有)以及使用大写还是小写.)

In your examples, ByteArray.toHexString() would be a perfectly good name, as it converts the array into a new type, without keeping any connection to the original.  I think anyone seeing the name would immediately know what it does.  (More or less, anyway; params or docs would have to specify details such as what separator it used, if any, and whether it used upper or lower case.)

同样,String.toByteArrayFromHex() 比替代方案更清晰,但我不确定它是否读得好.String.hexToByteArray()怎么样?

Similarly, String.toByteArrayFromHex() is much clearer than the alternative, though I'm not sure it reads well.  How about String.hexToByteArray()?

这篇关于用于库中类型转换的 asX 与 toX 前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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