您使用什么类型的变量名来比较两个对象

时间:2021-11-12 23:03:39

I do a lot of data analysis scripting, comparing two objects or strings to determine differences or come up with gaps that need to be filled.
When you're in a loop, comparing object a to object b, do you have a preferred coding standard that makes sense (i.e., it is self-documenting) and can travel nicely to other code? Or are variable names irrelevant in this type of code:

我做了很多数据分析脚本,比较两个对象或字符串来确定差异或提出需要填补的空白。当你处于循环中时,将对象a与对象b进行比较,你是否有一个有意义的首选编码标准(即,它是自我记录的)并且可以很好地运行到其他代码?或者这类代码中的变量名称无关:

for (String a : TreeMapvar) {  
    // Read b from data source  
    if (a.equals(b)) {  
        // Update a  
    }  else {
        TreeMapvar.add(b);
}

A different example:

一个不同的例子:

TreeMap<String, MyObject> TreeMapvar = new TreeMap<String, Object>();
File f = "Somefile";
// Open the file with bufferedReader br  
while ((line=br.readLine)!=null)) {  
    //insert code parse line to get object a  
    if (TreeMapvar.containsKey(a)) {  
        if (TreeMapvar.get(a).somefield.equals(a.somefied)) {  
            //insert code to update a  
        } else {  
            //insert code to insert new object  
        }  
     }  
}

5 个解决方案

#1


Well, if you need 2 variable names, let's call them a and b, then you probably have 3 objects involved: self, a, b.

好吧,如果你需要2个变量名,让我们称它们为a和b,那么你可能有3个对象:self,a,b。

Now, that's quite a bunch for me, I try to keep things smaller.

现在,这对我来说相当多,我试着让事情变得更小。

So, consider your first example:

所以,考虑你的第一个例子:

for (String a : TreeMapvar) {  
    // Read b from data source  
    if (a.equals(b)) {  
        // Update a  
    }  else {
        TreeMapvar.add(b);
}

So, why not have your whole snippet be part of the b class definition and rename b to "this"?

那么,为什么不让你的整个片段成为b类定义的一部分并将b重命名为“this”?

lookAtYourTreeMap() {
    for( String a : TreeMapvar) {
      this.accomodateTreemapIfNecessaryTo(a);
    }
}

And then I would have a method

然后我会有一个方法

accomodateTreemapIfNecessaryTo(a) {
  if(a .equals( this)) {
     a.update();
  } else {
    TreeMapVar.add(a);
  }
}

Notice that the inventor of the modern computer, Kent Beck, says that, methods need to be short.

请注意,现代计算机的发明者Kent Beck说,方法需要简短。

Start the whole thing in your third component like this:

像这样在你的第三个组件中启动整个事情:

B b = loadBFromSource();
b.lookAtYourTreemap();

Loops like yours I would call "structural code".

像你这样的循环我称之为“结构代码”。

#2


I think variable names are not at all irrelevant, but also that there's no good generic name that would fit all possible cases in such a loop. It depends on what is stored in the TreeMap and what does the data source contain. Nevertheless, we could say something like

我认为变量名称根本不相关,但是在这样的循环中没有适合所有可能情况的通用名称。它取决于TreeMap中存储的内容以及数据源包含的内容。不过,我们可以这样说

for (String localData : TreeMapvar) {  
    // Read remoteData from data source  
    if (localData.equals(remoteData)) {  
        // Update or add to a counter  
    }  
}

if I got your drift.

如果我得到你的漂移。

The thing is that the comparison is made evident by the use of .equals() so we shouldn't need to use variable names (in general) to say that the values are going to be compared, what's not evident (and thus more important) is what values are being compared.

问题在于,使用.equals()可以明显地进行比较,因此我们不需要使用变量名(一般来说)来表示将要比较值,哪些不明显(因此更重要) )是被比较的价值。

EDIT: Your second example changes nothing. You are still using lousy varnames a and b where you should be using descriptive ones showing what a and b actually represent. I still see the comparison clear, so that's not relevant to naming.

编辑:你的第二个例子没有改变。你还在使用糟糕的varnames a和b,在那里你应该使用描述a和b实际表示的描述。我仍然看到比较清楚,因此与命名无关。

EDIT (re, your comment): That's easy. The only needed thing is some context, let's say the Map contains usernames associated to whatever you like and you are comparing them against existing user information.

编辑(重新,你的评论):这很容易。唯一需要的是一些上下文,假设Map包含与您喜欢的任何内容相关联的用户名,并且您将它们与现有用户信息进行比较。

for (String a : TreeMapvar) {  
    // Read b from data source  
    if (a.equals(b)) {  
        // Update or add to a counter  
    }  
}

for (String userName : TreeMapvar) {  
    String existingUserName = getNextUserName();
    if (userName.equals(existingUserName)) {  
        // Update or add to a counter  
    }  
}

#3


Variable names should enhance the code readability, in your example (without trying to modify the code):

在您的示例中,变量名称应该增强代码可读性(不尝试修改代码):

  1. a could be named something like baseValue
  2. a可以命名为baseValue

  3. b could be named something like compareToValue or testValue
  4. b可以命名为compareToValue或testValue

so if (a.equals(b)) {
becomes if (baseValue.equals(compareToValue)) {

所以if(a.equals(b)){成为if(baseValue.equals(compareToValue)){

#4


If the language in question implements operator overloading, then I would implements the == operator. If it doesn't, then you can either use equals as above or implement "Compare" function

如果有问题的语言实现了运算符重载,那么我将实现==运算符。如果没有,那么你可以使用上面的equals或实现“比较”功能

Your lanuage or framework probably already implements both of these for strings. You have to keep in mind that the "Equals" function in most languages actully compares references (shallow) verses instancs (deep).

你的语言或框架可能已经为字符串实现了这两个。你必须记住,大多数语言中的“等于”功能实际上比较了参考(浅)经文(深)。

The compare function looks like this:

比较函数如下所示:

int Compare(string a, string b)
{
  if(a < b) return -1;
  else if (a == b) return 0;
  else return 1;
}

#5


In short, context (the name of the variable speaks for what it holds) & readability is what I think matters.

简而言之,上下文(变量的名称代表它所拥有的内容)和可读性是我认为重要的。


foreach(Order order in Orders)
{
    if (order.ID == queriedOrderID)....
}

#1


Well, if you need 2 variable names, let's call them a and b, then you probably have 3 objects involved: self, a, b.

好吧,如果你需要2个变量名,让我们称它们为a和b,那么你可能有3个对象:self,a,b。

Now, that's quite a bunch for me, I try to keep things smaller.

现在,这对我来说相当多,我试着让事情变得更小。

So, consider your first example:

所以,考虑你的第一个例子:

for (String a : TreeMapvar) {  
    // Read b from data source  
    if (a.equals(b)) {  
        // Update a  
    }  else {
        TreeMapvar.add(b);
}

So, why not have your whole snippet be part of the b class definition and rename b to "this"?

那么,为什么不让你的整个片段成为b类定义的一部分并将b重命名为“this”?

lookAtYourTreeMap() {
    for( String a : TreeMapvar) {
      this.accomodateTreemapIfNecessaryTo(a);
    }
}

And then I would have a method

然后我会有一个方法

accomodateTreemapIfNecessaryTo(a) {
  if(a .equals( this)) {
     a.update();
  } else {
    TreeMapVar.add(a);
  }
}

Notice that the inventor of the modern computer, Kent Beck, says that, methods need to be short.

请注意,现代计算机的发明者Kent Beck说,方法需要简短。

Start the whole thing in your third component like this:

像这样在你的第三个组件中启动整个事情:

B b = loadBFromSource();
b.lookAtYourTreemap();

Loops like yours I would call "structural code".

像你这样的循环我称之为“结构代码”。

#2


I think variable names are not at all irrelevant, but also that there's no good generic name that would fit all possible cases in such a loop. It depends on what is stored in the TreeMap and what does the data source contain. Nevertheless, we could say something like

我认为变量名称根本不相关,但是在这样的循环中没有适合所有可能情况的通用名称。它取决于TreeMap中存储的内容以及数据源包含的内容。不过,我们可以这样说

for (String localData : TreeMapvar) {  
    // Read remoteData from data source  
    if (localData.equals(remoteData)) {  
        // Update or add to a counter  
    }  
}

if I got your drift.

如果我得到你的漂移。

The thing is that the comparison is made evident by the use of .equals() so we shouldn't need to use variable names (in general) to say that the values are going to be compared, what's not evident (and thus more important) is what values are being compared.

问题在于,使用.equals()可以明显地进行比较,因此我们不需要使用变量名(一般来说)来表示将要比较值,哪些不明显(因此更重要) )是被比较的价值。

EDIT: Your second example changes nothing. You are still using lousy varnames a and b where you should be using descriptive ones showing what a and b actually represent. I still see the comparison clear, so that's not relevant to naming.

编辑:你的第二个例子没有改变。你还在使用糟糕的varnames a和b,在那里你应该使用描述a和b实际表示的描述。我仍然看到比较清楚,因此与命名无关。

EDIT (re, your comment): That's easy. The only needed thing is some context, let's say the Map contains usernames associated to whatever you like and you are comparing them against existing user information.

编辑(重新,你的评论):这很容易。唯一需要的是一些上下文,假设Map包含与您喜欢的任何内容相关联的用户名,并且您将它们与现有用户信息进行比较。

for (String a : TreeMapvar) {  
    // Read b from data source  
    if (a.equals(b)) {  
        // Update or add to a counter  
    }  
}

for (String userName : TreeMapvar) {  
    String existingUserName = getNextUserName();
    if (userName.equals(existingUserName)) {  
        // Update or add to a counter  
    }  
}

#3


Variable names should enhance the code readability, in your example (without trying to modify the code):

在您的示例中,变量名称应该增强代码可读性(不尝试修改代码):

  1. a could be named something like baseValue
  2. a可以命名为baseValue

  3. b could be named something like compareToValue or testValue
  4. b可以命名为compareToValue或testValue

so if (a.equals(b)) {
becomes if (baseValue.equals(compareToValue)) {

所以if(a.equals(b)){成为if(baseValue.equals(compareToValue)){

#4


If the language in question implements operator overloading, then I would implements the == operator. If it doesn't, then you can either use equals as above or implement "Compare" function

如果有问题的语言实现了运算符重载,那么我将实现==运算符。如果没有,那么你可以使用上面的equals或实现“比较”功能

Your lanuage or framework probably already implements both of these for strings. You have to keep in mind that the "Equals" function in most languages actully compares references (shallow) verses instancs (deep).

你的语言或框架可能已经为字符串实现了这两个。你必须记住,大多数语言中的“等于”功能实际上比较了参考(浅)经文(深)。

The compare function looks like this:

比较函数如下所示:

int Compare(string a, string b)
{
  if(a < b) return -1;
  else if (a == b) return 0;
  else return 1;
}

#5


In short, context (the name of the variable speaks for what it holds) & readability is what I think matters.

简而言之,上下文(变量的名称代表它所拥有的内容)和可读性是我认为重要的。


foreach(Order order in Orders)
{
    if (order.ID == queriedOrderID)....
}