Skip to content

Fix duplicate toUpperCase() calls, use Locale.ROOT #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
8 changes: 5 additions & 3 deletions java/com/google/openlocationcode/OpenLocationCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.openlocationcode;

import java.math.BigDecimal;
import java.util.Locale;

/**
* Convert locations to and from convenient short codes.
Expand Down Expand Up @@ -153,11 +154,12 @@ public double getEastLongitude() {
* @constructor
*/
public OpenLocationCode(String code) throws IllegalArgumentException {
if (!isValidCode(code.toUpperCase())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can get rid of the code.toUpperCase() here all together, since we're calling it inside of isValidCode().

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is public, can we make that assumption and document it the constructor comments?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your question here, can you rephrase? What assumption are you thinking we need to document?

String newCode = code.toUpperCase(Locale.ROOT);
if (!isValidCode(newCode)) {
throw new IllegalArgumentException(
"The provided code '" + code + "' is not a valid Open Location Code.");
}
this.code = code.toUpperCase();
this.code = newCode;
}

/**
Expand Down Expand Up @@ -496,7 +498,7 @@ public static boolean isValidCode(String code) {
if (code == null || code.length() < 2) {
return false;
}
code = code.toUpperCase();
code = code.toUpperCase(Locale.ROOT);
Copy link
Contributor

@JonMcPherson JonMcPherson May 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to remove all redundant calls of toUpperCase(), it could be modified to something like this

public static boolean isValidCode(String code) {
    return isValidCodeUpperCase(code.toUpperCase(Locale.ROOT));
}

private static boolean isValidCodeUpperCase(String code) {
    // do validity checks without calling code.toUpperCase() redundantly
}

And the constructor would instead call !isValidCodeUpperCase(newCode).
Just a minor improvement...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out the redundant call. It's so redundant, in fact, that I think we can just remove the one in the constructor since the code will be cast to uppercase in isValidCode().


// There must be exactly one separator.
int separatorPosition = code.indexOf(SEPARATOR);
Expand Down