Results 1 to 7 of 7
- 09-15-2009, 05:08 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
Programing Technique question - Try or If
I have snippet of code that has the possibility of generating a nullpointerexception. Here is the code:
Java Code:ArrayList overnightCities = ((SeqObject) seqHashTable.get(String.valueOf(seq))).getCities();
Java Code:if (seqHashTable.containsKey(String.valueOf(seq))) {
- 09-15-2009, 06:00 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
personally, I go with the if method of handling it, as I've been told try-catch blocks can create large overheads, and also because I've had difficulty catching RuntimeExceptions, notable ClassCastExceptions (so I use if (var instanceof MyClass) instead of a catch block). However, this is really just my personal preference.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 09-15-2009, 06:37 AM #3
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
Thanks Boyo, I like the idea of keeping the overhead down.
- 09-15-2009, 01:43 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
You shouldn't worry about any perceived overhead. You should code in a way that makes sense, logic-wise.
In your case, is it an error if seqHashTable.get(key) returns null?
In this situation, though, all you need to do is check for null. Try not to squeeze too much onto a single line:
Java Code:ArrayList overnightCities = ((SeqObject) seqHashTable.get(String.valueOf(seq))).getCities();
Java Code:SeqObject seq = seqHashTable.get(String.valueOf(seq)); if (seq == null) // Do something here, maybe throw exception? ArrayList overnightCities = seq.getCities();
- 09-15-2009, 05:57 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
Thanks for the heads up on one big line of code. I was wondering that as well. "If you can squeeze everything into one line, should you." The second parsed out way is more readable but not as glamorous. I don't know much about programming or Java and any chance I have of showing some sibilance of prowess I jump at. I have several instances where I grab an seq object like that and as a matter of fact here is some code from my program:
Java Code:displayaline((String) ((JList) evt.getSource()).getSelectedValue()); //Wow, evt.getSource retrieves the object that experienced the EVENT, then since you know it will be a JList //you cast it as (JList) that way you can access the functions of the JList item, namely ".getSelectedValue()" //that returns the value that you just mouseclicked on. Since we know it will be a String we castit as (String) //all this is seperate out below but Ithought I would try and combine into one statement, you know for fun! // JList temp = (JList) evt.getSource(); // String lineselected = (String) temp.getSelectedValue(); // displayaline(lineselected);
Last edited by TimHuey; 09-15-2009 at 06:00 PM.
- 09-15-2009, 06:12 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
I prefer open code.
It's easier to debug, and easier to maintain (as in your null problem above).
There are times when a oneliner is OK, the Hibernate Criteria stuff immediately springs to mind, but in that case you know what you've got (no nulls to bite you) since you're simply adding things in.
eg:
Java Code:criteria .add(Restrictions.eq("userId", userId)) .add(Restrictions.ge("orderDate", fromDate)) .addOrder(org.hibernate.criterion.Order.desc("orderDate"));
- 09-15-2009, 11:03 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 09:49 AM -
Improve my programing in applet
By Knights.innas in forum Java AppletsReplies: 4Last Post: 03-24-2009, 11:28 PM -
socket programing outside the network
By Omarero in forum NetworkingReplies: 4Last Post: 02-16-2009, 07:15 AM -
Hi, I am new to programing!
By Zrob in forum IntroductionsReplies: 1Last Post: 09-14-2008, 06:38 AM
Bookmarks