Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list-ops: include hints to explain why methods are static #378

Closed
jtigger opened this issue Mar 8, 2017 · 3 comments · Fixed by #864
Closed

list-ops: include hints to explain why methods are static #378

jtigger opened this issue Mar 8, 2017 · 3 comments · Fixed by #864

Comments

@jtigger
Copy link
Contributor

jtigger commented Mar 8, 2017

As per #177, we prefer using instance methods over class methods (see discussion in that issue for rationale).

However, there are conditions in which it is absolutely appropriate for a function to be static. A common situation is because the function is effectively an extension of an existing type — you are defining behavior you wished were defined on a given type.

Since classes in Java are closed for modification (i.e. you cannot add members to a class outside its definition like you can in other languages like Ruby or JavaScript), you cannot add the new behavior to the class, directly. The idiomatic solution in this case is to write a utility method.

Collections of these kinds of methods are often referred to as "utility classes". Examples of such classes from within the JRE include Arrays and Collections.

In the case of the exercise list-ops, we "wish" that List had map(), reduce(), filter(), etc. methods on it. It doesn't, so we're including them here.

Please include a HINT.md file in list-ops that points out that these methods are static and capture the description above, in your own words as to why this class has this shaped API.

@ilya-khadykin
Copy link
Contributor

Would you mind if I claim this one?

@FridaTveit
Copy link
Contributor

Great, go for it! :)

@ilya-khadykin
Copy link
Contributor

@FridaTveit done, I hope it looks good 😄

ilya-khadykin added a commit to ilya-khadykin/java that referenced this issue Oct 5, 2017
FridaTveit added a commit that referenced this issue Oct 6, 2017
list-ops: add hint of using static methods per #378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants