Wednesday, April 29, 2015

Regular expression: you should not use dot to match any character in Java/Groovy

Recently I just realize that in if you want to match "any character" in  Java/Groovy, you should not use dot match (.).

The reason is: dot match only works for single line text, will not work for line breaks.  For the multiple lines text matching, the solution to match any character is using [\s\S].

Please refer this article for details.

For example, following code snippet is used for stripping the javascript code in the text

Source code:

Learning Scala 1: swap array elements

Here posted my solution for the exercise 2 and 3 of Chapter 3 of page 39, in the book "Scala for the impatient".

Exercise 2.  Write a loop that swaps adjacent elements of an array of integers. For example,
Array(1, 2, 3, 4, 5)  becomes Array(2, 1, 4, 3, 5) .

Exercise 3.  Repeat the preceding assignment, but produce a new array with the swapped
values. Use for /yield.

Note: I realize you can not write java style for yield statement, like if ( i%2 ==0 ) yield s(i+1)

Monday, April 27, 2015

What I learned from Agile Engineering training

Last week I took the 3-day "Agile Engineering Training" in TELUS, thanks a lot to our trainers from LeanIntuIt - Shawn Button and Declan Whelan.  I learned a lot from them.  The course covers TDD, Simple Design, SOLID principles, refactoring and dealing with legacy code.  The fun part is there are a lots of coding Kata, like Bowling Game, Gilded Rose, etc.  I am so glad to have an opportunity to learn those stuff, even actually I learned them since 10 years ago, this time I feel  this training is a kind of a review of what I learned since I decided to be a software craftsman.

Here is the mind map summarized of what I learned.

Add some thoughts:
  1. TDD/SOLID principles/Refactoring skills become more critical and in an Agile/Scrum team,  you have to be great, to be better so that you can deliver faster,  your code will be easier to change, easier to maintain;
  2. Simple Design -  it is summarized by only 2 points: fixing names and removing duplication. Combining with TDD's Red/Green/Refactoring cycle,  it means just repeating this 2 things with test cases, your complex code logic design phase will turn into many tiny steps which just renaming and removing duplication; if you can do it fluently,  the effort of thinking hard before hand becomes simple physical typing movements - which means you can design the code without thinking too much. This conclusion is so striking.
  3. Refactoring -  When I tried the Kata Gilded Rose (a simpler version which have all the tests before),  I really sense the power of simple design - refactoring by doing renaming and extract methods, then the business logic gradually emerged, and design pattern is emerged. For me it means as long as you have all the test cases, then you can refactoring the code without need to understand the business logic, and business logic will be more clear after you refactored.  That is another aha moment for me. Looks like code quality is a separate attribute beside the domain knowledge. If we apply this to code review, it means the good code should be enough understandable without understanding the business background first.

  1. Four Elements of Simple Design, by JBrain
  2. Kata: Gilded Rose, this is a great Kata about refactoring, it worth trying more times