3

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.

tkw83
  • 185
  • 1
  • 5
SklogW
  • 839
  • 9
  • 22
  • 1
    This question is really subjective. – Koray Tugay Jan 22 '15 at 14:57
  • 1
    Yes it's a good idea. You could even put it in a method. It may make sense to find a shorter name though, maybe `nodesHaveSameInitial(node1, node2)`? – assylias Jan 22 '15 at 14:57
  • You could just add a comment. Commenting your code is just as important as having good variable names. – DaaaahWhoosh Jan 22 '15 at 14:58
  • 5
    No! Try to avoid comments and focus on better naming of classes, methods, parameters and variables. Documentation is always lying, as your code envolves but not your documentation – Steffen Jan 22 '15 at 15:00
  • thank you for your comments, @Koray Tugay isn't 'readability' in general subjective ? I aksed the question because the tag is available. – SklogW Jan 22 '15 at 15:00
  • 1
    Example 2 could result in an endless loop if you don't reassign `letterFromBothNodesAreEqual_andNameHasMoreThanOneLetter` within the loop. This is an example of how introducing variables for readability creates unexpected bugs. NESPowerGlove's answer shows a better approach. – TheBlastOne Jan 22 '15 at 15:00
  • More generally, if you break up your code into smaller atoms with explicitly assignments, it can make it easier to debug and to determine root cause when reading reports with stacktraces, etc. It is a balance of readability, maintenance, and conciseness. (Well, and correctness, but that is a different domain, that can be affected by readability, etc.) –  Jan 22 '15 at 15:02

4 Answers4

7

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).

NESPowerGlove
  • 5,496
  • 17
  • 28
  • So splitting it up into two methods would have the additional benefit of a more readable stack-trace too, am i correct ? – SklogW Jan 22 '15 at 15:05
  • @SklogW No problem. If you are looking for more info on clean code practices, I would recommend the book Clean Code. It's basically the book on the subject and the examples are all in Java. – NESPowerGlove Jan 22 '15 at 15:05
  • @SklogW I suppose it would, but I wouldn't go that deep into making decisions about readable code. There's often a quote that goes around saying that code is read 10 times more often then it's written or modified. Clean code is mostly about making the majority time spent coding (reading) easier. It is a fair point though about stack traces, sometimes when people write unit tests they write detailed names and identifiers so they can see the actual requirement that failed when a test fails in the stack trace / results. – NESPowerGlove Jan 22 '15 at 15:14
3

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.

Community
  • 1
  • 1
biziclop
  • 48,926
  • 12
  • 77
  • 104
2

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");
}
gotomanners
  • 7,808
  • 1
  • 24
  • 39
1

Yes offcourse its a good practice to name variables according to there work or functionality in the program.Because in case in the future if someone else works on your code then it will be easier for him to understand otherwise it will give him a headache same happens while working on distributed program your co-workers must understand variables work by there name.

kirti
  • 4,499
  • 4
  • 31
  • 60
  • This is what we say everytime, for someone who will use our code in future,But most of the time when i came across someone's code,I noticed that they have not taken care of the same for me(In particular).. :p – nobalG Jan 22 '15 at 16:33
  • Thats y we shouldnot do it @nobalG – kirti Jan 22 '15 at 17:16