While coming up with APIs or a framework, design forms a crucial part of the end result. While a solid design does indeed allow for flexible usage of an API, usability goes hand in hand with the design. What better way is there to create an API, than to learn from the mistakes and successes of other developers first ? Lets take a look at some examples in the java API where things could have been better…
Stack and Properties:
A java.util.Stack provides a standard Stack implementation in the java language. But did you know it extend a java.util.Vector ? Why is this wrong ? Because you can do this
package com.design;
import java.util.Stack;
import java.util.Vector;
public class StackTests
{
public static void main(String... args)
{
new StackTests().go();
}
public void go()
{
Vector<String> vector = new Stack<String>();
vector.add("first");
Stack <String> stack = (Stack <String>) vector;
String peek = stack.peek();
System.out.println(peek);
stack.push("next");
stack.push("item");
String remove = vector.remove(2);
System.out.println("Removed: " + remove);
peek = stack.peek();
System.out.println(peek);
}
}
Output:
first
Removed: item
next
There are so many things that are going on there that are wrong.
- Vector is a slow DS. Its methods are synchronized and there is no way around this right now since this contract has been sealed. It cannot be changed without changing backward compatibility.
This has been a sad week for java. One of the string of bad things happening to it, is the abandonement by Mac OS X. Among the interesting thoughts that have been thrown out there for a possible rescue plan, this one seemed to stand out.
In a nutshell, the link points to a post by Adam-Bien suggesting that users might pay for a JVM on the Mac, thereby opening up a business opportunity. That might be far fetched considering
- Apple wants to be a control freak of sorts. Anything that is not objective-C is a target
- Folks usually have a very hard time convincing themselves to pay for something that they used to get for free.
- It would be pretty difficult to write a JVM targeted for the Mac from scratch, given that many native API calls are secrets.
Every developer out there has to troubleshoot a problem in PROD every now and then. The series of steps goes something like this
- User notices something fishy
- Clicks on a button that reads ‘Do not click here’
- Boom !
- Developer tries to figure out what happened by fishing through log files.
Now step 4 can be relatively more simple than it needs to be. It is often complicated by bad practices or obstacles like the ones mentioned below
- Developers log all application logs into a central server log
- Access to logs are restricted by a ‘log viewing’ application.
Instead of fishing around for errors, would it not be cool if you could visualize the errors that occur in your application through some sort of timeline ? After investigating a couple of frameworks that would allow one to do this, I figured ‘Simile Timeline‘ would be a good fit.
It was a normal day. The sun rose, birds were singing and everything seemed to be going fine until Kaboom ! A bug appeared in the live environment. Things turned downhill from there that day. But we are more interested in how things got fixed so lets focus on that.
An investigation of the code revealed that the bug had crept in because of a minor mistake. A variable was referring to the wrong column index on a table. ‘Hmmm… simple fix’ I thought. I told my manager it would not take more than 5 minutes to do. ‘Its a one line fix’. Mistake.
There were loads of other things to do after fixing this bug. This is what I ended up doing over the next 4 hours.
- Analyze if any other code flow would explode.
- Code the fix – This only took 5 minutes as expected.
An article was written a while back about how neural networks can be used to classify source code. Yes the source code that you write to feed to compilers / interpreters.
The article explains at a high level what method could be used to perform this activity. In the end the author claims some level of success and wonders how other neural-network implementations / techniques would solve the same problem. This got me curious enough and I spent a weekend trying to crack this with Neuroph, the neural network library for java. I present to you my analysis and results below. For the impatient here is the code-classifier DEMO.