开发者

collections inside or outside main method

I'm interested in opinions on design.

There are at least two ways to locate a Collection of commonly used data. Outside the main method were all methods can access the same Collection and change it's data. Or 2, inside main were I can make as many collections as I need and pass them as an arg to functions, objects or methods.

Which is better? My thinking tells me that 2 results in code that is easier to reuse and has better data encapsulation. But, I see a lot of examples 开发者_JAVA技巧of 1 out there which tells me 1 could be better. But, why?


Completely depends on usage. An encapsulated class might well have a list as instance state. Sometimes it's entirely inappropriate and it makes more sense to pass/return state.

There's no "better", only "appropriate."

Moved from comment response

What makes sense is context-dependent. Classes have properties--a list is a perfectly valid property. After all, if you had a "Person" class, you wouldn't pass around a "first name" attribute; it's a property.

If you find something is being passed around internally a "lot", perhaps it's a property instead, or another class, or... Conversely, if it's only loosely associated with the class and not part of its internal representation, it might make more sense to only have the list as a local and/or parameter.

Sometimes it's just a matter of aesthetics or convenience, too.


In general, fields should not be accessible outside the class in which they are declared. If you see examples of collections as fields in a Main class that are being accessed by other classes that is probably not good design. If the collections are being accessed only by methods in the class, that is fine. If other classes need access to the collections, they should be passed some mechanism to get the collection (either pass the collection, or pass an object that has a getter).


Most developers are working up a level - focusing on interactions between classes, rather than within one. In theory, you're right, but in practice, it's generally not very important - understanding usage of a class private collection is usually very easy - most IDE's will even highlight all the usages if you simply select any occurrence of the name. Because of the IDE help, it ends up being harder to follow data flow in code written in your style. Your instincts are right, though, when thinking about inter-class usage.

A case in which your model is required is when a class needs to be stateless - especially where an instance of it is called by multiple threads simultaneously. In this case, any per-thread data can't, in general, be stored at the class level, and must be passed between functions.


You want to find yourself in a position where you revisit the code and know exactly what each part's supposed to do, and exactly what it is doing.

For that, the obvious (and most commonly used in proper software engineering) is more like the second one.

Try and create a clear separation. Ask yourself questions such as:

  • Does this function really need to be aware of this collection to operate correctly?
  • Is is possible think in a different way in order to segregate the responsibilities in this code?
  • Or a more general: What are my other options here, and have I given some thought to the consequences each of them logically entail?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜