Archive for the 'Programming' Category

TSOT – Ruby Show and Tell Night

I was at TSOT last night with about 30 or so fellow community developers (Brent Ashly, Andrew Burke, Hampton – from Unspace, Joey DeVilla etc…) and had a blast. Its always fun to geek out with my programming peeps. Joey was MC with a quick counter rant on What Zed Said – very amusing. Andrew showed off the sign management application he has developed, Hampton gave an amusing demo/explanation on ziplocal and the pains of developing the site. There was a demo (also an unspace demo) on a site for the iphone touch – that updates you with sports scores – very nice ui. TSOT will be holding these coder show and tells on the virtues of rails development every second tuesday of the month.

Brent and I both had our Asus Eee PCs with us – see picture below – had lots of fun showing them off.
Brent and his Pink Eee

Joey

Pictures:
Joey At TSOT
Hampton

Should objects be responsible for their own stubbing?

I’m an advocate of TDD (test driven development) and I try too use it all the time when I’m coding. I have gotten accustomed to using JMock for a lot of my testing as most of what I test are servlets and such which require quite a bit of setup (HttpServletResponse, HttpServletRequest etc). The one thing I don’t like though is that using jmock exposes too much of the inner workings (or expectations of the inner workings) of the system to be flexible enough for rapid design. What I mean by this is that when I’m coding TDD style, I write my test case first and as I do I am thinking about how I may setup the class interface etc… Jmock forces me to setup certain expectations in advance (not with a gun to my head but pretty close) about how the code will actually implement the solution.

For example say I’m writing a login servlet called LoginServlet. This servlet class has a method called “doPost” that takes a ServletRequest and ServletResponse. My test case begins like this…

public void testLoginWithValidCredentials()throws Exception{   String email = "testmonkey@nodomain.com";String pass = "abc123";

StubRequest req = new StubRequest();

req.setParameter("email",email);

req.setParameter("password",pass);

req.setSession((HttpSession)mockHttpSession.proxy());

....

....

loginServlet.doPost(req,response);

}

Now I knew in advance that I was going to need a session object because I’m putting something on it after the login; what that is and how it works are unknown at the moment. Also at this point I don’t really have any expectations for my test case. I guess my expectation is that after executing loginServlet.doPost(…) that there will be some new information on the Session object. Thats about all I know to be true. So we can pull the session off the request object afterward and check that its there…. or using the JMock way we can set an expectation that the mockHttpSession receives a setAttribute message once with some data (At this point I have decided that I will store an Account object on the session). So we create our JMock expectation:

mockHttpSession.expects(once()).method("setAttribute").with(eq(Account.class.getName()),eq(account));
**Note:  I created an account instance object with some test data

Great, now we can know for sure that the info was set to the session. But how to we get that info?
All of a sudden I need to make all sorts of assumptions about how the code is going to be implemented instead of actually implementing it. I’m sure I’m just missing something here but if the way I get the data is by creating a LoginController instance inside the servlet then I need a way to control what comes out of that controller from the test case. Ok, so I could add an overloaded constructor to my Servlet that takes a LoginController instance and then store that inside the servlet for use during the doPost call… my gut tells me that may be the wrong thing to do. Lets try it out and make sure….

public void testLoginWithValidCredentials()

 throws Exception{		LoginControllerStub loginControllerStub = new LoginControllerStub();

 	loginControllerStub.setAccount(account);

loginServlet = new LoginServlet();

 	loginServlet.setLoginController(loginControllerStub);

String email = "testmonkey@nodomain.com";

 	String pass = "abc123";

StubRequest req = new StubRequest();

 	req.setParameter("email",email);

 	req.setParameter("password",pass);

 	req.setSession((HttpSession)mockHttpSession.proxy());

mockHttpSession.expects(once()).method("setAttribute").with(eq(Account.class.getName()),eq(account));

loginServlet.doPost(req, response);

}

Now we need to fill in the login servlet code…

public class LoginServlet extends HttpServlet {
	
	private LoginController loginController = null;
	
	public void setLoginController(LoginController lc){
		this.loginController = lc;
	}
	
	protected LoginController getLoginController(){
		if(this.loginController==null){
			this.loginController = new LoginController();
		}
		
		return this.loginController;
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse res) 
	throws ServletException, IOException {
		
		String email = req.getParameter("email");
		String password = req.getParameter("password");
		
		LoginController loginController = getLoginController();
		
		Account account = loginController.getAccount(email, password);
		
		if(account!=null){
			// successful login
			req.getSession().setAttribute(Account.class.getName(), account);
		}else{
			// something was wrong with the creds
		}
	}
}

And the test passes…. Hmmm maybe there is a clean way to do this after all….
The power of blogging wins again. This is really just a ramble run amok, if you have comments please send them to me.

Why did you do it that way?

I cam constantly in awe over how badly we all write code. We contort our ideas into one of three camps.

  1. Object Oriented
  2. Functional
  3. Curried Rice (aspect oriented, etc…)

And as such we loose much of the expressive nature in conforming to the rules of “Modern” programming.  I suspect that 30 years from now we (some of us anyway) will look back and marvel that anyone was able to write any meaningfully useful programs in the same way I now look back and am amazed that anyone wrote with punch cards.

Now I’m dancing on a line here and thats between the programming language and the programming model. I have heard it said that the language you code in will affect how you model your thoughts about programming. I agree with this as I have experienced it (I’m sure you have as well). But what about the inherent “model” of programming itself? Computers are an extension of (physical realization of) a mathematical idea. That is, you can feed one function into another function and end up with a more complex function. (I’m simplified it a bit but I hope you get the jist of what I’m saying – read Alan Turing‘s original paper) so in essence we are influenced by the rules of math when creating programs. Now I’m not saying there is anything wrong with that but in recent years with the proliferation of languages like Ruby etc. that call themselves 5th generational languages I wonder if we are not striving for something more than a mathematical way to express our ideas in programs?

I want a new way to think about and model my programs. Does anyone have any ideas?

WebService DataBase

Data Persistence as a Web Service

 

Imagine all you had to do to store and retrieve data in object form was to have:

 

Object obj = new Object();

obj.store();

 

and all the gory details about how it is stored could be ignored (sort of). In the java world and to some extent the .Net world you could use hibernate as it does a good job of solving the O/R Mapping problem. However it is still a not an ideal solution from a programmer perspective because its not transparent and can clutter up your business logic to some degree with all the DAO (data access objects) you need to write.

 

Would it not be great to take the approach that DB4O took with their object database (www.db4o.com) with their tiny footprint object database engine you can almost transparently persist and retrieve your objects. But its a little awkward to setup access to a shared version of the database. (see the db40 documentation for an explanation) After reading how it works think about how you might achieve this for a web application that has separate helper processes running on remote machines accessing the same database file?

 

It would be great to be able to have a database as a web service. A web service that allowed you to simply send a package of data and store it, retrieve it thought some criteria and filter, delete it and merge it seamlessly with other data available in its context.

 

What if the language of the transaction was simple and contained only two chunks of information:

 

  1. The object data itself (ie:

      1. String baz = “some string”;

        Integer yo = 103423;

    1. class foo extends object{

      }

  2. The meta data about this object:

      #GRAPH:{parent=object}

      #SUBGRAPH:{ child[0]=baz:type=String

child[1]=yo:type=Integer}

#OBJECTHASH:{234j23rjasxcmq023ejdmxaom3dkrj23r}

 

This would represent a self descriptive object that could be stored and indexed. Ah, but now comes the clincher. What if you have an object graph that needs to be stored and has interdependencies etc. Well thats a bit of an implementation detail but it would not be unreasonable to predict that some form of Meta-Meta-Data could form the foundation of the storage mechanism. If you take a look at the RDF standard it defines a way that definitions of “things” can be referenced by other “things” and this forms the basis of the Semantic web theory. I think that it would be possible to apply the same principals against the storage and retrieval with in a limited score with unbounded parameters. What I mean by this is that it would run off one system (limited scope) but its data constraints would be limitless (unbounded). I think also that this can be achieved using some combination of strongly typed languages and weakly typed languages.

More to come as I think about this some more…


April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Blog Stats

  • 6,328 hits

Flickr Photos