2013-03-02

PMD-Eclipse Rules Configuration

When I first installed the PMD Eclipse plugin and ran a code check on my codebase, PMD displayed no errors. The first thought was, "My code is perfect!". On second thought, PMD was not configured properly.

Unlike FindBug or CheckStyle, by default PMD is not configured to check for any coding violation rules. (At least this is true on my Mac Eclipse Juno.). You have to go to Eclipse Preferences -> PMD -> Rule Configuration and check the rules you want to use.

2013-02-22

Eliminate JavaScript global variables

Global variables in JavaScript can be overwritten, resulting in bugs. For example,

<html><head>
<script>
var counter = 0;

function increaseCounter() {
    ++counter;
    alert("Counter is " + counter);
}
</script>

<script>
var counter="A";
</script>
</head><body><form>
    <input type="button" value="Increase Counter" onclick="increaseCounter();" />
</form></body></html>

will output "Counter is NaN" when the button is clicked.

Global variables in JavaScript can be eliminated by closures. For example the previous JavaScript can be refactored as,

<html><head>
<script>
var CounterJS = (function() {
    var counter = 0;

    return {
        increaseCounter: function() {
            ++counter;
            alert("Counter is " + counter);
        }
    }    
}());
</script>

<script>
var counter="A";
</script>
</head><body><form>
    <input type="button" value="Increase Counter" onclick="CounterJS.increaseCounter();" />
</form></body></html>

2013-02-21

Initialize Map

Sometimes code is more clear by initializing a Map at its declaration. For example,

Map<String,String> map = new HashMap<String,String>() {{
    put("Key 1", "Value 1");
    put("Key 2", "Value 2");
}};

Behind the Java scenes, this is a combination of an anonymous inner class (the first outer set of brackets) and an instance initializer block (the second inner set of brackets).

Landscape mode in Android emulator

To change to landscape mode in Android emulator, press the left ctrl+F11 buttons. For Mac, use fn+ctrl+F11.

2013-02-20

15 minutes of vim

  • How do I start using vim?
    If you have a text file called MyDoc.txt, enter the following command in the terminal prompt:

    vim MyDoc.txt

  • How do I start inserting text?
    Press the "esc" button and then press the "i" button. Start typing your text.

  • How do I move the cursor around?
    Use the arrow keys.

  • How do I quit vi?
    Press the "esc" button and then type :q

  • How do I quit vi without saving the file?
    Press the "esc" button and then type :q!

  • How do I save a file?
    Press the "esc" button and then type :w

  • How do I undo my changes?
    Press the "esc" button and then press the "u" button.

  • How do I redo my changes?
    Press the "esc" button. Then hold the "control" button down while pressing the "r" button (Ctrl+r).

  • How do I delete text?
    Press the "esc" button and then press the "x" button. Press the "x" button again to delete the next letter.

  • How do I search?
    If you are searching for the text "findme", press the "esc" button and then type /findme

  • How do I find next after search?
    Press the "esc" button and then press the "n" button.

  • How do I replace?
    If you want to replace the text "findme" with "replacedme", first move to the line with the text "findme". Press the "esc" button then type :s/findme/replacedme/

  • How do I replace all?
    Press the "esc" button and type type :1,$s/findme/replacedme/g
    1,$ means "From line 1 to the end of the file" (I remember $ with the mnemonic "cash out last")
    s means substitute
    g means globally
    "From line 1 to the end of file, substitute findme with replacedme globally"

  • Where can I learn more about vim without being overwhelmed?
     Vi Editor Reference Manual