aspose file tools*
The moose likes Java in General and the fly likes style guide - views Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "style guide - views" Watch "style guide - views" New topic
Author

style guide - views

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I found this page while reading Paul's profile. A nice attempt to simplifies coding standards.
However, some of the things which I think are dated or not “right” are:
1. Matching braces always line up vertically in the same column as their construct.
2. All method names should be immediately followed by a left parenthesis.

Also, no mention on access specifier (or modifier) for methods of a class. My view is, all methods should be public to make them easy to "unit test".

What's say.

Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

"Not right" is subjective: some people prefer their braces that way. This is a religious discussion not worth getting in to.

Making all methods public isn't necessarily the best way to make things testable, either; a test class can just extend the class under test, or you can use reflection.

Depending on the type of code you're writing making a method public may be a recipe for disaster (think API).

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
My view is, all methods should be public to make them easy to "unit test".


I totally disagree. You should only expose those methods that you intend to, and that are meaningful to the functioning of the API. Exposing methods that are only used internally needlessly increases the coupling between the class you're creating, and the classes that use that class. If, in the future you need to refactor, you'll find that it's hard to change a public API.

John.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@John: I think it's important to make a distinction between different types of code, though.

As an example, a web site's action classes probably don't need many, if any, private methods--the classes won't normally be extended. An API, however, has significantly different usage and design patterns that wouldn't make sense for a one-off web application.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
@David:

Yes, I agree. I was trying to make the distinction between just making all methods public willy-nilly as Ankur proposed, and thoughtfully deciding which methods should be exposed. You're absolutely right that there are many cases where having all or most of a classes' methods public is fine and even necessary. I think that there are also cases where exposing the wrong method(s) exposes the internal workings of a class.

John.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I agree that if you are going to expose your application as an API to public (or internally too), exposing all methods might confuse users. I mostly never worked on such applications so expressed that as a statement.

@David: How extending a class will make its private methods testable?

I have few comments for reflection too but I'll take some time though...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Not a "beginning Java" question. Moving.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://c2.com/cgi/wiki$?MethodsShouldBePublic


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Ilja Preuss wrote:http://c2.com/cgi/wiki$?MethodsShouldBePublic

I disagree, in my opinion the public API of any class should be as small as possible, and methods should be private and only be <default>, protected or public when there is a specific reason to do so.

If it's about testability, then give your methods default (package) access instead of public, and put your test classes in the same package (but in a different source tree) as the program classes themselves.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, that was a link to a discussion, not an opinion (and a quite interesting one, I'd say), so I'm not sure that it's even possible to disagree with it.

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In my not so humble opinion:

- wanting to test a method directly that is not naturally part of the public API of a class is a smell. Most likely, that method belongs to a different class, where it would naturally be public.

- there is a big difference between a public and a published method: http://martinfowler.com/ieeeSoftware/published.pdf
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Ilja Preuss wrote:
- there is a big difference between a public and a published method: http://martinfowler.com/ieeeSoftware/published.pdf


More of a word game, IMO.
If it's public & it's public API then it's published. You can't do anything about it...





ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Ilja Preuss wrote:In my not so humble opinion:

- wanting to test a method directly that is not naturally part of the public API of a class is a smell. Most likely, that method belongs to a different class, where it would naturally be public.



Do you say that a class will never have private methods which need tests?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Ankur:

Private methods don't exist in a vacuum. They are called by public methods to do behind-the-scenes work, and will therefore be tested when the public method that calls them is tested.

John.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
ankur rathi wrote:
Ilja Preuss wrote:In my not so humble opinion:

- wanting to test a method directly that is not naturally part of the public API of a class is a smell. Most likely, that method belongs to a different class, where it would naturally be public.



Do you say that a class will never have private methods which need tests?


A private method that needs its own tests is likely to be a non-trivial method. That the method is private means that it isn't the main responsibility of the class - the main responsibility of a class is in its public methods. Having a non-trivial private method in a class violates the Single Responsibility Principle. Moving that method to a class where it is naturally public improves the design.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
(By the way... Obviously, you can't create a jUnit test for a private method. The closest you can come is to make the method package level, and put the jUnit test in the same package. I guess the normal practice is to create a "test" source directory, and put jUnit tests there, with each test class in the same package as the class it's testing. From a theoretical point of view, you don't really need to test private methods since you only really care about "outside" behavior. For example, I can write a complete test for ArrayList even though I have no idea how the author implemented it.)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Max Rahder wrote:(By the way... Obviously, you can't create a jUnit test for a private method.)


Actually, you can. You can directly access private methods using reflection. It's a bad hack, though. The only reason I can think of to ever use it is to write a test for a third party component.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: style guide - views