views:

88

answers:

3

I try to simplify conditionals in:

for ( int t=0, size=fo.getPrintViewsPerFile().size();
          t<size && t<countPerFile;
          t++)
{
    // ...
}

, more precisely:

t<s && t<c

You need to compare two times, then calc the boolean value from them. Is there any simpler way to do it? If no, how can you prove it? I can simplify it to some extent, proof tree.

[Added]

I tried to solve the problem directly through logic. It would interesting to see the implication in choosing the minima. Link:

http://www.umsu.de/logik/trees/?f=(\exists%20s%20\exists%20c%20\forall%20t%20%20(Pts%20\land%20Ptc))\leftrightarrow\neg(\foralls\forallc\existst(\neg(Pts)\lor\neg(Ptc)))

+1  A: 

You can do t < Math.min(s, c), but that wouldn't actually reduce the number of comparison.

I do think that appropriate use of Math.min and Math.max makes for a much more readable code, though. Unfortunately they only have overloads for 2 args (for int, long, float and double arguments). It would've been really nice if they also have 3 args and varargs overloads.

You can always write utilities method for these kinds of things (interval checking is a common idiom (minV <= v) && (v <= maxV) etc), but linguistically, no Java doesn't have any fancy operators that would do these things. They only have basic numerical comparison operators (JLS 15.20.1) and basic boolean operators (JLS 15.22.2, 15.23, 15.24).


Sidenote

Higher-level languages like Icon does allow these kinds of constructs:

Java                      Icon
(x < v1) || (x < v2)      x < (v1 | v2)
(a < b) && (b < c)        a < b < c
polygenelubricants
varargs overloads?
HH
http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html -- basically you can give it any number of arguments, and you can also find the min/max of an array by just passing the entire array. Would've been quite useful.
polygenelubricants
+3  A: 

If size and countPerFile are constant for the duration of the loop, you can precalculate their minimum before the loop, and then the test would be t<minimum.

int size=fo.getPrintViewsPerFile().size();
int minLimit = Math.min(size, countPerFile);
for (int t=0; t<minLimit; t++) {
    ....
interjay
+1  A: 

Forget about it. If you're dealing with printing or files this kind of micro-optimizaton would save you practically nothing.

EJP