8
\$\begingroup\$

I am having a problem describing what the following code does. Below is my attempt at its javadoc.

/**
 * Convert an integer so that the notion of its size fits within a certain number digits.
 * e.g.
 * Fit 123 to 2 digits -> 99+
 * @param in The integer
 * @param order The order of magnitude to fit it in.
 * @return The 'countified' String.
 */
public static String countify(int in, int order) {
    final int max = (int) Math.pow(10, order);

    if (in >= max) {
        return String.valueOf(max - 1) + "+";
    } else  {
        return String.valueOf(in);
    }
}

The idea is simple enough. For any value greater than 10^order it returns 10^order - 1 plus a + (e.g. 123, with order 2 becomes 99+), else it returns the number itself. How do I better describe it? Thanks!

\$\endgroup\$
  • \$\begingroup\$ What you need is a good name for your method right? Because I think the code inside is perfect as is. \$\endgroup\$ – IEatBagels Sep 22 '15 at 19:49
  • 1
    \$\begingroup\$ \@TopinFrassi That's what i thought when I posted the question, but clearly I was mistaken :) See @rolfl's answer. \$\endgroup\$ – iceman Sep 23 '15 at 4:55
7
\$\begingroup\$

There are three things I see as being significant issues with your code, and also a suggestion about your method's name.

Orphaned Else

When you have an if-statement with a guaranteed return in it, there's no need for an else-block. Your code would be better as:

if (in >= max) {
    return String.valueOf(max - 1) + "+";
}
return String.valueOf(in);

Input validation

  • bad orders: your code will produce unexpected results for values with an order outside the range of 1..10. An order of -1 implies a "max" of 0.1, which will become 0, which will mean values like 10 will be presented as -1+ .... which is hard to fathom.
  • bad values: Negative input values will cause consternation. An order of 2 implies 2-digits of value, but the input -12345 will output as -12345. Your code does not have a good way of expressing negative input values, so I don't know what to recommend other than avoiding them entirely, and throwing an IllegalArgumentException for negative input.

Edge Cases

Orders larger than 10 will effectively truncate to Integer.MAX_VALUE, which makes Integer.MAX_VALUE an interesting input..... An order of 10 and an input of Integer.MAX_VALUE, would normally imply an output of 2147483647, but you respond with 2147483646+

Name

The term "clamp" is often used when confronted with this.... where a value is clamped to be within a range, or limit. I would use that as the function name.

Solution

I would recommend transfroming your internal logic to use long, and be done with it... ;-)

public static String clamp(int in, int order) {
    if (order < 0 || in < 0) {
        throw new IllegalArgumentException("Inputs are required to be positive");
    }
    long max = (long)Math.pow(10, order);
    if (max > Integer.MAX_VALUE || in < max) {
        return String.valueOf(in);
    }
    return String.valueOf(max - 1) + "+";
}
\$\endgroup\$
7
\$\begingroup\$

Use less powerful language constructs

You use if and else

if (in >= max) {
    return String.valueOf(max - 1) + "+";
} else  {
    return String.valueOf(in);
}

Using if and else duplicates return String.valueOf

Here the ternary operator comes to the rescue. It looks hard but really is very easy to use.

condition ? a : b

If condition is true, than a, else b. As easy as that.

You can now write return String.valueOf just one time:

return String.valueOf(in >= max ? (max - 1) + "+" : in)
\$\endgroup\$
  • 1
    \$\begingroup\$ Appending the "+" already results in int to string conversion, so you could (should?) maybe write return in >= max ? (max - 1) + "+" : String.valueOf(in); instead. \$\endgroup\$ – Vincent van der Weele Sep 23 '15 at 5:05
1
\$\begingroup\$

Convert Integer to String if it is exceeding number of digits mentioned in order "it is better to call it width or digits" it will return max +ve number fit in digits mentioned in order concatenating "+" to it.

give more ex. like

  • (100, 2) -> "99+"
  • (355, 4) -> "355"
  • (99, 2) -> "99"

can use method name "toStringWithLimit"

\$\endgroup\$
1
\$\begingroup\$

I like @Caridorc's suggestion to use the ternary here.

As for a good name, I think intToStringWithMaxDigits(int n, int maxDigits) is clear, if verbose.

\$\endgroup\$
0
\$\begingroup\$

Math.log10

The answer from rolfl already covers input validation. I'd like to add that instead of using Math.pow with the expected number of digits, you can use Math.log10 on the argument.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.