You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Felix Knecht <fe...@otego.com> on 2010/06/04 11:28:30 UTC

How strict must we check in util methods

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Do we leave it up to third party developers to take care that only
correct values are passed or do we have to take care that otherwise an
exception is thrown? To give an example from shared AttributUtils:
We never check if the object to clone is a String, we just presume that
any other developer using this class respects the convention given by
javadoc. Can we do so or should an exception be thrown in such cases?

This question will appear for sure for other methods as well.

   /**
     * Clone the value. An attribute value is supposed to be either a String
     * or a byte array. If it's a String, then we just return it ( as String
     * is immutable, we don't need to copy it). If it's a bu=yte array, we
     * create a new byte array and copy the bytes into it.
     *
     * @param value The value to clone
     * @return The cloned value
     */
    public static Object cloneValue( Object value )
    {
        // First copy the value
        Object newValue = null;

        if ( value instanceof byte[] )
        {
            newValue = ( ( byte[] ) value ).clone();
        }
        else
        {
            newValue = value;
        }

        return newValue;
    }
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwIxz4ACgkQ2lZVCB08qHHdxgCfdoxVZnY6OLr33zaD+p0+Dz6T
LU8AnisthtKmNWYd3faao3mBgsrF4IK9
=2x2R
-----END PGP SIGNATURE-----

Re: How strict must we check in util methods

Posted by Felix Knecht <fe...@apache.org>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 06/04/10 11:36, Kiran Ayyagari wrote:
> On Fri, Jun 4, 2010 at 12:28 PM, Felix Knecht <fe...@otego.com> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Do we leave it up to third party developers to take care that only
>> correct values are passed or do we have to take care that otherwise an
>> exception is thrown? To give an example from shared AttributUtils:
>> We never check if the object to clone is a String, we just presume that
>> any other developer using this class respects the convention given by
>> javadoc. Can we do so or should an exception be thrown in such cases?
> 
> think it is ok to assume the other value as string, IMHO a throws
> clause makes the caller
> code cluttered with try/catch blocks (assuming that we are throwing a
> checked exception)
> 
> P.S:- hmm not sure if throwing a IllegalArgumentException is the right
> idea here, but is a good choice

Hehe ... Sounds like the answer of a politician :-)

> 
> thanks Felix
> 
> Kiran Ayyagari
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwIyzcACgkQ2lZVCB08qHG6dwCfQLkHMgKNTWMpGN/hhtcJFs63
i3kAn0LzOKCH+aWo60OQWLbvLUX6SbdL
=nWpg
-----END PGP SIGNATURE-----

Re: How strict must we check in util methods

Posted by Kiran Ayyagari <ka...@apache.org>.
On Fri, Jun 4, 2010 at 12:28 PM, Felix Knecht <fe...@otego.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Do we leave it up to third party developers to take care that only
> correct values are passed or do we have to take care that otherwise an
> exception is thrown? To give an example from shared AttributUtils:
> We never check if the object to clone is a String, we just presume that
> any other developer using this class respects the convention given by
> javadoc. Can we do so or should an exception be thrown in such cases?

think it is ok to assume the other value as string, IMHO a throws
clause makes the caller
code cluttered with try/catch blocks (assuming that we are throwing a
checked exception)

P.S:- hmm not sure if throwing a IllegalArgumentException is the right
idea here, but is a good choice

thanks Felix

Kiran Ayyagari

Re: How strict must we check in util methods

Posted by Emmanuel Lecharny <el...@gmail.com>.
On 6/4/10 11:28 AM, Felix Knecht wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Do we leave it up to third party developers to take care that only
> correct values are passed or do we have to take care that otherwise an
> exception is thrown? To give an example from shared AttributUtils:
> We never check if the object to clone is a String, we just presume that
> any other developer using this class respects the convention given by
> javadoc. Can we do so or should an exception be thrown in such cases?
>
> This question will appear for sure for other methods as well.
>
>     /**
>       * Clone the value. An attribute value is supposed to be either a String
>       * or a byte array. If it's a String, then we just return it ( as String
>       * is immutable, we don't need to copy it). If it's a bu=yte array, we
>       * create a new byte array and copy the bytes into it.
>       *
>       * @param value The value to clone
>       * @return The cloned value
>       */
>      public static Object cloneValue( Object value )
>      {
>          // First copy the value
>          Object newValue = null;
>
>          if ( value instanceof byte[] )
>          {
>              newValue = ( ( byte[] ) value ).clone();
>          }
>          else
>          {
>              newValue = value;
>          }
>
>          return newValue;
>      }
>    
Hi Felix,

I think I replied by sending another mail (I wrote it 2 hours ago while 
I ddn't have internet connection ...)

I think that this is exacty what 'assert' is good for :

     public static Object cloneValue( Object value )
     {
         assert ( value != null );
         assert ( ( value instanceof String ) || ( value instanceof byte[] ) )

         // First copy the value
         Object newValue = null;

           ...

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.nextury.com