Which component should hold a constants class

112 views
Skip to first unread message

buddy

unread,
Oct 12, 2013, 11:06:50 AM10/12/13
to clean-code...@googlegroups.com
@UncleBob often said that you can pass data between components using simple datastructure like a hashmap or a dictionary. It would make sense to store the dictionary or map key in a constants class. Suppose the controller and the interactor component, where should that constant class be ?

1. In a seperate component with static variables class. That component will be distributed with interactor and controller for example. Will it make interactor and controller unstable ?
2. Each components have it's own constant class, but at some points, it'll violate DRY.
3. suggestions ???

Cheers
JFD

Per Lundholm

unread,
Oct 12, 2013, 6:22:17 PM10/12/13
to clean-code...@googlegroups.com
I disagree. Don't use hashmaps and such if you can rely on a typed language like Java. There is so much runtime checking if parameters are in hashmaps. And you still need to define the keys somehow as you say.

Better to invent a class that represents the intention. If you have trouble finding the name for the class, maybe there is trouble with your intentions.

Read my blog on builder pattern for Java and perhaps you agree with me. http://blog.crisp.se/2013/10/09/perlundholm/another-builder-pattern-for-java

Cheers
  Per


--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.

Sebastian Gozin

unread,
Oct 13, 2013, 3:51:03 PM10/13/13
to clean-code...@googlegroups.com
I think there is a place for hash maps and such even in a typed language such as Java. There's a lot of ceremony involved in creating a static data structure, making it fit with other parts of the system and builders to help you use them properly. Sometimes all that effort is just too much when compared to the simplicity of a dynamic hash map.

Though as usual with subjects like these it's hard for me to come up with a definitive context why one would choose one over the other. I'm not sure that's even possible.

On topic:
I suppose this set of constants would go in the module which holds the boundary API to be used between the 2 components. In a static language like java that would be together with he set of interfaces that define the boundary.

Having said that. Such a module would have to be pretty stable and as a result I should expect those constants to also be quite stable so why would we need to store them somewhere rather than hard coded? Wanting to keep them in a constants class to me sounds like you expect them to change and thus they would not be stable. Or perhaps it could be a smell indicating that you would prefer a static contract in this instance at which point you should probably do what Per is suggesting.

- Sebastian



On Sunday, October 13, 2013 12:22:17 AM UTC+2, Per Lundholm wrote:
I disagree. Don't use hashmaps and such if you can rely on a typed language like Java. There is so much runtime checking if parameters are in hashmaps. And you still need to define the keys somehow as you say.

Better to invent a class that represents the intention. If you have trouble finding the name for the class, maybe there is trouble with your intentions.

Read my blog on builder pattern for Java and perhaps you agree with me. http://blog.crisp.se/2013/10/09/perlundholm/another-builder-pattern-for-java

Cheers
  Per
On 12 October 2013 17:06, buddy <jeanfran...@gmail.com> wrote:
@UncleBob often said that you can pass data between components using simple datastructure like a hashmap or a dictionary. It would make sense to store the dictionary or map key in a constants class. Suppose the controller and the interactor component, where should that constant class be ?

1. In a seperate component with static variables class. That component will be distributed with interactor and controller for example. Will it make interactor and controller unstable ?
2. Each components have it's own constant class, but at some points, it'll violate DRY.
3. suggestions ???

Cheers
JFD

--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discussion+unsub...@googlegroups.com.

buddy

unread,
Oct 15, 2013, 5:03:27 PM10/15/13
to clean-code...@googlegroups.com
Thanks for your answer. Both answer are great. Class or map, i think it's more a matter of choice and needs.

Because it's to transport data between 2 components, using a builder and writing new classes to transport the data could in some case be overkill. But, if the answer is complex and have a lot's of data, using a builder is a great way to achieve that.

I agree with you Sebastian that having a constant class within a boundary must be pretty stable. That class will probably be subject to changes as the system evolve, making the components using that boundary unstable and even that boundary could become unstable.

Cheers
Reply all
Reply to author
Forward
0 new messages