Example of misusage of exceptions.
From: Skybuck Flying (nospam_at_hotmail.com)
Date: 10/31/04
- Next message: Arun Hallan: "Ant language schema"
- Previous message: Sudsy: "Re: stack implementation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 31 Oct 2004 19:01:19 +0100
Hello,
First I like to say I appreciate that this person has made a website about
adaptive huffman compression and even supplied some code.
However the code is pretty shocking ;) and a good example of misusage of
exceptions... probably because out of lazyness ;)
http://www.cs.sfu.ca/cs/CC/365/li/squeeze/AdaptiveHuff.html
This person/code is using/throwing exceptions instead of returning a boolean
value as I think that he should have done in the first place.
The first function tries to find a character if it can't find it it simply
(?!) throws an exception.
The second function tries to find a tree node with a certain frequency if it
doesn't find it it also throws an exception.
The update tree method has a try catch block... this whole construction
obfuscates the logic path quite considerably...
For example at first glance it looks like the 'catch' block can be 'entered'
from both functions... in reality however the second function will always
find the frequency... so the programmer did not clearly express that in the
code.
// two functions from other class.
/**
* findChar
*
* Returns the index of the character
*
*/
public int findChar (char letter) throws NoSuchElementException
{
for (int i=0; i < 2*ALPH_SIZE; i++) {
if (tree[i].letter == letter) return i;
}
throw new NoSuchElementException(" in findChar");
}
/*
* HighestInBlock
*
* Returns the value of the node that is the highest in the block
* i.e. of all nodes with the same count
*
*/
public int highestInBlock (int count)
{
int highest = -1;
for (int i=0; i < 2*ALPH_SIZE; i++)
{
if (tree[i].count == count) highest=i;
}
if (highest == -1) throw new NoSuchElementException("No such node
with count of " + count);
return highest;
}
public void updateTree(char newchar)
{
int current;
int max;
try { // first appearance for
symbol
current = findChar(newchar); // Go to symbol external
node
// could enter catch block ?! yes
max = highestInBlock(tree[current].count); // Node number max in
block?
// could enter catch block ?! probably not !
if (current != max && tree[current].parent != max) {
addMessage(" Swapping nodes " +current+ " and " +max);
swap(current, max); // Switch node with
highest node in block
pause();
current = max;
}
addMessage(" Increasing count for '" +newchar+ "'");
tree[current].count++; // Increment node weight
pause();
} catch (NoSuchElementException e) { // Yes
addMessage(" Spawning new node for '" +newchar+ "'");
current = spawn(newchar); // NYT gives birth to
new NYT and external node
current = tree[current].parent; // Go to old NYT node
tree[current].count++; // Increment count of
old NYT node
pause();
}
while (current != root) // Is this the root
node?
{
current = tree[current].parent; // Go to parent node
// no exception handling at all ?
max = highestInBlock(tree[current].count); // Node number max in
block?
// no exception handling at all ?
if (current != max && tree[current].parent != max) {
addMessage(" Swapping nodes " +current+ " and " +max);
swap(current, max); // Switch node with highest node
in block
pause();
current = max;
}
tree[current].count++; // Increment node weight
}
drawTrie(dispRect, null);
}
Pretty lazy isn't it ?!?
Bye,
Skybuck.
- Next message: Arun Hallan: "Ant language schema"
- Previous message: Sudsy: "Re: stack implementation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|