• Post Reply Bookmark Topic Watch Topic
  • New Topic

Console based interface loop  RSS feed

 
Maciej Olborski
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am implementing a console based interface for an assigment. I've got a DAO interaces and implementations ready and I am stuck on a loop that checks whether to display logged in menu or logged out menu.



So when this runs it should show loggedIn() method only and only if the condition is not met. At the start when program runs I set loggedOutLoop = true and loggedInLoop = false. When I handle login I set loggedOut = false and loggedIn = true

What seems to happen is that the else statement doesnt work and the output I've got is only loggedOut() method displayed where I only want loggedIn() to be displayed without loggedOut().

Can somebody suggest something?

Mac
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that you got confused whether loggedOutLoop and loggedInLoop has true or false value at particular time.
I'd suggest you to add print statements to track all values, so it will become clearer what's happening.

Maciej Olborski wrote:What seems to happen is that the else statement doesnt work and the output I've got is only loggedOut() method displayed...

Nothing strange about your observation about else statement, because as you mentioned yourself:
Maciej Olborski wrote:At the start when program runs I set loggedOutLoop = true and loggedInLoop = false.

So, the condition is being met at Line 12, in this case else body won't be executed. And I don't see where those values could change.

What is not clear, is text below, what do you mean?
Maciej Olborski wrote:When I handle login I set loggedOut = false and loggedIn = true
 
Maciej Olborski
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe if I add more code it will become clearer.



Maciej.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO, the names you have chosen are adding to the confusion when reading the code because they don't fit with regular grammar. I suggest you choose method and variable names that will help you better understand what you're telling the computer to do. Also, you should break down the problem into smaller logical chunks, so your code is telling a story at different levels of granularity. Starting from a high level, describe what's happening by making a series of method calls. Each method call will represent a progressively smaller subset of tasks until you get down to low-level detailed work. Read the code below and notice how very conversational it is because the names of methods and variables help you understand what role they play in what's happening. They help tell the "story" of what the program does.

That's the first high-level cut. Digging down to doLoggedInActions, you'd have something like:

Notice how there are still no details here. It's all just breaking things down into smaller chunks and making method calls. This is how you can manage the complexity. The way you have it now, the "story" of your program is all over the place and you are getting confused with all the details of each task mixed in with the details of other tasks.

In summary:
1. Choose names that help you tell the story better
2. Break the work to be done into smaller chunks.
3. Don't mix details of one task with details of other tasks. Keep each task in a separate method so that details are contained in one place.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, now that I look at that top-level code, I can even abstract it out one more time:

So you have to constantly look out for opportunities to summarize and separate details out to their own little section in the code. Ironically, writing simple-to-read code seems to be one of the most difficult things to do.

One of my favorite quotes is from C.A.R Hoare, winner of the 1980 Turing Award. In his Turing Award lecture,
C.A.R. Hoare wrote:... there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu,

I'm impressed! Impressed of simplicity. Your upper post idea should be on the course at schools, uni's or whenever programming is involved.
This is where the all things should start. This is the art. Respect.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Liutauras.

My role at work involves a lot of mentoring of less experienced developers and these are the same kind of things I teach them. Sometimes I think my co-workers look at me funny when I see a 50-line method and tell them I can't stand looking at it and please can we break it down into smaller chunks. I think in their minds, it's already as simple as it could be but somehow I can almost always find ways to make it simpler after I understand exactly what they want to do. That's the trouble with getting older, you can't really hold too many things in your head at once any more. You have to simplify and compartmentalize to make it easy on the old gray matter to grok what's going on
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu for explaining this. This really is simple and awesome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!