将长变量重新分配为本地缩写会不好吗? [英] Is it bad style to reassign long variables as a local abbreviation?

查看:73
本文介绍了将长变量重新分配为本地缩写会不好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更喜欢使用长标识符来使我的代码在语义上清晰明了,但是在重复引用同一标识符的情况下,我希望它在当前范围内摆脱困境".以Python为例:

I prefer to use long identifiers to keep my code semantically clear, but in the case of repeated references to the same identifier, I'd like for it to "get out of the way" in the current scope. Take this example in Python:

def define_many_mappings_1(self):
    self.define_bidirectional_parameter_mapping("status", "current_status")
    self.define_bidirectional_parameter_mapping("id", "unique_id")
    self.define_bidirectional_parameter_mapping("location", "coordinates")
    #etc...

让我们假设我真的想坚持使用这种长方法名,并且这些参数总是要硬编码的. 实施方案1感觉不对,因为每一行的大部分都是重复的字符.这些行通常也很长,当嵌套在类定义和/或try/except块中时,它们很容易超过80个字符,从而导致丑陋的换行.让我们尝试使用一个for循环:

Let's assume that I really want to stick with this long method name, and that these arguments are always going to be hard-coded. Implementation 1 feels wrong because most of each line is taken up with a repetition of characters. The lines are also rather long in general, and will exceed 80 characters easily when nested inside of a class definition and/or a try/except block, resulting in ugly line wrapping. Let's try using a for loop:

def define_many_mappings_2(self):
    mappings = [("status", "current_status"),
                ("id", "unique_id"),
                ("location", "coordinates")]
    for mapping in mappings:
        self.define_parameter_mapping(*mapping)

在实现2的保护下,我将所有类似的迭代技术集中在一起,该技术具有将唯一的"自变量与重复的"方法名分离的改进.但是,我不喜欢将参数放在传递参数的方法之前,这很令人困惑.我希望保留动词后跟直接宾语"的语法.

I'm going to lump together all similar iterative techniques under the umbrella of Implementation 2, which has the improvement of separating the "unique" arguments from the "repeated" method name. However, I dislike that this has the effect of placing the arguments before the method they're being passed into, which is confusing. I would prefer to retain the "verb followed by direct object" syntax.

我发现自己在使用以下方法作为妥协:

I've found myself using the following as a compromise:

def define_many_mappings_3(self):
    d = self.define_bidirectional_parameter_mapping
    d("status", "current_status")
    d("id", "unique_id")
    d("location", "coordinates")

在实现3中,long方法的别名是一个非常短的缩写"变量.我喜欢这种方法,因为乍一看,它可以立即识别为一组重复的方法,同时具有较少的冗余字符和较短的行.缺点是使用了非常短且语义上不清楚的标识符"d".

In Implementation 3, the long method is aliased by an extremely short "abbreviation" variable. I like this approach because it is immediately recognizable as a set of repeated method calls on first glance while having less redundant characters and much shorter lines. The drawback is the usage of an extremely short and semantically unclear identifier "d".

最易读的解决方案是什么?如果缩写变量"的使用是从本地范围中的未缩写版本中明确分配的,则可以接受吗?

What is the most readable solution? Is the usage of an "abbreviation variable" acceptable if it is explicitly assigned from an unabbreviated version in the local scope?

推荐答案

我会执行实施2,但这是一个封闭的调用.

I would go with Implementation 2, but it is a close call.

我认为#2和#3具有相同的可读性.想象一下,如果您有100个映射...无论哪种方式,如果不滚动到顶部,我都无法分辨底部的代码在做什么.在#2中,您要为数据命名;在#3中,您要为函数命名.基本上是洗.

I think #2 and #3 are equally readable. Imagine if you had 100s of mappings... Either way, I cannot tell what the code at the bottom is doing without scrolling to the top. In #2 you are giving a name to the data; in #3, you are giving a name to the function. It's basically a wash.

更改数据也是一件容易的事,因为无论哪种方式,您都只能以与现有内容相同的模式添加一行.

Changing the data is also a wash, since either way you just add one line in the same pattern as what is already there.

如果要更改对数据所做的操作,则会有区别.例如,假设您决定为定义的每个映射添加调试消息.使用#2,您可以在循环中添加一条语句,并且仍然很容易阅读.使用#3,您必须创建lambda之类的东西. Lambda没什么错-我和任何人一样都爱Lisp-但我认为我仍然会发现#2更易于阅读和修改.

The difference comes if you want to change what you are doing to the data. For example, say you decide to add a debug message for each mapping you define. With #2, you add a statement to the loop, and it is still easy to read. With #3, you have to create a lambda or something. Nothing wrong with lambdas -- I love Lisp as much as anybody -- but I think I would still find #2 easier to read and modify.

但这是一个近距离通话,您的口味可能会有所不同.

But it is a close call, and your taste might be different.

这篇关于将长变量重新分配为本地缩写会不好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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