You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gilles Sadowski <gi...@harfang.homelinux.org> on 2012/05/28 12:12:56 UTC

Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Hi Sébastien.

> [...]
> Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java
> URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java?rev=1343163&r1=1343162&r2=1343163&view=diff
> ==============================================================================
> --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java (original)
> +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java Mon May 28 08:31:13 2012
> @@ -22,6 +22,7 @@ import java.util.NoSuchElementException;
>  
>  import org.apache.commons.math3.exception.MathUnsupportedOperationException;
>  import org.apache.commons.math3.exception.DimensionMismatchException;
> +import org.apache.commons.math3.exception.NumberIsTooSmallException;
>  import org.apache.commons.math3.exception.OutOfRangeException;
>  import org.apache.commons.math3.exception.MathArithmeticException;
>  import org.apache.commons.math3.analysis.FunctionUtils;
> @@ -191,6 +192,37 @@ public abstract class RealVector {
>      }
>  
>      /**
> +     * Checks that the indices of a subvector are valid.
> +     *
> +     * @param start the index of the first entry of the subvector
> +     * @param end the index of the last entry of the subvector (inclusive)
> +     * @throws OutOfRangeException if {@code start} of {@code end} are not valid
> +     * @throws NumberIsTooSmallException if {@code end < start}
> +     */
> +    protected void checkIndices(final int start, final int end) {
> +        final int dim = getDimension();
> +        if ((start < 0) || (start >= dim)) {
> +            throw new OutOfRangeException(LocalizedFormats.INDEX,
> +                                          Integer.valueOf(start),
                                             ^^^^^^^^^^^^^^^

Why do you call "valueOf"?

> +                                          Integer.valueOf(0),
> +                                          Integer.valueOf(dim - 1));
> +        }
> +        if ((end < 0) || (end >= dim)) {
> +            throw new OutOfRangeException(LocalizedFormats.INDEX,
> +                                          Integer.valueOf(end),
> +                                          Integer.valueOf(0),
> +                                          Integer.valueOf(dim - 1));
> +        }
> +        if (end < start){
> +            // TODO Use more specific error message
> +            throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW,
> +                                                Integer.valueOf(end),
> +                                                Integer.valueOf(start),
> +                                                false);
> +        }
> +    }

Best,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.

> >> [...]
> >> +    protected void checkIndices(final int start, final int end) {
> >> +        final int dim = getDimension();
> >> +        if ((start < 0) || (start >= dim)) {
> >> +            throw new OutOfRangeException(LocalizedFormats.INDEX,
> >> +                                          Integer.valueOf(start),
> >                                             ^^^^^^^^^^^^^^^
> >
> > Why do you call "valueOf"?
> >
> 
> I am not Sébastien, but this should be obvious:
> 
> The constructor used here is
> 
> public OutOfRangeException(Localizable specific,
> Number wrong,
> Number lo,
> Number hi)
> 
> Integer.valueOf() is the best way to convert  int -> Integer

No, the best way is to let the compiler do the conversion, in order to have
a cleaner source code.


Regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by Konstantin Kolinko <kn...@gmail.com>.
2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
> Hi Sébastien.
>
>> [...]
>> Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java
>> URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java?rev=1343163&r1=1343162&r2=1343163&view=diff
>> ==============================================================================
>> --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java (original)
>> +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java Mon May 28 08:31:13 2012
>> @@ -22,6 +22,7 @@ import java.util.NoSuchElementException;
>>
>>  import org.apache.commons.math3.exception.MathUnsupportedOperationException;
>>  import org.apache.commons.math3.exception.DimensionMismatchException;
>> +import org.apache.commons.math3.exception.NumberIsTooSmallException;
>>  import org.apache.commons.math3.exception.OutOfRangeException;
>>  import org.apache.commons.math3.exception.MathArithmeticException;
>>  import org.apache.commons.math3.analysis.FunctionUtils;
>> @@ -191,6 +192,37 @@ public abstract class RealVector {
>>      }
>>
>>      /**
>> +     * Checks that the indices of a subvector are valid.
>> +     *
>> +     * @param start the index of the first entry of the subvector
>> +     * @param end the index of the last entry of the subvector (inclusive)
>> +     * @throws OutOfRangeException if {@code start} of {@code end} are not valid
>> +     * @throws NumberIsTooSmallException if {@code end < start}
>> +     */
>> +    protected void checkIndices(final int start, final int end) {
>> +        final int dim = getDimension();
>> +        if ((start < 0) || (start >= dim)) {
>> +            throw new OutOfRangeException(LocalizedFormats.INDEX,
>> +                                          Integer.valueOf(start),
>                                             ^^^^^^^^^^^^^^^
>
> Why do you call "valueOf"?
>

I am not Sébastien, but this should be obvious:

The constructor used here is

public OutOfRangeException(Localizable specific,
Number wrong,
Number lo,
Number hi)

Integer.valueOf() is the best way to convert  int -> Integer


Best regards,
Konstantin  Kolinko
>> +                                          Integer.valueOf(0),
>> +                                          Integer.valueOf(dim - 1));
>> +        }
>> +        if ((end < 0) || (end >= dim)) {
>> +            throw new OutOfRangeException(LocalizedFormats.INDEX,
>> +                                          Integer.valueOf(end),
>> +                                          Integer.valueOf(0),
>> +                                          Integer.valueOf(dim - 1));
>> +        }
>> +        if (end < start){
>> +            // TODO Use more specific error message
>> +            throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW,
>> +                                                Integer.valueOf(end),
>> +                                                Integer.valueOf(start),
>> +                                                false);
>> +        }
>> +    }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by Sébastien Brisard <se...@m4x.org>.
Hello again,

2012/5/28 sebb <se...@gmail.com>:
> On 28 May 2012 13:28, Sébastien Brisard <se...@m4x.org> wrote:
>> Hi Gilles,
>>
>> 2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
>>> Hello.
>>>
>>>>
>>>> >
>>>> > Why do you call "valueOf"?
>>>> >
>>>> I have to say I do not like implicit conversions, that's why I tend to
>>>> always use Integer.valueOf and the likes.
>>>
>>> Why?
>>>
>> This is going to get "philosophical": I would not dare to claim that
>> I'm holding *the* truth, this is only my way of seeing things. I do
>> not like things to happen implicitely, because I think that it opens a
>> door to errors (this would not be true of professional programmers,
>> but remember that I work in an environment where people are not real
>> computer scientists... so my philosophy is "close as many doors as you
>> can"...). I even think that J. Bloch has a nice example of potential
>> issues with auto-boxing. However, I agree with you: calling valueOf in
>> this context (that is: building a new exception) is certainly
>> far-fetched.
>>
>> That was the initial reason why I moved from C++ to Java ten years
>> ago: too many things happened "behind my back". Again, I'm not saying
>> that C++ is evil. I'm just saying that I did not have the background
>> with C++ to be fully aware of these implicit assumptions, and their
>> potential consequences.
>>
>> I'm pretty sure I will not convince you on this. I hope I have at
>> least convinced you that I have a good reason to do it this way :-)
>>
>
> I agree with you - making boxing explicit is better.
>
> The reasoning is that implicit boxing/unboxing can hide ineffiicient
> conversions as well as subtle bugs.
> I've seen code which treats the same item variously as a Integer and
> an int for no good reason.
> The compiler warnings allow the careful programmer to investigate and
> choose one or the other.
>
> Yes, the code is a bit longer, but it clearly shows that the
> boxing/unboxing was intended, and not an accident of compiler
> "helpfulness".
>
>>>> However, if you think that
>>>> it leads to less readable code, I'm OK with your way of seeing things.
>>>
>>> Good! ;-)
>>>
>>> Gilles
>>>
>> As I said, I will remove these explicit boxings (but be warned: I've
>> used valueOf() in the past, and do apologize for any unwanted
>> occurence...).
>>
>> Best regards,
>> Sébastien
>>
Wowww!
Who would have thought that this would raise so many answers!
While I also think explicit conversion is better, I think we can all
agree that Gilles has a point for this specific case

            throw new OutOfRangeException(LocalizedFormats.INDEX,
                                          Integer.valueOf(start),
                                          Integer.valueOf(0),
                                          Integer.valueOf(dim - 1));

is a bit far-fetched, since the code ends right there. I think we can
safely follow Gilles' advice, here.
Best regards,
Sébastien


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by sebb <se...@gmail.com>.
On 28 May 2012 13:28, Sébastien Brisard <se...@m4x.org> wrote:
> Hi Gilles,
>
> 2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
>> Hello.
>>
>>>
>>> >
>>> > Why do you call "valueOf"?
>>> >
>>> I have to say I do not like implicit conversions, that's why I tend to
>>> always use Integer.valueOf and the likes.
>>
>> Why?
>>
> This is going to get "philosophical": I would not dare to claim that
> I'm holding *the* truth, this is only my way of seeing things. I do
> not like things to happen implicitely, because I think that it opens a
> door to errors (this would not be true of professional programmers,
> but remember that I work in an environment where people are not real
> computer scientists... so my philosophy is "close as many doors as you
> can"...). I even think that J. Bloch has a nice example of potential
> issues with auto-boxing. However, I agree with you: calling valueOf in
> this context (that is: building a new exception) is certainly
> far-fetched.
>
> That was the initial reason why I moved from C++ to Java ten years
> ago: too many things happened "behind my back". Again, I'm not saying
> that C++ is evil. I'm just saying that I did not have the background
> with C++ to be fully aware of these implicit assumptions, and their
> potential consequences.
>
> I'm pretty sure I will not convince you on this. I hope I have at
> least convinced you that I have a good reason to do it this way :-)
>

I agree with you - making boxing explicit is better.

The reasoning is that implicit boxing/unboxing can hide ineffiicient
conversions as well as subtle bugs.
I've seen code which treats the same item variously as a Integer and
an int for no good reason.
The compiler warnings allow the careful programmer to investigate and
choose one or the other.

Yes, the code is a bit longer, but it clearly shows that the
boxing/unboxing was intended, and not an accident of compiler
"helpfulness".

>>> However, if you think that
>>> it leads to less readable code, I'm OK with your way of seeing things.
>>
>> Good! ;-)
>>
>> Gilles
>>
> As I said, I will remove these explicit boxings (but be warned: I've
> used valueOf() in the past, and do apologize for any unwanted
> occurence...).
>
> Best regards,
> Sébastien
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.

> 
> That was the initial reason why I moved from C++ to Java ten years
> ago: too many things happened "behind my back".

Arguably a lot more happens so with Java!

Gilles

> [...]

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by Thomas Neidhart <th...@gmail.com>.
On 05/28/2012 02:28 PM, Sébastien Brisard wrote:
> Hi Gilles,
> 
> 2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
>> Hello.
>>
>>>
>>>>
>>>> Why do you call "valueOf"?
>>>>
>>> I have to say I do not like implicit conversions, that's why I tend to
>>> always use Integer.valueOf and the likes.
>>
>> Why?
>>
> This is going to get "philosophical": I would not dare to claim that
> I'm holding *the* truth, this is only my way of seeing things. I do
> not like things to happen implicitely, because I think that it opens a
> door to errors (this would not be true of professional programmers,
> but remember that I work in an environment where people are not real
> computer scientists... so my philosophy is "close as many doors as you
> can"...). I even think that J. Bloch has a nice example of potential
> issues with auto-boxing. However, I agree with you: calling valueOf in
> this context (that is: building a new exception) is certainly
> far-fetched.

Do not mind, I can't tell you what I have already seen at my workplace
from *professional* programmers ;-)

btw, a nice example how dangerous auto-boxing is:

Map<Short, Object> map = new HashMap<Short, Object>();
// fill the map with objects, keys in the range 1..10
...

// get the object with key 3
Object o = map.get(3);

o == null or not?

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by Sébastien Brisard <se...@m4x.org>.
Hi Gilles,

2012/5/28 Gilles Sadowski <gi...@harfang.homelinux.org>:
> Hello.
>
>>
>> >
>> > Why do you call "valueOf"?
>> >
>> I have to say I do not like implicit conversions, that's why I tend to
>> always use Integer.valueOf and the likes.
>
> Why?
>
This is going to get "philosophical": I would not dare to claim that
I'm holding *the* truth, this is only my way of seeing things. I do
not like things to happen implicitely, because I think that it opens a
door to errors (this would not be true of professional programmers,
but remember that I work in an environment where people are not real
computer scientists... so my philosophy is "close as many doors as you
can"...). I even think that J. Bloch has a nice example of potential
issues with auto-boxing. However, I agree with you: calling valueOf in
this context (that is: building a new exception) is certainly
far-fetched.

That was the initial reason why I moved from C++ to Java ten years
ago: too many things happened "behind my back". Again, I'm not saying
that C++ is evil. I'm just saying that I did not have the background
with C++ to be fully aware of these implicit assumptions, and their
potential consequences.

I'm pretty sure I will not convince you on this. I hope I have at
least convinced you that I have a good reason to do it this way :-)

>> However, if you think that
>> it leads to less readable code, I'm OK with your way of seeing things.
>
> Good! ;-)
>
> Gilles
>
As I said, I will remove these explicit boxings (but be warned: I've
used valueOf() in the past, and do apologize for any unwanted
occurence...).

Best regards,
Sébastien


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hello.

> 
> >
> > Why do you call "valueOf"?
> >
> I have to say I do not like implicit conversions, that's why I tend to
> always use Integer.valueOf and the likes.

Why?

> However, if you think that
> it leads to less readable code, I'm OK with your way of seeing things.

Good! ;-)

Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1343163 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/linear/ test/java/org/apache/commons/math3/linear/

Posted by Sébastien Brisard <se...@m4x.org>.
Hi Gilles,

>
> Why do you call "valueOf"?
>
I have to say I do not like implicit conversions, that's why I tend to
always use Integer.valueOf and the likes. However, if you think that
it leads to less readable code, I'm OK with your way of seeing things.

Best regards,
Sébastien


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org