You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Dan Allen <da...@mojavelinux.com> on 2003/05/22 22:54:32 UTC

where to put request constants

I have been studying a lot of different struts code lately but I
can't seem to come to a conclusion about one particular issue.  It
was discussed earlier on this list that constants should be used
wherever possible in place of strings.  This prevents typo bugs from
showing up.  However, I am not sure where to stick these strings.
The strings I am referring to are the attribute names for storing
data into the request object.  I considered making an AppRequest
class that would hold the strings, but I am not entirely sure.
Also, following the model that Ted set down, I was also using
constants whenever I did a findForward() call, but again, I am
confused where to put those constants.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
'One Microsoft Way' is more than just an address.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: where to put request constants

Posted by Dichotomy <di...@greenmail.ch>.
I think calling the Constant Interface pattern a "very bad idea" is a very big 
overstatement. It's a fugly work around, that should, can and will be 
improved in 1.5. In the meantime, it's a decent way to save typing and make 
code more readable (adding "MyConstantsClass." at the beginning of every 
constant does not improve readability). And it's not hard to get rid of the 
anti-pattern once static imports do become available, as it only requires 
removing the 'implements' clause and replacing it by a static import.

Daniel

On Thursday 22 May 2003 8:12 pm, Brandon Goodin wrote:
> FYI The Constant interface is considered an anti pattern
>
> Q: What will the new static import facility offer developers?
>
> A: It lets the programmer avoid prefixing static members with class names.
> People really want this, so much that they implement so-called constant
> interfaces to get the effect:
>
> // "Constant Interface" antipattern - do not use!
> public interface Physics {
>   public static final double AVOGADROS_NUMBER   = 6.02214199e23;
>   public static final double BOLTZMANN_CONSTANT = 1.3806503e-23;
>   public static final double ELECTRON_MASS      = 9.10938188e-31;
> }
>
> public class Guacamole implements Physics {
>     public static void main(String[] args) {
>         double moles = ...;
>         double molecules = AVOGADROS_NUMBER * moles;
>         ...
>     }
> }
>
> This is a very bad idea: interfaces are for defining types, not providing
> constants. The fact that the Guacamole class uses the Physics constants is
> just an implementation detail, but it "leaks" into the public API of the
> Guacamole class. Not only does this confuse the clients of this class, but
> it creates a long-term commitment. Even if you re-implement the Guacamole
> class so that it doesn't need these constants, you still have to implement
> the interfaces, as clients of the Guacamole class can now depend on the
> fact that it implements Physics.
>
> The static import facility offers a clean alternative. It lets the
> programmer avoid qualifying static member names without subtyping. It's
> analogous to the package import facility, except that it imports static
> members from a class, rather than classes from a package. Here's how it
> looks:
>
> import static org.iso.Physics.*;
>
> class Guacamole {
>     public static void main(String[] args) {
>         double molecules = AVOGADROS_NUMBER * moles;
>         ...
>     }
> }
>
> Note that this works whether Physics is an interface or a class. If it just
> defines constants, it should definitely be a class rather than an
> interface.
>
> http://java.sun.com/features/2003/05/bloch_qa.html
>
> Brandon Goodin
>
> -----Original Message-----
> From: Dichotomy [mailto:dichotomy@greenmail.ch]
> Sent: Thursday, May 22, 2003 5:52 PM
> To: Struts Users Mailing List
> Subject: Re: where to put request constants
>
> On Thursday 22 May 2003 7:54 pm, Dan Allen wrote:
> > I have been studying a lot of different struts code lately but I
> > can't seem to come to a conclusion about one particular issue.  It
> > was discussed earlier on this list that constants should be used
> > wherever possible in place of strings.  This prevents typo bugs from
> > showing up.  However, I am not sure where to stick these strings.
> > The strings I am referring to are the attribute names for storing
> > data into the request object.  I considered making an AppRequest
> > class that would hold the strings, but I am not entirely sure.
> > Also, following the model that Ted set down, I was also using
> > constants whenever I did a findForward() call, but again, I am
> > confused where to put those constants.
> >
> > Dan
>
> One way you can do this is to put the constants in one or more interfaces
> that
> contain just the constants (and no function definitions). You could put
> them in an interface named AppRequest, for example.
>
> With the new java 1.5 features you'll be able to do a static import of the
> constants from another class/interface. For now you can either refer to
> them by name (AppRequest.CONST_1) or have your class implement the
> interface and then just refer to it as, say, CONST_1.
>
> --
> If education is too expensive, try ignorance.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org

-- 
If education is too expensive, try ignorance.


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: where to put request constants

Posted by Brandon Goodin <ma...@phase.ws>.
FYI The Constant interface is considered an anti pattern

Q: What will the new static import facility offer developers?

A: It lets the programmer avoid prefixing static members with class names.
People really want this, so much that they implement so-called constant
interfaces to get the effect:

// "Constant Interface" antipattern - do not use!
public interface Physics {
  public static final double AVOGADROS_NUMBER   = 6.02214199e23;
  public static final double BOLTZMANN_CONSTANT = 1.3806503e-23;
  public static final double ELECTRON_MASS      = 9.10938188e-31;
}

public class Guacamole implements Physics {
    public static void main(String[] args) {
        double moles = ...;
        double molecules = AVOGADROS_NUMBER * moles;
        ...
    }
}

This is a very bad idea: interfaces are for defining types, not providing
constants. The fact that the Guacamole class uses the Physics constants is
just an implementation detail, but it "leaks" into the public API of the
Guacamole class. Not only does this confuse the clients of this class, but
it creates a long-term commitment. Even if you re-implement the Guacamole
class so that it doesn't need these constants, you still have to implement
the interfaces, as clients of the Guacamole class can now depend on the fact
that it implements Physics.

The static import facility offers a clean alternative. It lets the
programmer avoid qualifying static member names without subtyping. It's
analogous to the package import facility, except that it imports static
members from a class, rather than classes from a package. Here's how it
looks:

import static org.iso.Physics.*;

class Guacamole {
    public static void main(String[] args) {
        double molecules = AVOGADROS_NUMBER * moles;
        ...
    }
}

Note that this works whether Physics is an interface or a class. If it just
defines constants, it should definitely be a class rather than an interface.

http://java.sun.com/features/2003/05/bloch_qa.html

Brandon Goodin

-----Original Message-----
From: Dichotomy [mailto:dichotomy@greenmail.ch]
Sent: Thursday, May 22, 2003 5:52 PM
To: Struts Users Mailing List
Subject: Re: where to put request constants


On Thursday 22 May 2003 7:54 pm, Dan Allen wrote:
> I have been studying a lot of different struts code lately but I
> can't seem to come to a conclusion about one particular issue.  It
> was discussed earlier on this list that constants should be used
> wherever possible in place of strings.  This prevents typo bugs from
> showing up.  However, I am not sure where to stick these strings.
> The strings I am referring to are the attribute names for storing
> data into the request object.  I considered making an AppRequest
> class that would hold the strings, but I am not entirely sure.
> Also, following the model that Ted set down, I was also using
> constants whenever I did a findForward() call, but again, I am
> confused where to put those constants.
>
> Dan

One way you can do this is to put the constants in one or more interfaces
that
contain just the constants (and no function definitions). You could put them
in an interface named AppRequest, for example.

With the new java 1.5 features you'll be able to do a static import of the
constants from another class/interface. For now you can either refer to them
by name (AppRequest.CONST_1) or have your class implement the interface and
then just refer to it as, say, CONST_1.

--
If education is too expensive, try ignorance.


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: where to put request constants

Posted by Dichotomy <di...@greenmail.ch>.
On Thursday 22 May 2003 7:54 pm, Dan Allen wrote:
> I have been studying a lot of different struts code lately but I
> can't seem to come to a conclusion about one particular issue.  It
> was discussed earlier on this list that constants should be used
> wherever possible in place of strings.  This prevents typo bugs from
> showing up.  However, I am not sure where to stick these strings.
> The strings I am referring to are the attribute names for storing
> data into the request object.  I considered making an AppRequest
> class that would hold the strings, but I am not entirely sure.
> Also, following the model that Ted set down, I was also using
> constants whenever I did a findForward() call, but again, I am
> confused where to put those constants.
>
> Dan

One way you can do this is to put the constants in one or more interfaces that 
contain just the constants (and no function definitions). You could put them 
in an interface named AppRequest, for example.

With the new java 1.5 features you'll be able to do a static import of the 
constants from another class/interface. For now you can either refer to them 
by name (AppRequest.CONST_1) or have your class implement the interface and 
then just refer to it as, say, CONST_1.

-- 
If education is too expensive, try ignorance.


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org