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
(keep public parts private until JForum day)
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Junilu Lacar

You're welcome.

You are asking some very interesting questions that show a greater than average level of diligence and analysis. I think it's great that you are putting so much thought and effort in your learning and practice. Keep it up, you're doing great so far.

One thing: try to look for the kind of subtlety that I explained here. Don't get caught up in just the technicalities of syntax and language mechanics. While those are important to know and understand, it's the things like semantics (what your code means in a more abstract sense) and proper organization that will make your code and your ability to write semantically coherent code that will help you get really good.

As I explained to someone the other night, don't just focus on your vocabulary and grammar. Good composition is what makes for great writing. In programming, composition is about design and organization. Study those things, too.
1 day ago
I think the book is referring to the switch part, not the case selectors.
1 day ago
I think the example code is more about semantics rather than syntax.

The printQ() method deals specifically with queues so on the client code side, it's more appropriate to declare any reference that is passed as an argument to printQ() as a Queue rather than as the actual runtime type of LinkedList which implements Queue. This way, the code is explicitly declaring that it is interested in the capabilities defined by the Queue interface rather than the fact that the implementation is actually a LinkedList.

The difference in cognitive load is a bit subtle but I think not insignificant. It's the difference between saying "I'm going to use a bunch of LinkedLists to do some Queue work" and "I'm going to use a bunch of LinkedLists, which are Queues, to do some Queue work." The second form is more explicit and clear that you're really interested in the Queue nature of the things you're using, not the LinkedList nature.
1 day ago

Carey Brown wrote:Protected is like private with the exception that an immediately inherited subclass also has access.

That seems to imply that not all subclasses have access to protected members, which isn't true. All subclasses can access a protected member because you can't specify a more restrictive access for an inherited member in a subclass. You either keep the same access specified by the superclass or you make it wider.

Can you clarify what you mean by "immediately inherited"?
1 day ago
Try declaring those constants as char instead of Character. See definition of constant variable here: https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.4
1 day ago
For convenience, this is the code you should discuss:
2 days ago

Campbell Ritchie wrote:And my apologies for misunderstanding what you were showing yesterday.

No worries, Campbell. I'm surprised it doesn't happen more often to me given that my posts are usually TL;DR
2 days ago
NOTE: See first two paragraphs in this post https://coderanch.com/t/689726/java/STUDENT-CHALLENGE-suggested-threads-reply to understand the context of this thread.

This is primarily for any of our new Ranchers who are students wanting to complete their quota of posting to online forums. Anyone else is still welcome to contribute to the discussion.

The Premise

We often say around here that programming is 10% typing and 90% thinking.

At dinner last night, I was asked "How should students be taught how to program?" when I started to gripe (again) about how students are taught computer programming these days. I gave the following analogy:

In your high school English classes, you were probably taught vocabulary, grammar, and composition, right? In my opinion, the most important part in writing is composition. Whether it's an essay for English or a program for a CS course, what makes your work "good" is really the composition part. The way students are taught programming at most places of higher education these days, however, it's basically all about vocabulary and grammar. That is, it's mostly about syntax and algorithms. When it comes to writing computer programs, "composition" is about design. Good composition and good design have at least one thing in common: a clear and coherent expression of ideas.

The coding horror of Arrow Code is very easy to recognize once you know what it is. People who don't know much about "composition", i.e. design, in programming are usually the ones who write horrific Arrow Code.

The Challenge
Look at the code in the first post in this thread (see below also). It is an example of the Arrow Code antipattern (follow the other link and read about Arrow Code if you haven't already). That should be immediately apparent to you at first glance.

Discuss this: What can you do to make that code better and eliminate or reduce the amount of arrow code? 

Please post your answers as replies in this thread.
2 days ago

Ryan McGuire wrote:My point is that in cases where it does make a difference, ++i is more likely to one that's needed.  If that's true, then I'd rather have had ++i become the standard rather than the exception. 

For instance...

In that code, I want i, j and k to all have the same value at the top of the loop.  If I had used k = j = i++, the second time through the loop, i would be 1 while j and k are still 0.

Thanks, Ryan, for trying to examine this line of reasoning with me without getting defensive. I'm glad you took it the way I had hoped you would.

I fully agree with you about the (out loud) readability aspect of ++i that you described earlier. However—and I know you're just trying to make a point—this latest example you gave still fails to convince me that there's any compelling reason besides correctness of program logic to prefer to use ++i instead of i++.

The problem is related to what Campbell pointed out earlier: the examples we're giving are hardly "preferable" because they are examples of very poorly factored code. We can't ignore that and focus solely on the merit of ++i over i++.  The context is always important and if the context we use stretches the bounds of reason, then our "proof" or reasoning is also shaky. 

It's kind of like asking whether you'd prefer to die by heart attack or by drowning. Well, if you really think about it, any sane person would rather NOT die, right? So neither of the choices given is really a "preference". And saying "Well, if you HAD to make a choice" doesn't cut it either. We always have a choice to NOT write poorly factored code.

In your last example, the variables j and k presumably only start with the value of i in each iteration of the loop then subsequently change independently of i in the for-loop body.  Well, in that case, the variables j and k should not be included in the for-loop header since the only variable that factors into the loop control is the variable i.

With this refactoring, it's clear that only the variable i controls the loop iteration and now you're back to choosing between ++i or i++ as a matter of style and personal preference, not program correctness.
2 days ago
NOTE TO STUDENTS CONCERNED: It has come to our attention that we have a number of new student members here at the Ranch who have been tasked by their instructor to post at least 5 answers in forums like this. We think this is a great challenge and an awesome learning opportunity. However, to minimize duplication and a lot of "Me, too!" replies, here are some threads that we suggest you check out and try to add meaningful discussion to.

NOTE TO MODERATORS: Please feel free to add to this list of threads. Try to choose topics that may be somewhat challenging and involves having to employ some critical thinking and a defense of opinions as much as displaying accurate understanding of technical issues.

Suggested threads for students being challenged to post answers here:

Simple (syntax, standard library usage, simpler algorithms)

Hangman Game: https://coderanch.com/t/678466/java/read-list-words-text-file

Using a loop to perform a calculation: https://coderanch.com/t/678392/java/method-returns-sum-row-array

Intermediate (TBD)

Advanced (longer discussions that are mostly about design and object-orientation)

* Discussion of an implementation of the Tic-Tac-Toe game:


* Discussion about an implementation of Stack:


* Debugging messy code


3 days ago
The line to focus on is line 6 in the gen() method, where a call to gen() is made as part of the expression on that line. The expression must be completely evaluated before return can be done.  It will keep doing that until the if statement on line 5 evaluates to false and execution falls down to the else part to execute the return on line 8. At that point, the recursion is ended and the whole stack unwinds.
3 days ago
So again, I can't reconcile my experience with Ryan's claim that he has seen cases where it's actually preferable to use preincrement over post increment.

Maybe we're reasoning with different definitions of "preferable". Maybe he meant to say "suitable" or "appropriate" instead. If that's what he meant, then I can agree with it for reasons I have already gone over ad nauseum.
3 days ago
Nah, even if it was used on the left hand side of an assignment, there is still no real preference available since the two forms can't be functionally equivalent.
3 days ago
Of course I did reason that you would hardly prefer doing something that is considered poor form or practice when I responded to Liutauras earlier, so I could have eliminated The Assignment form of a ForUpdate immediately. You never prefer to use a preincrement or post increment expression as part of an assignment unless maybe it's the only term on the right hand side. But then again in that case it wouldn't really be a preference because only one form can be correct in any given context.

What if the pre/post increment expression was used in the left hand side of an Assignment ForUpdate... ? Hmmm... Have to think about that for a second.
3 days ago

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . Let's say we define a for-loop like this:. . .

There are all sorts of things you can do. ...
Yes, you can define a loop like that, but as other people have said, what horrible code.

I was trying to reason about using a pre- or post-increment expression as part of an assignment as a valid scenario where one might prefer one form over another. The merits of using pre or post increment in an assignment, or lack thereof, was not part of that reasoning.

That reply you quoted was part of the long series of replies in response to Ryan's one assertion. I was just trying to find a way to reconcile his claim that in his experience, a pre-increment expression can be preferable to a post increment expression in some cases. That is not something that meshes with my own experience so I was trying to eliminate possibilities. You might want to go back a few replies more to get more context.
3 days ago