This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!

Junilu Lacar

+ Follow
since Feb 26, 2001
Junilu likes ...
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
Forum Moderator
Junilu Lacar currently moderates these forums:
Columbus OH
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Junilu Lacar

And I do apologize for jumping down your throat a little bit there. I should have given you the benefit of the doubt and maybe IM'd you first about your intent. I did not give you the same deference someone else gave me for a snarky post recently and I failed to pay that forward to you. No hard feelings, I hope but please, going forward, also consider your audience and the skill-appropriateness of any examples you wish to share. We're here to help each other learn and if nobody can understand the example, then there's no learning.
4 hours ago

Gerard Gauthier wrote: lambda's are a nice convenience for the everyday programmer but their features are rarely explored or used to the extreme.

I can only agree to the second half of this statement. Yes, they are underutilized and not enough programmers are leveraging their power.

However, lambdas are more than just "nice conveniences." That kind of implies that they are syntactic candy, like the for-each loop. In my opinion, lambdas are more than that. They are an attempt to facilitate functional-style programming constructs in Java. That requires a paradigm shift in the way you approach problem solving. Perhaps that is why or at least partly the reason why not that many programmers have adopted them them in their daily work.
5 hours ago
Here's the thing: it rubs me (and probably a few other people) the wrong way when you post something like that in a beginner forum and basically say, "Here's an example of what an awesome programmer like me can do with lambdas and closures and if you come away in awe of what I did even if you don't understand it, it's because I know what this code is doing and you still have a long way to go before you will."

If that wasn't your intent, it sure comes across like it. If you had posted this in our other forum about Puzzles and programming diversions, it would have come across totally differently and we would be having a different kind of conversation.
5 hours ago

Gerard Gauthier wrote:I think you missed the point, even when that point was mentioned in the post.
The difference between a good programmer and a exceptional programmer is, the exceptional programmer knows where the boundaries are.

Did you mean the stated point of "demonstrating the mind blowing concepts that are possible with lambdas and closures" or the implied point of being an exceptional programmer? Because I still disagree that you demonstrated either of them. Sorry.
5 hours ago

Gerard Gauthier wrote:I decided to post a contrived lambda example to show the Java rookies what can be achieved with this simple idea.
This posting was not a tutorial on lambdas, this posting was demonstrating the mind blowing concepts that are possible with lambdas and closures. If you come away from this post shaking your head in bewilderment... Then the post did its job.

I know quite a bit about lambdas and closures and I'm pretty sure that what you refer to as a "single method interface" is officially called a "functional interface." You also refer to the EqualTo functional interface as a lambda, which it isn't.

I'm also shaking my head in bewilderment, not because I don't understand lambdas and closures but because I fail to see how people coming away from this post shaking their heads in bewilderment achieves your stated goal of showing Java rookies what can be achieved by this simple idea. Unless, of course, if the "what can be achieved" that you had in mind was that this simple idea can be used to create terribly convoluted code that even experienced people like Campbell would have a hard time understanding...

Martin Fowler wrote:Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Lambda expressions were meant to provide a better alternative to anonymous classes in Java. They are quite powerful but with much power also comes much responsibility. In my opinion, your example shows how that power and responsibility can be easily abused. It's a pretty good example of how you shouldn't program with lambdas. If that was your actual goal then, yeah, I think your post did its job.
7 hours ago
I'd be ok with aBoxForAnyExtra(candybars) or aBoxForAnyLeftover(candybars). When reviewing code, have someone read it to you while you're not looking at the code. Our ears alone can give us a lot of good feedback about whether or not code is expressive and understandable.
1 day ago
In the real world, code should be reviewed for clarity and feedback like Liu's should be considered. Then there would be a discussion and some experimentation. On critical functionality that would require fine-grained expressiveness that Liu suggested, so that people don't stumble over any kind of misunderstanding of the code, Liu's point would be well taken. In this context, not so much, at least for me. The name extraBoxAsPerNeed does not appeal to me as much and the distinction it has over boxForExtra isn't that much. However, I might experiment for a minute with similar names with like intent such as boxForAnyExtra(candybars). My goal is to make the code read like a casual conversation, which I find much more pleasing. "Plus an extra box as per need for candy bars" just isn't consistent with the way I talk in real life. However, I would say "plus a box for any extra candy bars" because to me that already implies that if there are in fact no extras, I won't need another box.

Unfortunately, there are very few programmers even in professional settings who care to spend even a minute considering these kind of things and they keep writing code that makes other people and themselves stumble and fumble over what the code they're working with means. This literally costs companies and society billions of dollars per year.
1 day ago

Steven Fraser wrote:My brain exploded.

What made it explode, the idea that you can move your code around and make it so it actually reads like a story?

This is what I find disappointing about how programming is taught these days. It seems like it's all about syntax. Nothing about problem solving or writing clear, expressive code. To me, programming is mostly about the latter; syntax is just a detail. If nobody can understand your program, it's not much better than useless. Code spends most of its lifetime being read, not written. When it does need to be changed, then code that is easy to read is far easier to change than code that's difficult to read and understand.

As Martin Fowler (the author of "Refactoring: Improving the Design of Existing Code") says, "Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

1 day ago
The code I suggested before,

could also be written as:

This code is clearer than the previous version and only requires a few seconds more of refactoring. Many people question me about code like this, particularly whether or not it's "too much". I don't think it's too much. Anyone concerned with saving a few milliseconds overhead managing the call stack is worrying about the wrong thing. The amount of time a person reading the code spends trying to understand naked formulas, e.g. (hasExtra(candyBars)) ? 1 : 0 instead of boxForExtra(candyBars) far exceeds the time the computer would spend making another method call.

Generally, I prefer saving people some effort in reading my code over trying to save a few milliseconds by avoiding a method call. In fact, there are articles out there that demonstrate that refactoring to make that extra method call might actually allow the compiler to generate faster bytecode.

Small private methods have a good chance of getting inlined by the Java JIT compiler so why should I preempt that at the cost of readability? If I write code like the above, readers of my code have it easier and I also make it easier for the compiler to make optimizations like inlining of one-line return statement methods like those in boxForExtra() and hasExtra().

Anyway, in a program like this, readability is a bigger concern than performance. If you're going to tune your code for performance, use a profiler, not your gut.

See when does the Java compiler optimize and inline methods?
1 day ago

Ana Yo wrote:Is it becuase the x will always restart to 3 in the else statement?

Well, it's because x is always set to 3 at the start of the method. That means the if part will never be executed. Since the else part is always executed and that's the part where the recursive call is made, then recursion never stops until you've reached the full capacity of the stack. Once that happens and you try to recurse again, then you get the StackOverflow.

Have you tried following the execution yourself? What is confusing you? You are asking questions that you could answer yourself if you only followed the execution by hand. That's an important skill to learn if you're going to understand how a program works.
1 day ago
Telling users how long they have to wait is a double-edged sword. As it is, there could be a number of factors that are out of your control that determine how much time a SQL Query will actually take so giving an accurate wait time is iffy at best. If the time you give is too long, the user may decide that it's not worth waiting or get frustrated anyway. Either way, it's not a good user experience.

Since you're using an asynch call from the client side, you could just report "Still working on your request... will inform you when it's ready" and use a "toaster notification" or something like that to indicate when the results are ready. At that point, the user can go back to the screen and you can display the results of the query. This might require some caching of the results though and you might have to implement some timeout logic, say if the user does not view the results within 20 minutes of the notification then they'll have to run the query again. Or you could just find some way to cache the results on the client side through an AJAX callback.

These are just some ideas off the top of my head so if any of those sound like something you'd like to try, you'll have to do some experimentation yourself
1 day ago
There are many ways to solve that problem. Your solution is pretty good, considering your experience.

Here's your code with a few tweaks (not getting into OO yet):

Read this code and compare with what you wrote. Do you think this code is more expressive? Do you see the difference in how it's organized?
1 day ago
A lot of confusion in this thread. We have to consider that English is not OP's first language. That adds to the lack of clarity.

OP said he was trying to clean up code. That means source code. He also refers to "java class file" which, to Bear's point, is unclear. Is that supposed to be the .java file or the .class file?

Next, OP says the support guy told OP "you don't need the java class file." We don't know if that was what was actually said or just OP's interpretation.  Assuming the support guy knew what he was talking about, he meant the .java file because the WAR file would contain the .class file. OP already confirmed that he can successfully bring up the form in a browser and submit it so we know the .class file is not missing from the deployed WAR file.

What we don't know right now is how OP's source code is organized and what servlet that form gets submitted to. If OP answers my follow up questions we should be able to make some progress.
2 days ago
Tim, while that's all good, OP has already confirmed that the form can be submitted successfully. If that's true, I seriously doubt anything is actually missing.

OP's original goal was to do some refactoring so I think its reasonable to think OP is looking for the JSP file or the .java file of the class that handles the request made when submitting that form.
2 days ago