简体   繁体   中英

Introducing variables only for readability?

Is it a good idea to introduce variables only for the sake of readability ?

Example 1:

while(nameNode1.charAt(0) == nameNode2.charAt(0) && nameNode1.length() > 1 && nameNode2.length() > 1)
{
    nameNode1 = nameNode1.substring(1, nameNode1.length());
    nameNode2 = nameNode2.substring(1, nameNode2.length());
}

Example 2:

boolean letterFromBothNodesAreEqual_andNameHasMoreThanOneLetter = nameNode1.charAt(0) == nameNode2.charAt(0) && nameNode1.length() > 1 && nameNode2.length() > 1;

while(letterFromBothNodesAreEqual_andNameHasMoreThanOneLetter)
{
    nameNode1 = nameNode1.substring(1, nameNode1.length());
    nameNode2 = nameNode2.substring(1, nameNode2.length());
}

It might be an extreme example, but i think you get the idea.

I haven't seen this in code and i was wondering if this is a useful approach?

Thank You

Context: I'm trying to make the transition from college to Entry-Level-Developer and currently I'm focusing on clean-coding.

It's always better to make code readable, just don't over do it too much where it's a maintenance nightmare, although most clean code is easier to maintain.

I would introduce two new methods here instead of variables, where your code example would be:

while(letterFromBothNodesAreEqual() && nameHasMoreThanOneLetter())
{
    nameNode1 = nameNode1.substring(1, nameNode1.length());
    nameNode2 = nameNode2.substring(1, nameNode2.length());
}

It's a common readability refactor to extract boolean conditions into their own function with a name. If a person is reading your code and comes across some if with a bunch of conditions, they will wonder what the significance of it is, why is the branch needed or what it represents. The fields alone might not tell them the answer, but they might know what it represents if the condition had a name (the name of the function).

Apart from the fact that the variable name in your example is a bit too verbose, yes.

One thing that is important though is to remember to keep the scope of local variables as small as possible. So don't declare local variables at the start of the method if they're only going to be used in an if block further down.

Edit : And one more thing I've just noticed: your two examples are NOT equivalent. In the first scenario the expression is recalculated on every iteration, in the second one it isn't. In cases like this you need a helper method as explained by @NESPowerGlove instead of a variable.

是的,在程序中根据程序中的工作或功能命名变量是一个很好的做法。因为将来如果其他人在你的代码上工作,那么他会更容易理解,否则会让他头疼同样的事情发生在分布式程序上工作时,您的同事必须了解变量的名称。

This question is quite subjective but the following holds true for all subjects. (I wanted to have a little fun with this answer)

private boolean isMoreReadable = true;
private boolean isEasyToMaintain = true;
private boolean isProperlyCommented = true;
private boolean isBugFree = true;

// This method checks if my co-workers are happy with my code
private boolean myCoWorkersHappyWithMyCode() {
    return isMoreReadable && isEasyToMaintain && isProperlyCommented && isBugFree;
}

if (myCoWorkersHappyWithMyCode()) {
    System.out.println("YES, you wrote good code so I don't see why not");
} else {
    System.out.println("NO, keep learning to better yourself");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM