• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Proper code format with if/else

 
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a real simple one here. I notice that I do not need an else in this method and would like to know if in cases like this should I use the else or not?



 
Master Rancher
Posts: 3876
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the method return the value of searchItem or null?  If so, should the method be boolean?

 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Does the method return the value of searchItem or null?  If so, should the method be boolean?



It should and does return the value of searchItem if it exists. For example, if I add Bread to the list and search for Bread it returns Bread, but if I search for bread it returns null.  
 
Norm Radder
Master Rancher
Posts: 3876
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a description of a method that should return either true or false.
 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:That sounds like a description of a method that should return either true or false.



Well I thought this was going to be very simple but I guess there is always a twist that I did't see coming. As I progress through the code, and then call the above method from my main method using the code below I see that the code above's purpose is to provide a
way for the code below to know if searchItem exists or not.  But, it does not look to me as though the code above returns a true or false, but rather an index position of the item if it exists, and if it doesn't a null. Yes or no?  

 
Norm Radder
Master Rancher
Posts: 3876
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the findItem method returned a boolean then this statement
 
would be changed to this
 
Sheriff
Posts: 7647
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:I see that the code above's purpose is to provide a
way for the code below to know if searchItem exists or not.  But, it does not look to me as though the code above returns a true or false, but rather an index position of the item if it exists, and if it doesn't a null. Yes or no?


Well, it could return true or false, but it doesn't. If it were, those print() statements could have been handled by the other method, i.e. (pseudo-code):

And in this case itemExists(itemToSearch) replaces your method searchForItem(), except that instead of printing its finding results in a form of text, it returns true or false, and leaves printing the textual part (UI) for other method.

No, it doesn't return an index position. What (which line(s) of code) makes you think it returns an index?

Correct, it returns null if a searchable item isn't found.

 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm you are correct.

Liutauras Vilda wrote:

No, it doesn't return an index position. What (which line(s) of code) makes you think it returns an index?



Line 3 of the original code:  .indexOf(searchItem) -  doesn't this code return the index position of the first occurrence of the search item I am looking for?
 
Liutauras Vilda
Sheriff
Posts: 7647
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:Line 3 of the original code:  .indexOf(searchItem)


Ouh yes, I was looking to your most recent code snippet...
 
Norm Radder
Master Rancher
Posts: 3876
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my version of the code:
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not have your searchForItem() method return a string like that ("found bread in our grocery list").  searchForItem should return whether or not the item is found (i.e. a boolean).  Later, you might want to implement a way to delete an item.  You'd only need to delete it if it is found in the list.  If your searchForItem returns a formatted string like that, you can't use it to see if you need to delete the item or not.

You should have a method that calls the searchForItem method, gets teh boolean, and then does whatever needs doing based on that.  This makes your searchForItem much more flexible and useful in more places.
 
Saloon Keeper
Posts: 22272
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if that's a true List, then the simple "contains()" method should suffice, I think.

No, an explicit "else" is not required for every if statement, and in many cases, I actually consider one to be undesirable. One of my favorite anti-bugging techniques looks like this, in fact:


This one's a little bit contrived, but you can see that it armours itself against unpredictable return values by explicitly setting a "fail" return value as the default right up top and ONLY if there is success will that value be changed.
 
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Someone once wrote:The two hardest problems in computer programming are 1) cache invalidation, 2) naming things, and 3) off-by-one errors


About some of the names we've seen so far:

1. boolean findItem() -- "find item" is an imperative phrase, i.e., a command. Semantically, you'd expect this to initiate some kind of action and it would make more sense to return the item that was found rather than a boolean to signal success or failure. As already noted, the method contains() would suffice if you just want to know if a List contains a certain value. Note that contains() depends on the element class' implementation of equals().

2. integer hasA() - "has a" implies a question answerable by yes or no which again brings us back to contains() being sufficient for this purpose.

bryant rob wrote:in cases like this should I use the else or not


Since this is a very short method, most coding conventions allow foregoing the else part. There are many ways to skin this cat:

Another option would be to throw an exception of some sort, like an ItemNotFoundException of your making. That could (probably would) lead you to a discussion about checked vs. unchecked exceptions (But please, let's not digress into that discussion here; I just wanted to make a point that there are many design choices from which to choose, the merits of each being up for discussion and lively debate).
 
Tim Holloway
Saloon Keeper
Posts: 22272
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:2. integer hasA() - "has a" implies a question answerable by yes or no which again brings us back to contains() being sufficient for this purpose.



Which I considered as I coded it and then senility promptly made me forget about something appropriate. Like maybe "hasAt()".

Searches are a member of that peculiar class of function which can actually return 2 different classes of results. "-1" is a convention for "not found", but it's simply an illegal index with no inherent meaning. Throwing a "NotFound" exeption offends some. You could create a "NotFound object class and wrap Integer, but then the method would be subject to class-casting issues AND the common superclass would be Object, which is sloppy. You could also return a SearchResult class object with a "isFound()" property and an "index()" property, but that's a bit much for most.

No clean, easy or universal solution in Java, alas. Though it's not something I've seen other languages handle much better.
 
Junilu Lacar
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a non-trivial application, I tend to gravitate towards a design that separates "What you want to do" and "how it's done", i.e., a semantic layer that allows me to write fairly intent-expressive code that's loosely coupled with an implementation layer that hides the nitty gritty details. This way, something "offensive" like throwing a NotFound exception is contained in the lower levels of the application and gets translated into more appropriate and consumable forms as it propagates upwards through the application layers. When it gets to a human user, it will be as a user-friendly and actionable message or if it's a software client, something reasonable that is covered by a contract test.
 
Bartender
Posts: 4006
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coming back to the subject: there is also a ternary form of the if/else. No doubt Campbell will tell more about this when he reads this topic.

Don't forget there is also the good old iterator to find an object, without needing an index.
 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have included my code for my Main and GroceryList classes. This is an exercise in the Java Master Class that I am taking, and  I do realize there is a better way to handle this as the instructor pointed out,  but with where I am at in my learning of java I can understand this logically.  I will be modifying it to be more user friendly instead of having the user to enter a number for selection purposes.

What I have found is that in order for me to learn the material I have to start with something basic. Then experiment and modify it and sort of see what changes do what. Then I go to Oracles tutorials and other websites and read a little further in depth with regard to the subject matter and experiment and modify a little more. When it is all said and done, I feel that I can conceptualize what I am wanting trying to accomplish and put it on paper. And usually if I can put it on paper by drawing it out I can put it in code. I may not be 100%, and I may need to look back over my notes, but unless I am taking a certification isn't coding an open book exercise anyway.















 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Since this is a very short method, most coding conventions allow foregoing the else part. There are many ways to skin this cat:



Thanks. I would prefer to use the else at this point. It helps me to read the code a little better.




I will look at this. Thanks. I definitely am for using the least number of variables when possible.


Another option would be to throw an exception of some sort, like an ItemNotFoundException of your making. That could (probably would) lead you to a discussion about checked vs. unchecked exceptions (But please, let's not digress into that discussion here; I just wanted to make a point that there are many design choices from which to choose, the merits of each being up for discussion and lively debate).



Good because I have not gotten to exceptions yet, but I will be there pretty soon and am sure that I will have another 100 questions.
 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
Another option would be to throw an exception of some sort, like an ItemNotFoundException of your making.



Even though I told myself today and pretty much stated here that I would leave exceptions for another day I did some reading and can anyone agree on anything in java? I read arguments for if/else and I read arguments for throwing the exception.  I happen to like the else because it is simple. But, lets say for instance I have a simple app like above and there are 3 of us going to the grocery store today (mom, dad, and daughter) and daughter deletes milk after purchasing milk, but I know for a fact milk was once there. It returns null...and I wonder what happened to milk? But, after daughter deletes milk and I search for milk because I can't remember the brand of milk on the list, an exception is thrown that lets me know groceryList.remove(searchItem) occured, now I know someone purchased milk and must have deleted it from the list.  This make better sense to me, so if I am anywhere correct in my assumption as to an exception being thrown this would be a place for it.  Yes or no?  
 
Tim Holloway
Saloon Keeper
Posts: 22272
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throwing an Exception is a relatively expensive thing to do. A lot goes on. You have to construct an Exception object, it has to be tied to a stack trace, and then the JVM has to walk its way back up, executing any "finally" clauses in the way before it at last reaches something that will catch the Exception. Even if it's the JVM's last-resort/abort function.

So you don't want to use it for trivial purposes and especially if the offending condition happens frequently.

On the other hand, sometimes an Exception is the cleanest way to arrange things. As with much in software, it's as much an art as it is a science, though.
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:can anyone agree on anything in java?  



You made me laugh. Let me give you another example:



This seems to me to be the standard way to indent blocks in Java nowadays. But there's another way to do it:



Now it's possible to discuss the pros and cons of those two styles. But once upon a time it went beyond discussions and I'm sure that people lost or quit their jobs because of insisting on one style over the other. That predated Java, other earlier languages also used {braces} as delimiters and needed to be formatted nicely too. However the holy wars seem to have died down lately. If you want to read more (a lot more) about that sort of thing you can check Wikipedia: Indentation style.

 
Tim Holloway
Saloon Keeper
Posts: 22272
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heretic!
I don't care what Linus Torvalds says!

Seriously, though, the brace-on-the-line versus brace-on-separate-line controversy was only one of a number of style arguments that ranged in those fabled days, and yes, a lot of it pre-dates the invention of Java or even C++.

My view was that the brace-on-line version was preferable when you printed your program listings, since it allowed more lines of meaningful text on the page. Just as I now object to lines that are so line that they either wrap randomly or get truncated when printed or displayed on a smaller screen. Well, I did back then, too, once I moved from 132-column printers on 17-inch-wide fan-fold paper down to page printers on US Letter/A4 sizes.

However, the brace-on-its-own-line is perhaps more readable. I'm not going to worry about that one. Especially since printing is a last-resort option for me these days.

There is a class of program called a "pretty-printer" than can take program text and apply a set of formatting rules to it. Also many of today's IDEs have their own formatters, and if you don't like their standard arrangements, there's usually a style preferences dialog you can bring up.

Now the one thing I will not forgive is this:


I've been burned too many times when an extra statement got inserted above the return clause and thus ruined the logic of the thing.
 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote: If you want to read more (a lot more) about that sort of thing you can check Wikipedia: Indentation style.


I am always interested in a good read and it was interesting. Just seems to me that rules would prevail and the pre-java languages that uses/used curly braces would have been rule oriented dictating the formatting of the braces...but, the early programmers weren't accountants either were they!
 

Paul Clapham wrote:


I seem to prefer this way only because Intellij formats in this manner.



Tim Holloway wrote:However, the brace-on-its-own-line is perhaps more readable. I'm not going to worry about that one. Especially since printing is a last-resort option for me these days.
There is a class of program called a "pretty-printer" than can take program text and apply a set of formatting rules to it. Also many of today's IDEs have their own formatters, and if you don't like their standard arrangements, there's usually a style preferences dialog you can bring up.



Tim, I did seem to have a harder time reading the code when the curly braces are located on their own lines but the more and more I read code I do seem to like that method. I do have to wonder why these IDE's like IntelliJ do not have a better print out format than it does. When I first started printing my code out I spent quite a while trying to figure out how to print without the code all over the place on the page.  I usually copy over to notepad or wordpad and the formatting works better for me. I will have to read more about this "pretty-printer."
 
Marshal
Posts: 69845
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:. . . Intellij formats in this manner.  . . .

That is because the old Sun Style Guide recommended such indentation. There is nothing to stop you finding the options in your IDE's formatter and enabling the one to put all braces on lines by themselves. (That doesn't work too well for array initialisers ‍) And why are you worrying about printing your code? Who uses paper for coding any more?
I recommend you decide which indentation convention you are going to follow, and set up your IDE to use that convention. Then stick strictly to that convention. When you find a programming job, they will tell you to do something slightly different, so you will have to learn a new convention. That learning process will take you ages, well over ten minutes.
 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: There is nothing to stop you finding the options in your IDE's formatter and enabling the one to put all braces on lines by themselves. (That doesn't work too well for array initialisers ‍)


I will be sticking to my original learned curly brace format. No sense in changing now.


Campbell Ritchie wrote: Who uses paper for coding any more?


Me. It's hard for me to trace and check off issues that I am having on a computer screen, and understand the code. Plus, I mainly print the code out and make notes along the way so that I can learn from my mistakes, compare the original way I did something  to how I changed the code, etc. Works for me, but I am open to recommendations for an alternative method of learning this. I don't print out as much as I did a month or two ago because I have adjusted my note taking, and do not necessarily need the code to be able to remind me what my notes mean. But, I still do prefer to print some code.  

For example:
When I originally wrote the code to modify an element in my grocery list, in order to reduce duplicate code in my modifyGrocery() method I just called the remove() and add() methods. Made sense to me.  But, I soon realized I was modifying the actual ArrayList instead of the element at index[i], and was not obtaining the result that I was looking for. It worked, but was it really doing what it was supposed to do?  So, what did I do? I did what I am suggested to do on here ‍ and searched for a thread on CodeRanch about this and soon discovered https://coderanch.com/t/561935/java/difference-set-add-method-list  which showed me the set(), which by the way who shows up on that thread...you and Paul 8 years ago. Man yall have been around huh . Now, normally I would have printed out the original code with the remove() and add() and then the code with the set() to show the difference and the results from the console. I realize you and probably everyone else is thinking "what the heck for?" but a month ago this would have helped me see the difference and understand it better. Now, I just put it in my notes comparing what I did and I can visualize the code...I couldn't do this a month or two ago. Anyway, I will probably state this a million more times moving forward but I thank each and every one of you for helping me.
 
Tim Holloway
Saloon Keeper
Posts: 22272
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And why are you worrying about printing your code? Who uses paper for coding any more?



I do. Not for coding, although in my collection I do have a set of vintage Fortran, Assembly and COBOL coding pads, all nicely ruled for keypunching into 80-column Hollerith cards.

For debugging. There is a class of problem that I call "Pink Pen" problems. This is a case where things have gotten so gnarly that I have to use a highlighting pen to accentuate the critical spots so that I can cross-reference them easily as I work through the logic. The highlighter is pink because yellow smears on laser prints, as does orange, blue is a bit weak, but pink sets a nice balance. Your Mileage May Vary.

I don't have many of these situations anymore, and they're not all code. A lot of times they're things like config files. And it's less necessary in an OOP environment where code modules are small to draw lots of circles and arrows and stuff.

But sometimes nothing less will do.
 
Campbell Ritchie
Marshal
Posts: 69845
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . I have to use a highlighting pen . . ..

I have never done that. I have however written the logic of the steps on a sheet of paper and cut the sheet into ½in wide strips. Sometimes I have to duplicate those strips. It was really good for implementing truly demonic choice. The only problem is that my implementation of

A ⊓ B ⊓ C ⊓ D ⊓ E ⊓ F ⊓ G ⊓ H

occupies about 1,440 lines of output code.

Yes, I have overreached myself about not using paper, haven't I? Any red faces are well‑merited
 
Junilu Lacar
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:Me. It's hard for me to trace and check off issues that I am having on a computer screen, and understand the code. Plus, I mainly print the code out and make notes along the way so that I can learn from my mistakes, compare the original way I did something  to how I changed the code, etc. Works for me, but I am open to recommendations for an alternative method of learning this.


Check out version control systems like Git and Subversion. Many developers keep their pet/toy projects on sites like GitHub or BitBucket. This is handy, too, when applying for developer jobs because many employers will ask (or just go out and search for) a candidate's public repository of personal projects as part of the screening process. Not having any fairly active public repos of personal projects can be a deal breaker sometimes and it's definitely something that could potentially give you an advantage over others. It's a double-edged sword though because you could also be exposing your weak spots as well, sometimes unwittingly.

As for notes, I use apps like Evernote and Joplin (learned about this one from a post another moderator made, I forget who it was now, maybe Tim H?). I hardly ever take notes down on paper anymore.
 
Tim Holloway
Saloon Keeper
Posts: 22272
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back in my pre-OOP days, I'd use a special print program. Some had put explicit form-feed character codes in their source to ensure that pages broke in a readable way when printed, but I don't like invisible stuff, so my print program looked for a line consisting of "/**/". These days, as I said, listings are usually shorter and less messy, so I let enscript do syntax highlighting for me, instead.

Yeah, I do love Joplin. I still use a sticky-note program for a lot of my quick on-screen notes, but I'm migrating the wisdom data to Joplin where I can still get at the server rescue instructions when the server itself is down. Which, alas, is exactly what happened on Monday. A deteriorating disk had been migrated to a good one but left in place. Unfortunately, the system boot services hadn't moved with the data and OS. I was up until 4 am trying to recover.

I really hate grub2. In olden times, you'd do a mklilo and it would install a bootloader on any disk you wanted, no questions asked. The grub2 bootloader is a lot more powerful, but it thinks it's an operating system and woe betide you if you don't have the necessary advanced modules already installed on a system disk that didn't already have a working grub on it. Especially when your connections to OS package servers are limited.  

Anyway, speaking of wisdom, a technical blog is a good thing to maintain. Mine is called "Things I wish I'd Known Earlier - But learned the hard way!" It's not a blog for vanity's sake, it's where I record my gripes and solutions to problems that were much harder than they should have been. It's hopefully useful to others, but it's also where I make sure I only make the same mistake once.

Incidentally, when publishing projects on a git server, make sure you include a good README.md file to describe what the project is and how to use it. It's all very well to be able to design and code good software, but documentation is more than just something to do because you're forced to. It also catches the eye of the casual reader much better than a block of filenames does when people visit the site.
 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Check out version control systems like Git and Subversion. Many developers keep their pet/toy projects on sites like GitHub or BitBucket.



I have a github account https://github.com/bryantlabs/JavaMasterClassProjects that i have been uploading exercises that I have competed while going through the course. I plan on taking a full course on git and github sometime in the future.   https://github.com/bryantlabs/JavaMasterClassProjects

Junilu Lacar wrote:As for notes, I use apps like Evernote and Joplin (learned about this one from a post another moderator made, I forget who it was now, maybe Tim H?). I hardly ever take notes down on paper anymore.



Thanks for the these. I will be looking at them. Anything to help me break the habit of paper and pencil.
 
Tim Holloway
Saloon Keeper
Posts: 22272
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, for the initial stages of program design, I like FreeMind, which is a mind-mapper. Kind of like an outlining program, but more graphical. While you can do actual outlines in Joplin or even MS-Word/LibreOffice Writer, Freemind is faster for me, and I like the ways that I can visually accent different items. What makes it especially useful is that even though it's a GUI app, most of the brainstorming work I do can be done with a few simple command and function keys so I can lay down ideas as fast as I can type them and easily cut-and-paste to re-organize and almost never have to grab for the mouse.

Mice are very important to me, but consider that a mouse was an optional piece of equipment for early versions of Windows and a whole culture evolved around the consistent meanings of hotkeys. And it was productive. And then web browsers came along and negated the whole concept, even though the first-generation browsers were text-mode apps (and some still are today).

Each Freemind item is just one line long, although you can extend that a bit and for that matter, put in hyperlinks, It's basically about an idea, not the details. But once I need detailed info, I can dump the mindmap into a form that's more friendly for extended documentation.
 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Also, for the initial stages of program design, I like FreeMind, which is a mind-mapper.


I watched a couple of you tube videos on FreeMind. I like the concept of putting my thoughts in a node versus on paper since I can change and edit a lot easier.

Tim Holloway wrote:almost never have to grab for the mouse.


Before learning to code I did not realize how often I used the mouse. Now, almost never.

 
bryant rob
Ranch Foreman
Posts: 95
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking of taking notes:

I took notes on paper with this one.

  • The user inputs an item into scanner in the Main class searching for the item in the grocery list.
  • item is passed as a parameter as a String to the findItem() in my GroceryList class
  • findItem() returns an integer index position using the indexOf() method and if item is found
  • findItem() returns the item  back to the if statement inside the searchForItem() method in the Main class where the originial findItem() method was called using the get() method
  • Two methods inside of one method - one returns and int - the position of item in the index, the other returns a String - the item's name. Both are needed in this case to accomplish my task at hand.


  • In my opinion, this is not the best way to handle this search but it worked for me and I understand it. I had to take notes on paper. I don't think a mind map or some other form of note taking would have helped me trace this out
    and understand it.  At this point in my learning I have to be able to connect the dots.  I am not so sure mind mapping would have helped here as much as pencil and paper.





     
    Tim Holloway
    Saloon Keeper
    Posts: 22272
    151
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, mind mapping is about capturing ideas, not about logic flows. There are other tools for that.
     
    bryant rob
    Ranch Foreman
    Posts: 95
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Holloway wrote:No, mind mapping is about capturing ideas, not about logic flows. There are other tools for that.



    The mind mapping would definitely help me with conceptualizing the idea. Please share what tools I may could use for the logic flows, because this is where I am having the hardest time putting it together. It just seems strange that I am going to call a method from one method, use two methods inside that method to return something back to the original method doing the initial calling. Kind of hard to follow at first.
     
    Tim Holloway
    Saloon Keeper
    Posts: 22272
    151
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Maybe you should look at ArgoUML. You can map the relationships between different classes and method calls over time using a "swim lane" diagram. There's also Call Graphs: https://en.wikipedia.org/wiki/Call_graph which don't show temporal sequences, but are simpler. A Call graph is usually all I need. Swim lanes are more often used for communications protocol mapping,

    A Call graph can be done via a mind map, actually, since it's just a tree of classes and how they invoke each other.
     
    bryant rob
    Ranch Foreman
    Posts: 95
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the info Tim. Looks interesting. Putting this in my notebook of "paper"  to look at later today.
     
    Tim Holloway
    Saloon Keeper
    Posts: 22272
    151
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joplin runs on phones and tablets. You don't need paper.

    It also has an alarm function if you need reminding.
     
    Liutauras Vilda
    Sheriff
    Posts: 7647
    522
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    bryant rob wrote:I took notes on paper with this one.

  • The user inputs an item into scanner in the Main class searching for the item in the grocery list.
  • item is passed as a parameter as a String to the findItem() in my GroceryList class
  • findItem() returns an integer index position using the indexOf() method and if item is found

  • ...


    Again, that reads like code with corrupted syntax. More likely you want to have notes in a form what logic might be excluding any technical language.

    3rd bullet point could read similar to: "looking for an item it tells at which position it was found or states it wasn't found otherwise"
     
    Campbell Ritchie
    Marshal
    Posts: 69845
    278
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    bryant rob wrote:. . .

  • The user inputs an item into scanner in the Main class searching for the item in the grocery list.
  • . . .

    Afraid I shall have to agree with Liutauras there. You don't enter items, but text representing their names. I also worry whenever I see people who have the concept of a “Main” class. I think it is unfortunate that NetBeans even creates a class of that name. There is nothing “main” about the class containing a method about which there is nothing “main”. Except that that is its name.
    Maybe our FAQ will help explain what you are looking for.
     
      Bookmark Topic Watch Topic
    • New Topic