"I always knew one day Smalltalk would replace Java. I just didn’t know it would be called Ruby."
-- Kent Beck
I got it from ozmmdotorg blog.
"I always knew one day Smalltalk would replace Java. I just didn’t know it would be called Ruby."
-- Kent Beck
IF a person has a pink monkey
THEN take a refrigerator
Blaine,
I don't read your blog as often as I'd like, but whenever I do it gets me thinking.
I cut my teeth on the first edition of "Java in a Nutshell" and agree many of the code examples are horrible. For some unfathomable reason, most Java books make little or no attempt to teach OO concepts, choosing instead to focus on syntax and the API.
The problem with correcting code is that your corrections are open for correction :) In fixing the "baby sins" you've created, IMHO, a more egregious one. public methods with filname parameters is generally a Bad Idea as it allows a careless coder to corrupt any file of his choosing.
You've also created some minor baby sins (embryo sins?) yourself. I believe redundancy does not necessarily lead to clarity. The repetitive "InCents" while well intended, gets downright annoying. I only need to be told once that we're dealing in cents. When you switch to another unit measure, let me know, otherwise it should be safe to assume nothing has changed. Make cents, er, sense? By renaming the method to addCentsToPurse, it pretty clear that cents is the order of the day.
Your constant and variable names are a little too "techie." I've lately come to understand that if code can be read by a non-coder, coders will be able to read it all the faster. seek(TOTAL_AMOUNT) is less technically accurate than seek(RECORD_POSITION) but it more clearly indicates what we expect to find at that location.
So, here's my version (hoping the format looks ok):
private static final String PURSE = "blah";
private static final String READ_AND_WRITE = "rw";
private static final int TOTAL_AMOUNT = 100;
public void addCentsToPurse(int cents) throws IOException {
RandomAccessFile purse = new RandomAccessFile(PURSE, READ_AND_WRITE);
try {
purse.seek(TOTAL_AMOUNT);
int centsInPurse = purse.readInt();
int total = centsInPurse + cents;
purse.seek(TOTAL_AMOUNT);
purse.writeInt(total);
} finally {
purse.close();
}
}
As a final note, I'm always tempted to combine multiple lines, as in:
purse.writeInt(centsInPurse + cents);
While this is a trivial example, it does help with debugging to use a new variable and multiple lines.
Jeff
Blaine-
I can't speak for Obie, of course, but I certainly don't feel the way you seem to think. I'm incredibly impressed by Seaside and Smalltalk in general. Avi is clearly a very smart guy doing very smart stuff.
I was IMing Obie during the talk, and said "it's probably just because I don't understand smalltalk, but this feels like total voodoo to me".
But I don't think voodoo is a bad thing. Voodoo is what gets me interested in something; I want to figure out how it works.
I don't feel like I'm in any position to comment on whether or not Seaside poses a "threat"... I don't even totally buy that there's a competition.
Avi, on how to do maintenance and debugging on Seaside servers: "We can just vnc into the server. Squeak has vnc built into it."
Django Jacob, "Of course it does..."
I'm having fun in this session, sitting next to Ben Bleything, and trying not to contribute to the sporadic chuckling and heckling about the voodooo magic of Seaside. This is definitely one of the more enjoyable sessions I've attended so far, but I don't think Seaside poses serious competition to any of the major web frameworks.
try {
//something potentially dangerous
} catch(Exception ex) {
ex.printStackTrace();
}
| When | August 1, 2006, 7pm-9pm |
| Where | Panera Bread 13410 W Maple Rd Omaha, NE 68164 (402) 964-1110 We have reserved the backroom |
//Open a file for read/write ("rw") access
RandomAccessFile f = new RandomAccessFile(datafile, "rw");
f.seek(100); //Move to byte 100 of the file
byte[] data=new byte[100]; //Create a buffer to hold the data
f.read(data); //Read 100 bytes from the file
int i = f.readInt(); //Read a 4-byte integer from the file
f.seek(100); //Move back to byte 100
f.writeInt(i); //Write the integer first
f.write(data); //Then write the 100 bytes
f.close(); //Close the file when done with it
private static final String READ_WRITE_ACCESS="rw";
private static final int TOTAL_RECORD_LOCATION=100;
public void addAmountToTotalAndSave(int amountInCents, String fileName) throws IOException {
RandomAccessFile totalFile = new RandomAccessFile(fileName, READ_WRITE_ACCESS);
try {
//go to total record location
totalFile.seek(TOTAL_RECORD_LOCATION);
int previousTotalAmountInCents = totalFile.readInt();
int newTotalAmountInCents = previousTotalAmountInCents + amountInCents;
//reset position to total record location so that we can write new total
totalFile.seek(TOTAL_RECORD_LOCATION);
totalFile.writeInt(newTotalAmountInCents);
} finally {
totalFile.close();
}
}
| When | July 11, 2006, 7pm-9pm |
| Where | Cafe Gelato 156th & Dodge 445-4460 |