Having a Mac

I normally use Windows and once in a while Ubuntu. I'm one of the rare persons who love Windows 8. It is by far the best version. Mainly because I'm a power user. I never use the tiled start screen. It's like on a Mac. You should never use the application bar. You should use Spotlight. On Windows, you should use Windows+Q and Windows+W to find settings and applications. As soon as you've learned there two shortcuts, life is good.

Back to the Mac. These days, Windows constructors are a selling bad hardware. SSDs are rare, autonomy is bad, screen is ok (not wow), etc etc... Macs are strangely the best deal you can get. For instance, a Mac Book Air is the cheapest computer on which to run Windows efficiently. Doh.

So I ended up with a Mac and decided that before formatting it, I would try OS X (Maverick) just in case. Some stuff is good. It's really fluid and it wakes up instantaneously for instance.

However, the guy who did the windowing should be exchanged with an hostage of IS. That should slow them down. I will spare him if he agrees to implement the following suggestions:

  • Cmd+Tab should bring to front the requested window. Always. It doesn't matter is the window is minimized, in background or whatever. Just bring it in front
  • Cmd+Tab should allow to select between the multiple windows of an application. Or at least any other shortcut. Being forced to go to the Window menu is not a good option
  • Home should always go to the beginning of the line. End to the end. There's no point in randomly changing this behavior depending on the application
  • Also, Cmd+Left or Fn+Left should always go home. Cmd+Right or Fn+Left should go at the end. Pick one, stick with it and it should work everywhere
  • I should be able to lock my workstation immediately but also to have it locked automatically in 15 minutes. Those are two different needs
  • Why having virtual desktop for only maximized windows? Why can't I create as many desktops as I want and put whatever I want on them?
  • Being able to dock windows on the left or right natively would be nice. I currently use BetterTouchTool for that
  • We should really be able to hibernate for real. Even is it means that the computer will wake up more slowly in that case. Nothing is worst than leaving the computer to rest for the night and to discover in the morning that the battery ran out magically
  • The Windows ca-fr keyboard was perfect. Why moving the brackets and square brackets to somewhere I can't reach them? And don't get me started on the azerty on mac. It's a great feat to turn the worst keyboard on Earth into something even worse... 
That would be a good start. I've also replaced the shell but I will talk about that another time.

New EasyMock website is out!

After years having the most vintage open source website out there, the EasyMock team is proud to announce that the new website is up and running.

Please tell us what you think. Directly on the pages using Disqus, in comment of this blog post, by twitter (@henri_tremblay) or in the mailing list.

Next steps:
  • Be able to deliver more quickly
  • Deliver EasyMock 3.3 ASAP!
Meanwhile, enjoy the website.

Université de la performance

I just published on Slideshare the slide from the Université de la Performance done at Devoxx France 2014.

Obviously it’s in French and much less interesting without the demos. So the video is available on YouTube.

Collisions on a Switch on Strings

Today, I felt on a really nice blog post about how a switch is implemented for String under the hood.

It basically explains that the switch is implemented on the String hash code. Easy and efficient. But it made me wondering what would happen if the was a collision in the hash codes. By that I mean: What happen if you got two cases containing strings having the same hash code?

To test it, I've created such a switch. First I was needing to found two strings with the same hash code. The way the hash code of calculate for a string is quite simple. It loops over the characters, adding their value to the previous result multiplied by 31. So, doing a bit of algebra, the two characters strings below will be in collision.


int c1 = some_character, c2 = some_other_character;
int c3 = c1-1, c4 = 31*c1+c2-31*c3;

String s1 = new String(new char[] {(char)c1, (char)c2});
String s2 = new String(new char[] {(char)c3, (char)c4});
From where I just had to create a switch with the result. For instance "10" and "0O" both have a hash code of 1567.


switch (args[0]) {
case "10":
    System.out.println("Here");
    break;
case "0O":
    System.out.println("There");
    break;
case "123":
    System.out.println("Away");
    break;
}

Decompiling this code brings a surprising result.


String str1 = args[0];int i = -1;
switch (str1.hashCode())
{
case 1567:
  if (str1.equals("0O")) {
    i = 1;
  } else if (str1.equals("10")) {
    i = 0;
  }
  break;
case 48690:
  if (str1.equals("123")) {
    i = 2;
  }
  break;
}
switch (i)
{
case 0:
  System.out.println("Here");
  break;
case 1:
  System.out.println("There");
  break;
case 2:
  System.out.println("Away");
}

So, in case of collision, the compiler changes the strategy and add a redirection. It is worthy to mention that the redirection applies even to cases that are not in collision (like "123" in the example).

So, unless the JVM manage to optimize this at runtime (which is highly possible), if we are unlucky enough to have a collision in your switch, you code will be slower than if there wasn't any.

New file shortcut

There is no way to create a new file in Windows explorer using a shortcut. You can Ctrl+Shift+N to create a new folder but strangely nothing to create a file.

The solution is to use AutoHotKey and a script. This one works on Windows 8. It was adapted from here.

#IfWinActive ahk_class CabinetWClass

; Create new file in Explorer
^N::
NewTextFile()
return

#IfWinActive

NewTextFile()
{
WinGetText, full_path, A
StringSplit, word_array, full_path, `n
Loop, %word_array0%
{
IfInString, word_array%A_Index%, Address
{
full_path := word_array%A_Index%
break
}
}
full_path := RegExReplace(full_path, "^Address: ", "")
StringReplace, full_path, full_path, `r, , all

IfInString full_path, \
{
NoFile = 0
Loop
{
IfExist %full_path%\NewTextFile%NoFile%.txt
NoFile++
else
break
}
FileAppend, ,%full_path%\NewTextFile%NoFile%.txt
}
else
{
return
}
}

Windows hard links

As on Unix, it is also possible on Windows to create a hard link to a file. I'm using this feature to backup in Dropbox files that need to be placed in some other directory. For instance, my ssh config file.

Here's how you do it from a cmd:

mklink /h %USERPROFILE%\Dropbox\.ssh\config %USERPROFILE%\.ssh\config

Note that there is no way to visualy know that it is a hard link. You have to guess and confirm your intuition doing:

fsutil hardlink list config

Also, if you are wondering, delete one of the file doesn't delete the file. You need to delete all links to it to get rid of it.

Ctrl+V in cmd

I've been using this for a while in fact but thought it was useful enough to be told.

A really annoying thing on Windows is that you can't Ctrl+V in a cmd. You can anywhere else but not there.

A nice piece of software named AutoHotkey can solve this. It allows you to script (in a strange language sadly) hot keys anywhere in Windows.

For Ctrl+V, the following script does the trick:

#IfWinActive ahk_class ConsoleWindowClass
^V::
SendInput {Raw}%clipboard%
return
#IfWinActive

UPDATE: In fact SendIput has encoding issue. So I'm now using Send !{Space}ep which simulate an access to the system menu and then select the paste by typing 'e' and 'p'. Not that is works in English. You might have to change the letters to fit your OS language.

LiveReload

I’m not originally a web developer. But I’m doing more and more of it. I’ve discovered LiveReload some time ago and became addicted to it.

By that I mean that I’m putting it on pretty much every web page I’m working on. Getting instant feedback is great.

I’m using Grunt to launch it. So I’m now having two files that I’m copying around. They are pretty basic but could be useful so I thought I could share.

package.json

{
  "name": "livereload-gruntjs",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.2",
    "load-grunt-tasks": "~0.3.0",
    "requirejs": "~2.1.11",
    "grunt-contrib-connect": "~0.6.0",
    "grunt-contrib-watch": "~0.5.3"
  }
}

Gruntfile.js

module.exports = function(grunt) {

  require('load-grunt-tasks')(grunt);

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    connect: {
      options: {
        hostname: 'localhost',
        port: 9001,
        livereload: 35729
      },
      livereload: {
        options: {
          open: true,
          base: 'app'
        }
      }
    },

    watch: {
      livereload: {
        options: {
          livereload: '<%= connect.options.livereload %>'
        },
        files: [
          'app/{,*/}*.*'
        ]
      }
    }

  });

  grunt.registerTask('serve', [
    'connect:livereload',
    'watch'
  ]);

};

Playing with keytool

It's been a while since I played with certificate in Java. It's usually the kind of thing you do once in your project and never touch again.

So I played with the Java keystore. I wanted to do a self-signed Sha256RSA certificate. It feels a bit like coding in perl. You start by something really long like:


# Create the key with openssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -sha256 -keyout bar.key -out bar.crt

# Create a new keystore with a dummy key
keytool -genkey -alias foo -keystore keystore.jks

# Delete the dummy key
keytool -delete -alias foo -keystore keystore.jks

# Import the real key
keytool -import -alias bar -file bar.crt -keystore keystore.jks

to a happy one liner (that also prevent from having to answer a bunch of question and providing a password all the time)


# Generate a self-signed certificate and create the keystore
keytool -genkey -keystore keystore.jks -alias bar -dname "CN=octo.com, OU=, O=OCTO, L=Paris, ST=, C=FR" -storepass mypassword -keypass mypassword -keyalg RSA -keysize 2048 -validity 730

# Export the certificate (ok, yes, it's a two liner if I want to get back the certificate)
keytool -exportcert -keystore keystore.jks -alias bar -file bar.crt -storepass mypassword -keypass mypassword

Playing with numbers in JavaScript

Today I did a bit a JavaScript to test some librairies to manipulate numbers.


The first is BigDecimal. It is based on the GWT implementation of Java BigDecimal class. It works exactly the its Java counterpart. If your doing financial calculation in JavaScript, this library is a must.

Then, I tried Numeral.js. A library allowing to format numbers easily in a given locale. Easy to use and works perfectly.

The last one was Formula.js. This one is fun and useful. It re-implements all Excel functions in JavaScript. They are two drawbacks.

The first one is that you need to add a bunch of other libraries to use it. But depending on which function you are using, you might not need all of them but that's left to you to figure it out.

The second one is that it calls eval in some function parameters. Could be dangerous so beware.

My suggestion on this library is to copy & paste the code of the functions you are using to limit the required third-party libraries, tweak it a bit to remove the evals and possibly migrate it to BigDecimal ;-)