aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes [URLyBird]Why not come in and debate on my code of find? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "[URLyBird]Why not come in and debate on my code of find?" Watch "[URLyBird]Why not come in and debate on my code of find?" New topic
Author

[URLyBird]Why not come in and debate on my code of find?

Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi ranchers,
I recently completed the find method in Data. I show the whole code here, hoping your advices and rectifications .
Some comments are as following:
1) my read() synchronize RAF and throw RecordNotFoundException with a String parameter "deletedRecord" in case of a deleted record and throw RecordNotFoundException with a parameter "overflowed" if the RAF's pointer out of the length of RAF.
2) i dont synchronize RAF in this method, cause the data will also be out of date even if i do such sychronizing. So i use a Collection to store the result of read().
Debate on improvement of arithmetic, brevity of coding, idea of designing are welcome and expected here.


SCJP 1.4 SCJD
Petr Hejl
Ranch Hand

Joined: Feb 26, 2006
Posts: 68
You should never use exception description in program!!!
1) exception type -> for program
2) exception stacktrace -> for programmer
3) exception message -> for user

Better should be to made descendants of RecordNotFoundException.

Oh, now I see description is parameter, but generaly this so called "enumerated exceptions" are bad idea. I would prefer something like
for(int i = 0; i < getRawRecordCount(); i++). Yes just after cycle ends the new record can be added, but this can happen in your code too. There is no chance (if you do not lock whole db for find) to avoid this - it is a real world
[ April 05, 2006: Message edited by: Petr Hejl ]
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
I would rather do it like this


[ April 05, 2006: Message edited by: Mike Corleone ]

MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Petr Hejl
Ranch Hand

Joined: Feb 26, 2006
Posts: 68
But if record number 0 would be empty record (deleted flag) you wouldnt find anything - worst case.
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
You also need to think of ways to optimise the find method. If all the fields are null, i.e. there is no search criteria, do you need to compare the field values?

Jason
Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284

Personally i use to avoid the usage of public method calls within other instance methods if those throw their own exceptions.
In your case i would define a private method like:


for being used inside other instance methods like find and read:


That is
'keep your exceptions for your clients'.

Regards
Ori
[ April 05, 2006: Message edited by: Oricio Ocle ]

SCJP, OCMJD, OCMJEA
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
To Jason,
If all the fields are null, i.e. there is no search criteria, do you need to compare the field values?

In case of all elements in criteria are null, my code will "continue" server times and then i (in for(int i = 0; i < 7; i++) ) will be 7, so no compare happened.

To Oricio,

In my opinion, adding new methods to Data is not a good choice, so I didnot yet use private getFileRecord().

In your case, every time you read one record, you will have to store all records into recordString first. Isn't it a waste of time?


To all,

Should I not use RecordNotFoundException's description in DB Layre? If not, could you give me an approach to get the status of deleted record and pointer at the end of RAF in the find method.
Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284

Hi Zhixiong!
Avoid inner exceptions is an extended good practice. Moreover if the exceptions are thrown inside a loop, and moreover if these exceptions are expected to occur frequently, as is your case.
You can google something like "exception cost" java and find lots of explanations.
Any way if you only are intested in performance, you can try something like this and extract your own conclusions:


Regards
[ April 06, 2006: Message edited by: Oricio Ocle ]
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi Oricio,

After run your code, I can clearly see that Exception throwing is a slow procedure. Thanks for your good code.

So I did some modification to my find.
1) In Data I create a Vector variable deletedRecordsNo store deleted records' number.
2) add into delete method
3) remove
and add before 'try'.
But a disadvantage can be easily seen that if server crash, then the deleted records can not be recognized.Does such a disadvantage matter and influence the score?
Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284

Hello Zhixiong,
In Data I create a Vector variable deletedRecordsNo store deleted records'
.
IMO, that procedure adds complexity and potential problems for a doubtful benefit.
Other case is implementing a full database cache, that obviously would provide a performance improvement, since, for example, you are reducing disk access.
You can seach for "cache" in this forum.
Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [URLyBird]Why not come in and debate on my code of find?