3

We had big discussion about small peace of code in our team.

Example 1 developer added extra local variable to make code more readable. In local variable he saved value from constant.

Example 2 same code without extra variable, but less readable from our developer point of view.

What do you think is more readable? Better from refactoring point of view?

1.

var tolerance = Constants.DateTypeGeneratorTolerance;

var dayType = DateTypeGenerator.GenerateDateType(
                courseTripValidity,
                tolerance,
                symbols,
                startDate,
                endDate);

2.

var dayType = DateTypeGenerator.GenerateDateType(
                courseTripValidity,
                Constants.DateTypeGeneratorTolerance,
                symbols,
                startDate,
                endDate);
3
  • 3
    This question doesn't have a single correct answer, so it may be closed as being too subjective (but for what it's worth, I prefer the version with a local variable). Commented Jun 22, 2012 at 12:16
  • I'd remove the vars...I'm joking. At least the original developer considered readability which is a plus in my view Commented Jun 22, 2012 at 12:38
  • Pretty unhealthy to have big arguments about something so inconsequential as this. You all go stand in a circle now, hold hands and sing Kumbaya. Commented Jun 22, 2012 at 13:12

1 Answer 1

1

There is no definitive answer to this, but I personally prefer the latter version. IMHO Constants.DateTypeGeneratorTolerance is not unbearably long (although not very far away from that either). And if its name is well chosen, it tells precisely what it means. Whereas introducing a local variable clutters the space and makes it more difficult for me to follow what's going on - not only when directly reading the code, but it is also more cumbersome to search for the usages of this constant.

Moreover, the name itself is long largely because it contains scoping information. By putting all global constants into a single class, you feel the need to differentiate between them using naming prefixes (like DateTypeGenerator). Whereas if you moved the constant to where it logically belongs to - either to DateTypeGenerator itself, or to a separate DateTypeGeneratorConstants class - the constant name itself would become short and concise.

Note that several languages offer special features to reduce the need for qualifiers, thus shortening names. Such as static imports in Java, or using directives in C#.

Combining the above with C# using directives you get:

using Tolerance = DateTypeGenerator.Tolerance;

...

DateType dayType = DateTypeGenerator.GenerateDateType(
                courseTripValidity,
                Tolerance,
                symbols,
                startDate,
                endDate);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.