Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is an interface what has only constants called?

 
Sital Kotamraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any special name to that interface(an interface with public constants)? I was asked this in an interview.

Thanks
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I call it bad code.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
I call it bad code.


Indeed, Ken is in good company here. There's no word for that kind of interface, specifically, but doing things that way has been called the "Constant Interface Antipattern."
 
Sital Kotamraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is keeping constants in classes and using them elsewhere also a bad practise? I am talking about making classes soley for keeping constants...

Thanx in Advance,

Maki Jav
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maki Jav:
Is keeping constants in classes and using them elsewhere also a bad practise? I am talking about making classes soley for keeping constants...

Thanx in Advance,

Maki Jav


I would say it smells real bad. Why would you need to do that?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anything wrong with keeping constants in a class to use elsewhere?

In my opinion, no, provided you call your class an enum . . .

CR
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Is there anything wrong with keeping constants in a class to use elsewhere?

In my opinion, no, provided you call your class an enum . . .

CR

 
Steve L. Williams
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it worse then passing needed objects all over the place, just because they contain needed constants.

There is a reason many java API classes have public static final variables.

Have an interface may not be the best way to go about it, but it is hardly crippling.
[ May 25, 2006: Message edited by: Steve L. Williams ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve L. Williams:
How is it worse then passing needed objects all over the place, just because they contain needed constants.

There is a reason many java API classes have public static final variables.

Have an interface may not be the best way to go about it, but it is hardly crippling.

[ May 25, 2006: Message edited by: Steve L. Williams ]


Why would you be passing objects around just because they have a constant to begin with? You don't have to pass anything to have access to a public static variable.

Developers use a constant interface because they want to inherit the constants. That's not what an interface was meant for and it needlessly attaches an interface with no contract to your class.
 
Amit K Srivastava
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... It is always a nice to have if you place and organize constants at one place. It makes your code maintainable. I guess having a class in a java application holding constants for all the classes is better.

public class MyAppConstants
{
public static final class ModuleOne
{
public static final String ABC = "some text";
public static final String XYZ = "some other text";
.....................................
.....................................
}

public static final class ModuleTwo
{
public static final String OOO = "XXXXXXXX";
public static final String PPP = "xxxxxxxx";
.........................
.........................
}
............................
............................
}
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take your point, Amit Srivastava, but I hope you don't try to compile anything with that many public classes in.

CR
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic