Fork me on GitHub
0

Micro Framework for using HTML 5 local / sessionStorage

 

Hey there,

lately, I’ve been using the HTML 5 local and sessionStorage which is a great thing. However, one thing which annoyed me was that it’s not possible to store objects into a storage.

Also I wanted a better way to prevent errors (like the’s no value for a key, or even localStorage is not defined due to an outdated browser)

So I came up with a little “Framework” that does nothing more than provides a simple api for both session and localStorage.
The good thing is, that it takes care of any object, that you want to store into your storage. It simply transform it into a jSon String. If you read this string later, it will be parsed as an object.

Also there’s a meaningless Exception that will occure if you try to use sessionStorage while in localMode (file://). In this case, I’ll throw out a better Exception so that one understands what the problem is ;-)

You can download it here.

Post to Twitter

0

How to insert data into a (SQLite) database on Android (the fast way)

Have you ever tried to add a lot of data (and I really mean a lot, in my case 2600 rows of data) into the Android-internal database (SQLite)?

If so, you propably went the same road as I did.

I tried a normal InsertStatement which was way to slow (10 sec. in my emulator). Then I tried PreparedStatements. The time was better but still unacceptable. (6 sec.). After some frustrating hours of writing code and then throwing it away, I finally found a good solution.

The solution was Android’s InsertHelper (320 ms). (Imagine some angels singing right now ;) )

What is an InsertHelper?

The Android-OS provide the InsertHelper as a fast way to do bulk inserts.

To give you an overview of the performance (measured with an emulator on a crap computer, trying to insert 2600 rows of data):

  • Insert-Statement
    10 seconds
  • Prepared-Statements
    6 seconds
  • InsertHelper
    320 ms

And here it is:

 public void fillDatabase(HashMap<String, int[]> localData){
		InsertHelper ih = new InsertHelper(this.db, TABLE_NAME);
		Iterator<Entry<String, int[]>> it = localData.entrySet().iterator();
		final int firstExampleColumn = ih.getColumnIndex("firstExampleColumn");
		final int secondExampleColumn = ih.getColumnIndex("secondExampleColumn");
		final int thirdExampleColumn = ih.getColumnIndex("thirdExampleColumn");
		try{
				while(it.hasNext()){
						Entry<String, int[]> entry = it.next();
						int[] values = entry.getValue();
						ih.prepareForInsert();
						ih.bind(firstExampleColumn, entry.getKey());
						ih.bind(secondExampleColumn, values[0]);
						ih.bind(thirdExampleColumn, values[1]);
						ih.execute();
				}
		}catch(Exception e){e.printStackTrace();}
				finally{
						if(ih!=null)
								ih.close();
				}
		}

The main differences to a “normal” insert are the binding to a specific column as well as the prepareForInsert() call.

Essentially an InsertHelper is nothing more than a wrapper around a compiled statement which can be found here.
However, most people find it easier to work with InsertHelper.

But… there is one more thing ;)

You can speed up the insertion even more with temporarily disable thread locks. This will gain about 30 % more performance. However it’s important to be sure that only one thread per time is using the database while inserting data due to it’s not threadsafe anymore.
@see here

 public void fillDatabase(HashMap<String, int[]> localData){
		//The InsertHelper needs to have the db instance + the name of the table where you want to add the data
		InsertHelper ih = new InsertHelper(this.db, TABLE_NAME);
		Iterator<Entry<String, int[]>> it = localData.entrySet().iterator();
		final int firstExampleColumn = ih.getColumnIndex("firstExampleColumn");
		final int secondExampleColumn = ih.getColumnIndex("secondExampleColumn");
		final int thirdExampleColumn = ih.getColumnIndex("thirdExampleColumn");
		try{
		this.db.setLockingEnabled(false);
				while(it.hasNext()){
						Entry<String, int[]> entry = it.next();
						int[] values = entry.getValue();
						ih.prepareForInsert();
						ih.bind(firstExampleColumn, entry.getKey());
						ih.bind(secondExampleColumn, values[0]);
						ih.bind(thirdExampleColumn, values[1]);
						ih.execute();
				}
		}catch(Exception e){e.printStackTrace();}
				finally{
						if(ih!=null)
								ih.close();
						this.db.setLockingEnabled(true);
				}
		}

Post to Twitter

0

It’s all about experience

 

This time, it’s about communication.. and why it sometimes fool you.

Have your ever had a conversation and wonder why the person you talk to doesn’t react/act like you expected? Have you ever said something which led to a misunderstanding? Than go on reading.. :)

We are unique. Every single person. Why are we unique? Not only because we look different. Not because I am german but you are italian. It’s because of the different experiences we’ve made.
The day, we were born, we’ve made our first experience. We then learned more and more until today.

Imagine the following situation:
You are at a rock concert, It’s pretty full and the crowd is going crazy. Someone accidently smacks his arm into your face and breaks your nose. You’ll have to leave the concert and go to a hospital. What is your experience? Next time, when there’s a rock concert you would think twice before going there.

Now, let’s say I am a huge Metallica fan and I want you to join me going to one of their concerts. I know you like rock music. I know you like Metallica. So I ask you “Would you like to join me”. You the say: “Me?? Never, no way!” I am more than dissappointed and I’ll never ask you again.

What happened?
Because you’ve made other experiences than me, you’ll probably take other decisions than me. The situation above leads to a misunderstanding. I didn’t know about the accident so I propably won’t understand your reaction. I expected you to be excited. You belie my expectations.

This situations happen in everybodys live a few times a day. Just think about:
Programmer 1 says: “Ruby is awesome”.
Programmer 2 says: “Ruby? The worst language I know”.
Programmer 1 feels angry and replies: “What are you talking about? Ruby is…”(blabla)

Because Programmer 1 don’t like Ruby (maybe he tried it and failed), he says something which Programmer 2 couldn’t understand. He was misunderstood. What Programmer 1 wanted to say was: “I tried it, and I THINK that it sucks (which doesn’t mean the language itself is bad)”
Programmer 2 understood: You are stupid, your decision is stupid.

How can we avoid those problems
We are all function like a standard input output machine. The experience is a big database, where we get our false-true-false logic from. But we can avoid misunderstandings by putting a “filter” in front of your output. Next time, you have a discussion with someone, try to carefully watch yourself. As soon as something your “partner” said felt wrong, or you started feeling angry, try to think before speak. What do you want to say and what do you think your discussion partner will understand. Could this lead to a misunderstanding? If yes, avoid saying what you want to say or even better, think about why this could lead to a misunderstanding and find another way.

Post to Twitter

0

What would you do

Let’s imagine the following situation…

You are a software architect in a medium-sized company. Your team consists of 6 prof. developers. You’ve developed a nice JEE Application, but your boss isn’t happy about the software quality. Sometimes, customers reports performance issues or erros.
So you take a look at the source and notice: Not enough tests… Why? You ask the main developers. They look at each others, rolling their eyes and say: I am a developer, why should I write tests. Also I don’t know any good unit-test frameworks..

So, you have 2 problems. On the one hand, you have developers who just don’t understand, why tests are important and why it is part of developing software and on the other hand you actually need to have a good framwork to work with…

You have to take 2 decisions. Which framework.. and more difficult, how can I convince my team writing tests isn’t bad stuff…?

Sometimes, you feel angry. Or lonely. Why me, why do I have to take this decision.

The ugly truth is, it’s your job. No matter if you are a software architect, a developer or team leader. Sometimes it’s you who have to take decisions.
Why do we all often feel a little bit doubtful, if we decide to take a specific framework for example?.. Because this decision comes with a responsibility. So it’s easy to go say “Hello” to your boss and ask him: “Please give me some advice, what should I do”. But think about it. What do you expect? Do you expect him to take a decision for you? Do you expect him to know more about the framework than you do? He won’t.

He rather would ask you “What would you do?”. — Feels wrong? What the fu**** hell, I asked him to give me some advice, so why the hell does he asked me such a stupid question? Isn’t it obvious that I am unsure what to do…. ..PING.. You noticed something?

You are unsure.. Could it be that you thought you know the problem (the framework, the people themself) better than you actually do? Are there remaining questions? Technical questions? If you only answere one question with “YES”, you should go back to the start, take your time and learn about the specific problem until you are sure what is the best way, to solve it.

If a programmer doesn’t understand, why writing tests is important but you want him to write tests, speak with him. Try to understand, where the problem is. Maybe there really is a problem. Maybe it is just because there’s no known framework.

If you have to take a decision, be sure, you know about the problem. Also be sure, you understand the framework if you have to choose one. If you are convinced your decision is right, then have no doubt. Be passionate, so your teammember will be “infected”. If all goes wrong, you at least learned something which you could take as a base for better decisions for your next problem (which will come sooner or later). Don’t hesitate to take decisions. They are one of the main reasons why we learn. And isn’t it all about learning?

This post was inspired by Roy Osherove and his talk at QCon 2011…

Roy’s talk is finally online. I strongly recommend to watch this video!
Find it here

Post to Twitter

0

QCon 2011

http://qconlondon.com

The Qcon 2011 was based in London. From the 9th of March ’till the 11th, me and my work collegue Daniel Temme had the chance to meet awesome people from all over the world. The QCon is well known for it’s high standards and unique talks. It was a pleasure to meet people like Simon Brown, Michael Mahemoff, James Pearce or Peter Pilgrim.

The first day

The first day started with the keynote “Management and Organizational-design Consultant” by Craig Larman. He’s a very passionate speaker and gave me some great new ideas about what Scrum is essentially about. He talked about what an Architect is and what his role means for the team. Very interesting new perspective about what an Architect should and shouldn’t do..

Further Talks:
Dan North – From months to minutes
Adrian Kosmaczewski – Introducing in IOS Software Development
Lars Hesel Christensen – Do’s and dont’s in Android
Dan North – Chris Reed – Agile Operations – optimising the business one shell script at a time
Eoin Woods – Where did my architecture go?

Lars Hesel Christensen was a non experienced speaker, but non the less it was an interesting resume about good and bad style while using the Android framework.

See: Wednesday

 

The second day

The second day started with the keynote “Things I wish I would have known” by Spingsource founder Rod Johnson. He talked about how Springsource became a well known company and why today Spring is a framework, which every developer at least should know. He gave the audience tips how one can start his own business. Mainly it’s about having a dream, having passion and at least one should be willing to spend more time in business than in any other hobby. It’s still possible to be successful. But it will be pain…

Further Talks:
Tom Paquin – OnLive Launch Experience
Nick Callen – Data architecture at Twitter scale
Roy Osherove – Team Leadership in the Ager of Agile
Ian Robinson – Getting things done with Rest
Jason Sobel – Infrastructure at Facebook

I was deeply impressed from Roy Osherove’s talk. It was enlightening how he took situations, which occurs daily at work and described, how a leader can do better. Amazing, how many mistakes one can make while (trying) to communicate to others. After hearing him speak, I was aware why nearly no one could follow my orders correctly ;)

See: Thursday

 

Third day

The third and final day started with the Keynote “Innovation at Google” by Patrick Copeland. It was amazing to see, how he tries to turn problems around till he knows every detail of it. He explained, how he creates “prototypes” of “innovations” which means sometimes nothing more than taking a piece of wood and treat it like it’s a phone for example. The piece of wood must have the same dimension and the same weight. With this little piece, he tries to get a feeling whether this idea could work, or not. If not, it’s like “Google Wave” which was turned down after almost one year.. It’s all about trying things. The customer is not stupid and if he/she doesn’t want something, the vendor will know it. Sooner or later… So be the first, release what is possible to be released, or your competitor will do so.

Further Talks:

Michael Mahemoff – Single Page Apps
Kevlin Henney – Is it possible to do OO Programming in Java
David Recordon – HTML 5 @ Facebook
Glenn Vanderburg – Clojure and the Web
James Pearce – HTML 5 and the dawn of rich mobile internet applications

Michael Mahemoff, a very cool australian guy working for Google was talking about how single page Websites can create a whole new user-expercience.
It was also interesting to see, what HTML 5 means for Facebook.

See: Friday

 

In addition to the conference, we had the chance to network with some of “the big guys” of the industry, which maybe sooner or later could be a huge benefit. Who knows…

Anyway, thank you QCon, see you next year!

Post to Twitter