Friday, May 31, 2013

Law of Demeter in Java and Scala

Target audience: Beginner
Estimated reading time: 10'


The Law of Demeter for methods requires that a method of an object may only invoke the methods of the following kinds of objects:
 1  The object itself: this
 2  Variables or objects which scope is the class (attributes or variable members)
 3  The method parameters (or arguments)
 4  Variables or objects local to the method
 5  A global variable or object accessible by the object

The advantage of  the Law of Demeter is that applications are easier to maintain and update because objects are less dependent on the member attributes of other objects. Such a advantage is important when using 3rd party libraries or frameworks. Design patterns such as Facade, Adapter or Proxy provide developers with similar benefits.
The main drawback of Law of Demeter is the constant need to create wrappers to isolate the internal structure of other objects adding execution time overhead. Such wrappers, commonly used in large frameworks, relies on interfaces that delegate the actual implementation of functionality to concrete classes.  Aspect programming, attempts to get around this overhead, among other things.

The law of Demeter was very popular in early 1990's when C++ gained acceptance in the software engineering community.

Use case
The following Java and Scala code snippets illustrates the programming idioms that complies and also violates the Law of Demeter. The following Java class that implements a string concatenation complies with the law regarding local, class attributes and methods.

public class StringConcatenation  {
  private String _name = null;
  public String rightUsage(final String s) {
      // Rule 1: Invoke its own method using 'this'
    if( this.isValid(s) ) {
      // Rule 2: Call its own attribute:  '_name'
      StringBuilder buf = new StringBuilder(_name); 
      // Rule 3: Call methods parameter: 's'
      buf.append(s );
      // Rule 4: Call local object : 'buf'
    return buf.toString();

The rightUsage method complies with the Law of Demeter because it is referring to objects, variables or method with either class or local scope. Let's consider the following Scala Trait, Dictionary and class, ScientificDictionary that are provided as part of a 3rd party library. The Translation class uses a specific dictionary (scientific, medical,....) in particular language (English,German..) to translate any document.

sealed trait Dictionary[Language] {  
   def translate[Language](s: String): String 
case class ScientificDictionary[Language] 
   extends Dictionary[Language] { }                
case class MedicalDictionary[Language] 
   extends Dictionary[Language] { }
case class SlangDictionary[Language] 
   extends Dictionary[Language] { }
class Translation[Language](
  var dictionary: Dictionary[Language]
)  {
  def translate[Language](s: String): String = 

The method wrongUsage below violates the Law of Demeter because there is no guarantee that the 3rd party library provider may not alter or remove a reference to Dictionary from the Translation object. There is also no guarantee that the translate method may be removed or deprecated in future releases of the library.

class StringConcatenation(_name: String) {
  def wrongUsage(
    translate: Translation[Spanish], 
    s: String
  ): String = translate._dictionary.translate(s"${_name}$s")

Some code analysis tools can be configured to enforce one or more Demeter rules. At the minimum, these rules should be part of the tool box of software development technical lead responsible for code reviews.

Law of Demeter Wikipedia

Sunday, May 19, 2013

Effective Time Management

Target audience: Beginner
Estimated reading time: 15'


Each team member to contribute to the extend of his/her capability during the work week. The two key elements of an effective time management are
  • Flexibility: Team members should be able to work multiple tasks, change roles and get around bottlenecks. 
  • Availability:  Team members should support other team members, constantly evaluate the efficient usage of their time and be accessible outside business hours if necessary. 

Effective Meetings

There are some basic no "non-sense" rules to make sure that meetings are conducted efficiently.
  • Each meeting should have a clear theme and agenda, emailed to all attendees 48 hours prior the schedule day 
  • Meeting should be restricted to one hour and start with a recap of pending action items for previous sessions and listing the issues on the agenda. 
  • The team should spend no more than 10' on each issue. In case a consensus is not reached, someone  should be assigned to task to research, investigate and propose a solution for the next meeting. 
  • It is highly recommended to deal with critical or controversial issues at the beginning of the meeting. 
  • Minutes of the meeting, including action items, deliverable and milestones should be posted within 24 hours.  

Urgent vs. Important Tasks  

The most productive tasks are (in decreasing orders)
     1. Important & Non urgent
     2. Important &Urgent
     3. Non important & Urgent
     4. Non important & Non urgent

The following table lists some activities as classified by their importance and urgency.

.UrgentNot Urgent
Important Escalations
Resolving bottlenecks
Assigning Defects
Solving Escalations
Project Planning
Technical investigation   
Unimportant    Time-sensitive interruptions    
Responding emails
Phone Calls
Unscheduled meetings
Browsing web
Social visits

The role of project manager is to
- Protect engineers from urgency & interruptions
- Define/communicate the important tasks for the week

Time Wasters   

Unscheduled interruptions are far more distracting than people. Beyond the actual time spent to address the interruption, it takes on average 15 minutes to get back to the activity in progress.
The following table lists some examples of interruptions or time wasters and possible solutions.

Wasters Solutions
Lack of clear prioritiesBetter project management
Unproductive meetingsMeetings with goal,agenda,minutes
Productivity bottlenecksFlexible organization in terms of skills,schedule
Unnecessary interruptions        Manager to control interruptions
Process inefficienciesProcess automation and productivity tools
Lack of accountabilityQuantitative metrics (K.P.I.) functionality,quality,schedule