Tuesday, August 31, 2010

The importance of your Compile Time

Lot's of time i find myself working to shorten compile and build times. While most people agree with shorter is better for compile times, the questions of what is short? and how important is it? seem to remain vague and seemly open to judgement.

It's not. So I wanted to take a moment to clarify it.


 I vBlogged my thoughts here





You can see the excel sheet here
and read the Joel on Software Thread here

Monday, January 18, 2010

Recipes in Java

 Learning to Program Java with Recipes


 

 14 amazing kids showed up yesterday for a session at Wintriss Techinical school on learning to program Java! We tackled 2 recipes durning these 2 hours, and wanted to show how to setup your own computer so you can continue at home.

 
 
 

Here's What you need to do to get started with the Recipes. First the quick-links, then detailed instructions below.

 







  1. Import Workspace

Download Java SDK


 If you don't already have java on your machine you'll have to download it. How can you tell if you do have java? open an terminal window.
Mac : command+space then type terminal and hit enter
Windows : windowskey+r then type CMD and hit enter
In the terminal/command window type
java -version
if you see something like
java version "1.6.0_15"
you've got it, otherwise download it from Sun at http://java.sun.com/javase/downloads/widget/jdk6.jsp
  

Download Eclipse


You want to download the "Eclipse IDE for Java Developers (92MB)"
it's here
http://www.eclipse.org/downloads/

Download the Workspace


Download the "Learn To Program - In Java" workspace from CodePlex. http://learntoprogram.codeplex.com/
Unzip it to the directory of your choice. But remember the location, you will need in the next step.

Import the Workspace

In Eclipse, right click in the Navigator Window and select "Import Workspace".
A Window will appear asking for an import source, select "Existing Projects into Workspace", if you can't find it start typing existing and it will appear.
Click next.
The root directory is the location where you unzipped the workspace.  then simply hit finish.

If you are successful you should see the IntroToProgramming folder in the navigator window.

That's it, Happy Programming!
If you have any questions, please feel free to email me.


 

Recipes in Java

 Learning to Program Java with Recipes


 

 14 amazing kids showed up yesterday for a session at Wintriss Techinical school on learning to program Java! We tackled 2 recipes durning these 2 hours, and wanted to show how to setup your own computer so you can continue at home.

 
 
 

Here's What you need to do to get started with the Recipes. First the quick-links, then detailed instructions below.

 







  1. Import Workspace

Download Java SDK


 If you don't already have java on your machine you'll have to download it. How can you tell if you do have java? open an terminal window.
Mac : command+space then type terminal and hit enter
Windows : windowskey+r then type CMD and hit enter
In the terminal/command window type
java -version
if you see something like
java version "1.6.0_15"
you've got it, otherwise download it from Sun at http://java.sun.com/javase/downloads/widget/jdk6.jsp
  

Download Eclipse


You want to download the "Eclipse IDE for Java Developers (92MB)"
it's here
http://www.eclipse.org/downloads/

Download the Workspace


Download the "Learn To Program - In Java" workspace from CodePlex. http://learntoprogram.codeplex.com/
Unzip it to the directory of your choice. But remember the location, you will need in the next step.

Import the Workspace

In Eclipse, right click in the Navigator Window and select "Import Workspace".
A Window will appear asking for an import source, select "Existing Projects into Workspace", if you can't find it start typing existing and it will appear.
Click next.
The root directory is the location where you unzipped the workspace.  then simply hit finish.

If you are successful you should see the IntroToProgramming folder in the navigator window.

That's it, Happy Programming!
If you have any questions, please feel free to email me.


 

Saturday, January 16, 2010

Mock Smells


Today, Bob Martin tweeted :

unclebobmartin

I use mocking frameworks as little as possible. I think heavy reliance on a mocking framework is a smell. I usually write my own mocks.

 

 

I agree with this. Of course the 140 characters allowed in a tweet don’t really give much space to explain what that smell is. So let’s refine our nose.

Mocks let you test implementation. Anyone who ever gets uppity about the use of stubs, fakes & mocks is paying a lot of attention to this. With a mock you can verify that something has been called. While this can be useful, it is rarely part of the Behavior of a system, and often part of the implementation. Since there are literally 1,000’s of way to program the exact same behavior, Unit tests that lock a particular implementation will actually in up “protecting” your code from being refactored.  This smell tends to come up in newly written code, and is usually accompanied by the phrase

anytime I want to change anything a bunch of tests break and I have to go and fix them”.

 

Mocks let you fake very hard to fake calls. Ever need to mock out a call to HttpServletRequest?  There are over 40 methods on that interface. Even with the help of your IDE, that’s a pain. A good mocking framework (I prefer EasyMock) will let you do it in 2-3 lines. This is great, especially in legacy code, or API’s you don’t control. But it’s a cover for a much more insidious smell that exists in HttpServletRequest. Simply put, an interface should not have 40 methods. Now you might argue that was how many were needed to handle something as complex as a web call. And you would be wrong. Take a look at Rack (or our Port of it - JRack) it handles everything with 1 simple abstraction. 

When mocks are the easiest way to gain an handle into your code, you have coupled you code too tightly, and not left enough inserts points.

 

Mocks let you fake calls. Lastly, mocks let you fake easy calls too. One example I use a lot is Loaders. My Loader interface looks like this:


public  interface Loader<T>

{

  public T load() throws Exception;

}

 

 

I will constantly be making calls like


request.init2Edit(new MockLoader<Member>(member));

 

 

 

But here a mocking framework is overkill. It’s so easy to write the above line, than


Loader<Member> loader = EasyMock.createMock(Loader.class);

EasyMock.expect(loader.load()).andReturn(member);

request.init2Edit(loader);

 

 

If  I’ve kept my code clean, mocking frameworks just aren’t that useful.

 

So hopefully you will start to sense the same things in your code. Of course if your nose is prickling over some hard to test piece of code, grab your mocking framework. It’s your first line of attack.  Like grabbing some cologne when you are a bit smelly and guest are coming over, just realize that eventually you have to take a bath. 

 

 

 


Wednesday, March 25, 2009

Extensions Methods In Java!

Note: Oracle has locked down java so this is no longer possible :-(


W00T!

I just added extension methods to java. It's a bit of a hack, but a small bit. (you need to replace the java.lang.Object class)

First, the hack, download and follow the instructions in the readme file of Java_Extensions.zip

Now you can run this test successfully...

package com.spun.util.extensions.tests;
import junit.framework.TestCase;

public class ExtensionsTest extends TestCase
{
public static class MyStringUtils extends ExtendableBase<String>
{
public String removeVowels()
{
StringBuffer b = new StringBuffer();
for (Character c : caller.toCharArray())
{
switch (c)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :

case 'u' :
break;
default :
b.append(c); } } return b.toString();
}
}
public void testname() throws Exception
{
String name = "Hello World".use(MyStringUtils.class).removeVowels();
assertEquals("Hll Wrld", name);
}
}

Pay extra attention to this line:

"Hello World".use(MyStringUtils.class).removeVowels();

What's more: this use of extensions doesn't have name spacing issues, nor does it pollute all of your objects. It's IDE friendly supporting both code completion and quick fixes!

As a side note, if generics were done better, I would like it to be:

"Hello World".use<MyStringUtils>().removeVowels();

But this is available now! I'll blog more on it tomorrow, but I am very excited about it.

Saturday, January 10, 2009

That which is measured gets done.

The other day at my Letip meeting one of our members, Mikel Bruce, gave a great talk on goal setting.

One of the things he mentioned, which I particularly liked, was a tool called an awareness sheet. The idea is simple, write down all the things you want to do each day: exercise, eat diner as family, limit tv watching, floss etc… then at the end of each day, you check off what you’ve done
.

The important part here though isn’t in checking off what you’ve done. This is why you don’t check it off as you do it. The important part is the awareness of what you haven’t done. The benefit here is that it isn’t penalizing you for not doing it. It’s not like you have to do everything every day. It’s just making you aware of what you want to do, and if you’re doing it. However, this awareness will help you to actually do it.


Think of it as a small commercial, every night, reminding you what you want. Here’s a small picture of a sample month.

In the end, it's just measuring the habits you want. I'm also finding this useful for events. For example, an presentation awareness sheet. Just to keep me aware of all the details I want to improve when speaking.

Wednesday, October 15, 2008

Java Packing

Recently I have started to have a large appreciation for java’s class loader and jar packages. The combination allowing for many great things that becomes troublesome and cumbersome in other languages. But, I wanted to talk about a few things I believe could be much better.

Embedded jars.

Jar files can have class files in them, even resources in them, but not other jar files. Why not? If I have a project that needs a.jar, b.jar, myproject.jar, it would be simpler and easier to jar up the a & b jars into my jar. I’m not saying this is always the best way to go, but it should be an option.

Monkey Patching.
Monkey Patching is the ability to hack out a solution over someone else nice code. Interpreted languages like ruby or php always get this, since you get the source, not a binary. Now there are lots of bad things about Monkey Patching, but it’s really useful when you need it. Now I understand that if you don’t have source, Monkey Patching is not so practical.

However 100% of the jar files I am currently using are Open Source. As such, there should be a standard way of constructing a Jar file so the source is included. Additionally, there should also be easy ways to recompile the jar.
Like: javac recompile a.jar

Again, the jars I’m using are open source, but the ability to easily modify that source is very complex in java. Also, debugging into those classes usually loses source. We need a simpler way to work with the source the jar was made from.

Distilling Minimum class files.

This would also be nice if you could use the jar file the same as regular source path.
For example, let’s say I have a class my1 which uses a1, and a1 uses b1.
(short hand: my1-> a1 -> b1 ) but my source tree has

my1.java
my2.java
a1.java
a2.java
b1.java
b2.java


if I compile my1.java and let javac know my source path,
it’s will follow the links and only give me

my1.class
a1.class
b1.class


Nice right, the minimum needed to run my class. Now think how nice it would be if you had jar files with source attached,
a1 & a2 are in a.jar
b1 & b2 are in b.jar

but still you could get the same results, a jar file with just the minimum needed to run your app.

Right now, there might be a class that i'd like to use, but i'll rewrite it, because i don't want to include the whole jar that it's packaged in. I can't just remove the class file, because it depends on other class files. So I rewrite rather that bloat.
Why should I ever have to make that choice?