Search the web
Sign In
New User? Sign Up
novajug · Northern Virginia Java Users Group (NOVA
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Defining Classes Within Methods   Message List  
Reply | Forward Message #13411 of 13718 |
Re: Defining Classes Within Methods

Dave -

Thanks for responding.

Yes, it is a human issue and not a compiler issue.

1) For me, having the classes defined within the method
enhances readability. The nesting tells the reader that outside
of the context of this method, the class is inaccessible and
therefore irrelevant. And when viewing the class in its
entirety, the outer scope is not "polluted" with something that
doesn't really belong there. To me this is no more than another
application of the same principle that guides us to use local
variables rather than the more global instance variables -- in
fact, classes defined within a method or called local classes.
It is also similar to declaring something private -- it is a
more precise definition of the actual scope of use.

In the cases I referred to, the class definitions were really
the only thing happening in the method, other than the
instantiation of them. So they didn't take away from the
readability of the method, IMO.

2) Testing is an interesting point. In this case, they were
trivial classes used to construct a GUI object; the behavior of
that object was tested in other ways.

----

Regarding the question about memory allocation, it *does*
matter in Java, IMO. Consider that common practice is to create
an object outside of a loop, even though its scope could be
defined more narrowly by creating it *inside* the loop. So we
are *already* compromising (albeit in a small way) precision of
communication for the sake of performance. We're not going to
take the drastic step of switching languages because of a few
loops, but we'd also like to not do something stupid.

Regarding the message to Brian Goetz, here is something that
might work:

----

We understand that the allocation and collection of short lived
objects is much cheaper than that of longer lived objects. We
also understand that we don't need to be as concerned as we
were in earlier days with coding for performance.

Nevertheless, in writing our programs it would be helpful to
have some idea as to the relative magnitudes of the costs, so
that we can avoid severely impacting performance, especially if
the approach we chose was intended to improve that performance.

So, what kinds of differences are we talking about? How many
orders of magnitude? Just how cheap is the allocation and
collection of a short-lived object (not counting its
construction)?

One case we discussed was a loop. How much time and memory
allocation would need to happen inside of the loop to make it
likely that that an object created outside the loop would *not*
be considered a short lived object?

These questions are not precise, and we understand that the
answers would also not be precise. Even imprecise information
would be helpful, though, as we currently have next to none.

Thanks.

----

- Keith


--- In novajug@yahoogroups.com, David Bock <dbock@...> wrote:
>
> Keith,
>
> Regarding garbage collection and memory allocation, the speaker you
> referred to earlier was Brian Goetz. If you summarize the thread into
> a concise question for him, I'll forward it on. But in short, I
> suspect his answer for this situation would be something along the
> lines of, "small short-lived object creation is a non-issue for Java
> performance. If you are in a coding situation where you think that
> matters for the performance of your algorithm, you should probably be
> in a language other than Java in the first place".
>
> Back to your initial question, this isn't a matter for the compiler
> - I suspect almost the same bytecode would be produced in both cases,
> with the only difference being related to the scope they could be
> created on. So if the difference in coding style isn't for the
> compiler, who is it for? It is for us, the humans maintaining that
> code. So I would ask you:
>
> 1) Does narrowing the definition in scope make it more or less
> readable to you? (Does the locality of the definition make it more
> relevant, or 'in the way' of the comprehension of the method?)
>
> 2) In what scope is it more or less testable? If defining it in such a
> place means I can't write unit tests and get code coverage for it,
> that would be a major drawback, even if it led to in-place
> understanding.
>
> -db





Fri Jul 3, 2009 11:39 pm

krbennettmd
Offline Offline
Send Email Send Email

Forward
Message #13411 of 13718 |
Expand Messages Author Sort by Date

Keith, Regarding garbage collection and memory allocation, the speaker you referred to earlier was Brian Goetz. If you summarize the thread into a concise...
David Bock
javaguy_98
Offline Send Email
Jul 3, 2009
9:18 pm

Dave - Thanks for responding. Yes, it is a human issue and not a compiler issue. 1) For me, having the classes defined within the method enhances readability....
Keith R. Bennett
krbennettmd
Offline Send Email
Jul 3, 2009
11:39 pm

Ignoring the GC, loader, memory model discussion for a moment. Your current configuration is going to be "less obvious" for other developers and more difficult...
clay_shooter
Offline Send Email
Jul 6, 2009
2:30 pm

Clay - Moving the classes inside the methods has nothing to do with optimization. It's about expressing more clearly the scope of the class. It's a human...
Keith Bennett
krbennettmd
Offline Send Email
Jul 8, 2009
1:32 am

Your description is interesting but unconvincing. You are trying to simulate closures so you create multiple intra-method classes across several methods. I'm...
clay_shooter
Offline Send Email
Jul 13, 2009
2:31 am

Joe - ... simulate closures so you create multiple intra-method classes across several methods. I'm still having trouble how this increases clarity in the...
Keith R. Bennett
krbennettmd
Offline Send Email
Jul 16, 2009
1:32 am
 First  |  |  Next > Last 
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help