Fork us on GitHub

New Default Code

Generating better hello world applications
New Default Code

New Default Code

This is new behavior that went in without fanfare. If you created a new hello world app you might have noticed this. We changed the default boilerplate for Codename One and made it more representative of what you’d want to see in a hello world app.

The entire change to the default generated app is within the init method:

public void init(Object context) {
    // use two network threads instead of one
    updateNetworkThreadCount(2); (1)

    theme = UIManager.initFirstTheme("/theme");

    // Enable Toolbar on all Forms by default

    // Pro only feature

    addNetworkErrorListener(err -> { (2)
        // prevent the event from propagating
        if(err.getError() != null) {
        Log.sendLogAsync();"Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
1 By default networking in Codename One runs on one network thread for consistency. Two threads make more sense from a performance standpoint. We wanted to make this easily configurable and it should be easy in the init() method
2 Handling network errors in a generic way is probably one of the hardest things to grasp. So many developers are still stuck with the default error handling code…​ With that in mind we added a bit of boilerplate to handle network errors in a way that makes sense and should be easily customizable

I had some thoughts about removing this boilerplate and packaging it in a class. But then I thought again.

I Like Boilerplate

  • It’s simple

  • It’s obvious

  • You can instantly find what you want

This isn’t code for the sake of code or declaration. The code is still concise and does what we expect. This is one of those cases where boilerplate makes more sense.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.