This blog has moved to a new URL:
http://r-c-r.tumblr.com/
Recursion: See Recursion
A blog about bits and pieces in the computer science sphere.
August 6, 2010
June 28, 2010
Java and Scala Trends
Here's a comparison of what the relative Java and Scala trends are:
This shows the absolute comparison:
This shows the absolute comparison:
April 25, 2010
Note to self about Scala and IntelliJ
I am using IntelliJ as IDE for almost everything including Scala.
However, I had an issue with IntelliJ not compiling my Scala code due to the following error:
"Error: fatal error: object scala not found."
If you have the same error the solution is to add a module dependency to the scala-library.jar file (found in your Scala distribution).
Happy coding!
March 23, 2010
The key to successful unit testing is isolation
The key to successful unit testing is isolation!
A couple of years ago, when Spring framework entered the scene, we had some issues with unit testing in my team. Since Spring is based on IOC we felt we had no control of the code since just instancing a class and run tests on it would generate a lot of null pointers.
So we came up with the, what we thought back then, brilliant idea to wire up Spring for every unit test. This way we knew that all Spring beans would be set on the class to be tested and, hence, we got rid of the malicious null pointers.
But this introduced another problem, namely that if we have a service bean that utilizes a DAO bean we would have to make sure that the DAO bean being used would have a database to run on. Enter in-memory databases!
So now we had a test suite that could be run and no framework errors would prevent us from implementing unit tests for the code.
However, we did not realize that we were actually testing more than we wanted to!
In reality the tests we wrote for our service layer would also test the DAO layer.
Not really an ideal situation...
So what to do about this then? Spring is being used as defacto standard these days.
Enter Mockito :-)
With Mockito you have a tool that enables you to unit test exactly what you want.
Let me give you a little real-world example that I ran into the other day.
I am working with the customer system at my current employer and for each change it is important to track these changes with an audit trail. So, for example, if a customer logs into the system correctly an audit trail should be created with information about status (success) and some other information. Should the login fail then the same information apart from status = access_denied should be generated.
The audit trail is created with Spring's JmsTemplate and ActiveMQ's vm://localhost functionality (no socket connections involved).
So the code I want to test looks something like this (names etc. has been changed to protect my customer's code):
package com.x.domain.service;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.util.Assert;import com.x.domain.Authentication;import com.x.domain.AuthenticationStatus;import com.x.domain.LoginDetails;import com.x.domain.infrastructure.messaging.AuditLoggingProducer;@Service("authenticationService")public class AuthenticationServiceImpl implements AuthenticationService {@Autowiredprivate AuditLoggingProducer auditLoggingProducer;public Authentication login(LoginDetails loginDetails) {Assert.notNull(loginDetails, "LoginDetails cannot be null");// Simplified code - used for illustration of this exampleAuthentication authentication = null;if (loginDetails.getUserId() == 1) {// Allow user with id to login - every one else should be denied// The winner (pos 1) takes it all, huh? :-)authentication = createAuthentication(AuthenticationStatus.SUCCESS, loginDetails.getUserId());} else {authentication = createAuthentication(AuthenticationStatus.FAILURE, loginDetails.getUserId());}return authentication;}/*** Creates authentication and makes sure that auditing service is called*/private Authentication createAuthentication(AuthenticationStatus status, int userId) {Authentication authentication = new Authentication(status, userId);auditLoggingProducer.audit(authentication);return authentication;}}
Now, in order to test the code above I will use some nice features of Mockito and Spring:
package com.x.domain.service;import org.mockito.ArgumentCaptor;import org.springframework.test.util.ReflectionTestUtils;import static org.mockito.Mockito.*;import static org.junit.Assert.*;import com.x.domain.Authentication;import com.x.domain.LoginDetails;import com.x.domain.infrastructure.messaging.AuditLoggingProducer;public class AuthenticationServiceTest {private ArgumentCaptor<Athentication> argument = ArgumentCaptor.forClass(Authentication.class);private AuthenticationServiceImpl authenticationServiceImpl;private AuditLoggerProducer auditLoggerProducer;@Beforepublic void setUp() {// Just create a simple instanceauthenticationServiceImpl = new AuthenticationServiceImpl();// Now wire it up with a mock (to prevent null pointer exception)auditLoggingProducer = mock(AuditLoggingProducer.class);ReflectionTestUtils.setField(authenticationServiceImpl, "auditLoggingProducer", auditLoggingProducer);}@Testpublic loginInWithValidUserShouldCreateSuccess() {LoginDetails loginDetails = new LoginDetails(1, ...);// Login with valid userAuthentication authentication = authenticationServiceImpl.login(loginDetails);// Assert that the returned authentication is of correct statusassertEquals(AuthenticationStatus.SUCCESS, authentication.getStatus());// Here comes the tricky bit to test:// Make sure that audit logging has been called (1 time), but// also make sure that the sent message is of status successverify(auditLoggingProducer, times(1)).audit(argument.capture());assertEquals(AuthenticationStatus.SUCCESS, argument.getValue().getStatus());}@Testpublic loginInWithInvalidUserShouldCreateFailure() {LoginDetails loginDetails = new LoginDetails(123, ...);// Login with valid userAuthentication authentication = authenticationServiceImpl.login(loginDetails);// Assert that the returned authentication is of correct statusassertEquals(AuthenticationStatus.FAILURE, authentication.getStatus());// Here comes the tricky bit to test:// Make sure that audit logging has been called (1 time), but// also make sure that the sent message is of status failureverify(auditLoggingProducer, times(1)).audit(argument.capture());assertEquals(AuthenticationStatus.FAILURE, argument.getValue().getStatus());}}
By utilizing the tools Mockito brings you can isolate you unit tests 100% (or close to) and therefore do proper unit testing!
Best of luck.
Labels:
Mockito,
Spring,
unit testing
March 13, 2010
QCon 2010 London - Not Only SQL, Emil Eifrém, Neo4J
Here are my notes from the "Not Only SQL: Alternative Data Persistence and Neo4J"-session from Emil Eifrém (@emileifrem) at QCon London 2010. Please note that these are my notes and I might have misinterpreted some parts of the session, hence, read at own risk :-)
4 trends during 2009 that lead up to NOSQL
- #1 data set size: more data will be created during 2010 than all previous years together
- #2 connectedness: blogs, wikis, tagging, ontologies, …
- #3 semi-strucuture
- #4 architecture
The meaning of the term NOSQL is:
- not "Never SQL"
- not "No To SQL"
- is "Not Only SQL"
4 Emerging Categories of NOSQL
- key-value stores (based on Amazon's Dynamo Paper). Example Programmes: Dynomyte, Voldemort
- big table clones (based on Google's Big Table Paper). Example Programmes: HBase, Hypertable, Cassandra (which is used by e.g. Twitter)
- document databases (collection of key-value pairs, inspired by Lotus Notes!). Example Programmes: CouchDB, MongoDB
- graph databases (nodes, relations, key-value pairs). Example Programmes: Sones, Neo4J, AllegroGraph
Grap Database Neo4J
- disk based
- JTA/transactional
- whiteboard friendly: model your datamodel on a whiteboard and the implementation will look about the same!
- scales up
- robust
Go check it out here Neo4J. I know I will!
QCon 2010 London - Sharpening the Tools, Dan North, DRW
Here are my notes from the "Sharpening the Tools"-session from Dan North at QCon London 2010. Please note that these are my notes and I might have misinterpreted some parts of the session, hence, read at own risk :-)
I would also like to point out that if you have the chance to go to any QCon conference you should! They are the best.
We all follow the Dreyfus Model - 5 levels of skill acquisition (for everything: crawl, walk, c++ programming)
- Novice : follow all rules
- Advanced Beginner: breaking rules, testing boundaries
- Competent: goal oriented
- Proficient: intuition (thoughts starting with "I"), analogies/war stories/patterns usage
- Expert: operating of instinct (knowing the right decision)
Remember this model and analyze where you are in it for every task. This way you can ask the right questions and get the right help.
Tools used by Dan North to become a better software developer (remember the 5 levels)
Technical Tools
- languages/libraries
- IDEs, OS
- SCM, build tools
Programming Tools
- refactoring
- patterns
Modeling Tools
- UML (should only stay on the white board - or else someone is trying to sell you something :-))
- DDD
Methodology Tools
- XP, SCRUM, BDD
- Lean, Kanban, ToC
Personal Tools
- ToDo-lists, GTD, Pomodoro (write down all "subconscious" thoughts/wishes on a piece of paper during the session to keep focus)
- GROW framework
- NLP (chunking)
Group Tools
- Six Thinking Hats (used by e.g. NASA)
- Stand-Ups
- Retrospectives
Some tips for how to become an expert software developer.
Tip #1: Practice the basics
- practice Katas (auth. but you need a sensei to learn from)
- code 4 fun (dojos) - solve problems in different manners (no params, only void methods, …)
- learn a language you don't need in work (bridging languages/concepts)
- learn IDE shortcuts!
- practice your "shell-fu"
Tip #2: Learn from other people
- stalk experts :-)
- listen to novices (the beginners mind)
- read and give books journeys
- read code
- follow people (twitter, rss, …)
- programme in pairs
Tip #3: Understand trends
- build up a network that you listen to
- "Do you know what you don't know?" - use trends to find out what's lurking around the corner
Tip #4: Share knowledge
- Blog about success (and failure)
- Join mailing lists (and help answering questions)
- Use learning lunches, muffin mornings, etc. Keep these simple (no PPT - only white board)
- speak @ conferences - but don't become a conference speaker!
Tip #5: Maintain your toolbox
- some tools are timeless, others disposable
- use the "clothing" rule to update your tools
- ivy instead of maven :-)
Tip #6: Learn how to learn
- check out the book: Pragmatic thinking and learning
- understand second-order incompetence
- start using "six thinking hats"
- practice chunking: "what for?", "how?", "how else?"
- eat your own dog food
- listen like you don't know the answer
Conclusions
- always assume you're out of date
- you owe it to yourself to keep current
- learning is a continual cycle
I would like to send out a big thank you to Dan North for this highly entertaining and interesting talk - Henrik Engstrom
Labels:
Dan North,
London,
programming,
QCon,
tools
January 26, 2010
Drawing on top of a FLV stream - not as straight forward as you may think
I have spent the last 3 hours trying to make my Flex application draw images on top of a media stream (FLV). It turns out not being as straight forward anymore (since Flash 9 and the new security model).
After plenty of Googling and recompilation I found, what turns out to be, a very simple solution.
To spare you a couple of hours extra work I think it is a nice gesture to share my findings :-)
1. Add the following row in the Flash client:
Security.loadPolicyFile("http:///crossdomain.xml");
2. Add a crossdomain.xml file in the ROOT of your domain, e.g.:
<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"><cross-domain-policy><allow-access-from domain="*" /><site-control permitted-cross-domain-policies="all" /></cross-domain-policy>
Simple enough, isn't it? Well, as long as you put your crossdomain.xml file in the root domain it is... I struggled with understanding this until I found this thread. In the middle of the discussions there a guy writes that it is important to put the file in the root of your domain. Thanks dude! That saved a couple of more hours of work for me.
December 28, 2009
The one book you should buy and learn by heart if you are a professional developer is...
Lean Software Development - An Agile Toolkit by Mary and Tom Poppendieck. I have had this book lying around on my desk for maybe 2 years without reading it. Last weekend I didn't have anything to read so by coincidence I picked up this book and got started. It is a page turner filled with common sense. Though it is common sense that can be repeated over and over again because it contains some tools you need to success with your software development projects.
I have worked as consultant for 11+ years now and I nod my head at every statement in this book recalling similar situations I have experienced first hand at different clients.
The book is divided into 7 chapters devoted to 7 lean principles and thinking tools for translating each principle into agile practices:
- Eliminate Waste
- Amplify Learning
- Decide as Late as Possible
- Deliver as Fast as Possible
- Empower the Team
- Build Integrity In
- See the Whole
Yes, these principles are common sense, I know, however, none of the projects I have been involved in has managed to adhere to these "simple" principles. I just wish this book could be mandatory reading for all professional developers out there. It would simplify life for us (developers) and our customers.
Abstract methods with ActionScript
The other day I wanted to use the Template Method pattern with ActionScript. I suddenly realized, to my astonishment I must admit, that there are no abstract methods in AS. But of course there is a solution for it. Not the greatest one, but still...
First the abstract class:
package se.x.y {public class AbstractSpecialCanvas extends Canvas {public AbstractSpecialCanvas(name:String) {_name = name;init();}public function init() : void {// Setup of some graphical componentsvar createButton:Button = new Button();createButton.label = "Create Legend";createButton.setStyle("fontSize", 16);createButton.addEventListener(MouseEvent.CLICK, submitForm);// ...}public function submitForm(event:MouseEvent) : void { }}}
Notice the empty function called submitForm in the code snippet above. This is the trick, i.e. to declare an empty method that can be referenced from the class.
And now the implementing sub class:
package se.x.y {public class MySpecialCanvas extends Canvas {public MySpecialCanvas(name:String) {super(name);init();}public function init() : void {// Setup of specific graphical components}public override function submitForm(event:MouseEvent) : void {// handle specific data gathering and submit the stuff to the server}}}
Obviously you will have to remember to override the method in the super class. One way to do this is to have the method in the super class print an error message or similar. This way you would catch the mistake of not remembering early.
Labels:
abstract method,
actionscript
Scrum vs Kanban
Scrum vs Kanban. Interested in the difference and similarities? I can highly recommend the book Kanban and Scrum - making the most of both written by Henrik Kniberg and Mattias Skarin. It's great reading and should be read by everyone who consider themselves professional developers.
Subscribe to:
Posts (Atom)

