You are here: Home » Blog

Google Chrome developer tip: extended reload button

Posted on Thursday, August 14 2014 at 12:51 | Category: Software Engineering | 0 Comment(s)

When the "Developer Tools" console is open in Google Chrome, the reload button gets a drop down menu with some options which are quite useful when developing web applications:

  • Normal Reload: just reload the page, same as F5
  • Hard reload: reload the page, but do not use anything from the cache for this request
  • Empty Cache and Hard Reload: Empty the cache and reload (which is implicitly a hard reload then, since the cache is empty)

To actually open the menu, it is necessary to press and hold the reload button a short time, similar to the "back" button where chrome displays a page history then.

Also, the functionality is only available when at the same time the developer console is open for the specific page - the developer console can be opened by pressing F12.

See also What's the difference between “Normal Reload”, “Hard Reload”, and “Empty Cache and Hard Reload” in chrome? for some additional information.


git status shows the status of the workspace - like which files have been modified or which files are new. For new directories with new files in them, the default behavior is to only show the directory name - but not the contents of the directory:

$ mkdir -p newDir/subDir
$ touch newDir/newFile1 newDir/newFile2 newDir/subDir/newFile3
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       newDir/
nothing added to commit but untracked files present (use "git add" to track)

I usually prefer to also see the contents of the directories - this can be achieved by adding the parameter --untracked-files=all to git status:

$ git status --untracked-files=all
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       newDir/newFile1
#       newDir/newFile2
#       newDir/subDir/newFile3
nothing added to commit but untracked files present (use "git add" to track)

It is a littlebit cumbersome to type this parameter each time, but Git allows to define aliases for commands. Straightforward, we can try to add the following alias to ~/.gitconfig:

[alias]
        status = status --untracked-files=all

Unfortunately, this will not work - Git does not allow to redefine existing commands (see "alias.*" at git-scm.com/docs/git-config). Some commands allow to add default parameters in ~/.gitconfig, in a format similar to

[status]
        untracked-files=all

but again, for status, this does not work.

The simplest way is to just define a new command alias, like

[alias]
        statusall = status --untracked-files=all

This will not change the default behaviour of the status command (which is probably good, since scripts might rely on the default output format of the command), but it simplifies the above command line:

$ git statusall
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       newDir/newFile1
#       newDir/newFile2
#       newDir/subDir/newFile3
nothing added to commit but untracked files present (use "git add" to track)

There is one annoying thing in MS Visio when using Stereotypes in an UML class diagram: sometimes, the stereotype name is shown on the same line as the class name, where it should really be above the class name:

It is not really reproducable when this happens - sometimes adding attributes or operations fixes the layout, sometimes changing the class name breaks the layout. In any case, this article describes a solution how to fix it: 

  • Choose the text tool and select the class name in the class node.
  • Place the cursor before the class name and press RETURN.

 

This adds a line feed in front of the class name and makes the stereotype render properly above the class name:


Initializing Java collections and arrays during creation

Posted on Saturday, March 15 2014 at 21:32 | Category: Software Engineering, Java | 0 Comment(s)

Initializing collections with predefined values

Especially when writing unit tests or mock objects, it can be useful to initialize collections like Lists, Maps and Arrays with specific values. The usual way to do this is to instantiate the corresponding class and then add the values, e.g.


List<String> names = new ArrayList<>();
names.add("John");
names.add("Jack");
names.add("Greg");

However, when the collection is only required to be passed to a method as parameter, this adds a temporary reference variable to the current scope.

In another scenario, it could be necessary to create a collection with predefined values as class members, and using the approach above would require the add() calls to be done in the class constructor (or in the instance initializer). For readability and maintainability, it would be better to add the default values at the same place where the member is defined. In any case, initializing collections in this way can be done using the "double brace initialization syntax":


List<String> names = new ArrayList<String>() {{
      add("John");
      add("Jack");
      add("Greg");
   }};

Is this really a specific, probably new, Java syntax? Not really - lets rewrite it slighty:


List<String> names = new ArrayList<String>() {

   {
      add("John");
      add("Jack");
      add("Greg");
   }

};

It might be more obvious now - what we do is that we create an anonymous class as a sub class of ArrayList<String>. In that class, we create an instance initializer which is enclosed with the inner pair of braces. Syntactically this can be written as above, hence the name "double brace initializer". One particular detail to consider is that the diamond operator can not be used with anonymous classes, so we need to explicitly name the type when instantiating a generic class. See stackoverflow.com/questions/13821586/why-cant-diamond-infer-types-on-anonymous-inner-classes for more details. Using this approach also allows to pass a readily initialized collection as a parameter to a method, without the need to create a temporary variable as in our introductionary example:


   public void processMap(Map map) {
      System.err.println(map);
   }

   public static void main() {
       processMap(new HashMap<String, Integer>() {{
             put("one",  1); 
             put("two",  2); 
             put("three",  3); 
          }});
   }

Initializing raw arrays with predefined values

Another common use case is to create a raw array with predefined values. When T is the array type, then the syntax for array initialization is:


T[] array = new T[] {val1, val2, val3, ..., valN};

This creates an array with N elements, initialized with the given values. The nice thing is that this syntax can also be used to initialize an array while passing it as parameter. Consider the following method:


public void processArray(int[] array) {
   System.err.println(Arrays.toString(array));
}

Then, when we want to pass an array with the values 1, 2, 3, we can simply call the method like this:


processArray(new int[] {1, 2, 3} );

The same works also with reference type arrays:


public void processArray(Date[] array) {
   System.err.println(Arrays.toString(array));
}

processArray(new Date[] {new Date(123456789012L), new Date(234567890123L), new Date(345678901234L)} );

Displaying results 1 to 4 out of 13
<< First < Previous 1-4 5-8 9-12 13-13 Next > Last >>