Saturday, September 29, 2012



1.- Erlang can be compiled directly from the latest stable source. Go to the site: 

Download the latest source file, and extract to a directory of your choice. When I downloaded the file "otp_src_R15B01.tar" was the latest, but now there seems to be a newer version. 

2.- To compile the code you will need the developer command line tools, and these tools come with Xcode. if you don't have it already; download and Install Xcode from here:

Xcode is free, and can also be downloaded from the AppStore. Once you have downloaded Xcode, open it, go to preferences then downloads. There is an "Install" link to add the command line tools.

Once this has been completed, make sure to re-open your terminal window.

3.- Open terminal and go to the directory where you extracted the source code and run:

./configure
make
sudo make install

4.- To verify installation just open the terminal and type erl. You should see something like this:  

Nicolass-MacBook-Pro:~ nicolaslarrea$ erl
Erlang R15B01 (erts-5.9.1) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.1 (abort with ^G)
1>

5.- A lot of Erlang developers use Emacs, and if your using a Mac computer, then Acuamacs is the best choice. To Install Acuamacs go to: 

6.-To configure Acuamacs for Erlang you will need to create the folder “Aquamacs Emacs” in the following path

/Library/Preferences/Aquamacs Emacs

Then create the file Preferences.el specifying the paths where Erlang resides: 

;; Erlang mode
(setq load-path (cons"/usr/local/lib/erlang/lib/tools-2.6.7/emacs" load-path))
(setq erlang-root-dir "/usr/local/lib/erlang")
(setq exec-path (cons "/usr/local/lib/erlang/bin" exec-path))
(require 'erlang-start)

These paths are specific to the version of Erlang that I installed (R15B01), for different versions, double check the paths are correct. 

Finally, save the file in: /Library/Preferences/Aquamacs Emacs/ Preferences.el  

8.-Open Acuamacs and create a hello.erl

-module(hello).
-export([hello_world/0]).

hello_world() -> io:fwrite("hello world!\n").

An Erlang menu should be created right beside the Tools menu, and the syntax coloring should be activated.

9.- To compile the code go to Erlang -> Compile -> Compile Buffer, or press Ctrl-C + Ctrl-K.

10.- Finally, type in the shell:

hello:hello_world().

It should look something like this:


Friday, December 9, 2011

Coding null resilient methods using AOP, Java and AspectJ.

 

The last couple of months, I’ve been learning Aspect Oriented Programming, so I decided to write a short post about it. AOP helps a developer separate concerns, in a clean and simple way. No matter how good of a programmer you are, you will always find situations where certain concerns crosscut each other; For example: any method that uses “try/catch”. The developer ends up to tangling the business logic of the method with the error handling code. AOP helps separate these concerns. There are a lot of examples of AOP on logging, security, transactions, etc. For this post we’ll see how we can avoid invoking a method on a null object.

Whenever we use methods with arguments (objects), the developer should worry about checking if the passed object is null. Otherwise, invoking a method using a null object, will cause a runtime exception and crash your program. Making this null check validation has nothing to do with the business logic of the function. Moreover, the validation has to be repeated for every method we code. In addition, sometimes as we add arguments to a function we forget to update the null checks. Wouldn’t it be nice if we can be sure that we can call any method without having to check for nullability? Well it turns out that with aspects, this is rather simple, and even better we only have to code it once.

 

Suppose we have the class:

 

 

package MyTest;

public class MyConsolePrint {

       public void printStringRep(Object obj)

      {

          System.out.println(obj.toString());

      }

      public void printStringHashCode(Object obj)

      {

           System.out.println(obj.hashCode());

      }

      public void printAreEqual(Object obj1, Object obj2)

      {

            System.out.println(obj1.equals(obj2));

      }

}

 

If we pass a null object to any of the methods in this class, we are going to have problems. We’ll see how we can fix this without altering the class. To Test the application this is the code we’ll use.

 

import java.util.*;

import MyTest.MyConsolePrint;

 

public class Main {

    

       public static void main(String[] args) {

             

             MyConsolePrint console = new MyConsolePrint();

             

             ArrayList items = new ArrayList();

             items.add("hello");

             items.add(null);

             items.add(2);

             

             for(Object x: items)

             {

                   console.printStringRep(x);

                   console.printStringHashCode(x);

                   console.printAreEqual("hello", x);

             }

       }

}

 

Now we run the program and BOOM!

 

hello

99162322

hello == hello is true

Exception in thread "main" java.lang.NullPointerException

      at MyTest.MyConsolePrint.printStringRep(MyConsolePrint.java:8)

      at Main.main(Main.java:19)

 

 

The programs breaks when null is sent. However, this is expected. To fix this we can add null checks to our methods in MyConsolePrint. But imagine we have many of these cases. The amount of work would be huge. We’ll see how with one aspect we can intercept all method calls in the package/namespace, and avoid throwing an exception. Here is the Aspect:

 

package MyTest;

 

public aspect NullArgumentSafeAspect {

    

pointcut allMethodCallsWithinMyNamespace() :  

                                           execution(void *.*(..))//get all method calls

                                &&

                                           within(MyTest.*);//within my test namespace)

    

 

void around() : allMethodCallsWithinMyNamespace() {

            //before calling method

            for(Object arg: thisJoinPoint.getArgs())

            {

                  if(arg == null)

                  {

                       System.out.println("null arguments " +

                             "not safe to continue. Abort!");

                       return;//cancel the method call.

                   }

            }

            proceed();//call the method.

      }

}

 

The “pointcut allMethodCallsWithinMyNamespace() groups all method invocations within the MyTest namespace. The “advice” “void around ..” binds the implementation of our validation, to the “pointcut”. The invocation of the method is cancelled if null arguments are sent. This single Aspect works for all void methods with any type of arguments within the namespace of interest.

To conclude, using Aspect Oriented Programming we were able to separate the “null argument check” concern from the logic of the method. The best part is that we did this without altering the original class, and all the code is localized within the Aspect.

 

 

And here is the exception free output  : )

 

hello

99162322

hello == hello is true

null arguments not safe to continue. Abort!

null arguments not safe to continue. Abort!

null arguments not safe to continue. Abort!

2

2

hello == 2 is false

Saturday, May 21, 2011

Objective-C Memory Management.

 

I started learning Objective-C because I wanted to program iPhone/iPad applications. I heard that the language had garbage collection, but I was disappointed when I read it only worked for Mac OS X. Coming from a .Net background, memory management seemed like advanced black magic to me. As it turns out though, it’s really not that complicated –if you follow certain rules.

 

Memory management in Objective-C is based on “object ownership”. An object can have one or more “owners”. The number of ”owners” is stored in each object using a mechanism called reference counting. You can think of this number as a counter of the number of “owners” an object has. Reference counting in Objective-C works like this:

 

·     When you “create” an object its reference count is 1.

·     When you “take ownership” of an object, you increase its reference count by 1.

·      When you “release” an object, you decrease its reference count by 1.

·      When you “autorelease” an object, you flag it to be released later by an auto release pool. When the object is released, its reference count it’s decreased by 1.

·      When an object reference count is 0, the object is “deallocated” or destroyed. The objects “dealloc” method gets called automatically, and its memory footprint is 0.

·      Once an object is “deallocated”, it cannot be used anymore.

 

So, how can you “create” or “claim ownership” of an object?

 

·      You can create or “own an object”, by using a method whose name begins with “alloc” or “new”.

·      You can “claim ownership” of an object when you call “retain”, or call a method that starts with “copy” or “mutableCopy”.

 

To clarify how you can “create” an object, I’ll be using the class I defined in my previous post.

 

Camera * myFirstCamera = [[Camera alloc] init];

 

I’m calling “alloc” therefore I “own” the object. Also, the myFirstCamera object reference count is 1.

 

I can now use the object…

 

[myFirstCamera focus];

[myFirstCamera takePicture];

 

When I’m done using the camera instance, I relinquish the object by calling release.

 

[myFirstCamera release];

 

The camera object reference count decreases by one, becomes equal to 0, and the camera object is destroyed.

 

If you want to use an object in a different scope from which it’s declared, you need to prevent it from being destroyed. You do this by “taking ownership” of the object. Storing an object inside a class is one example of when you would want to do this. To explain this, in the Camera.h interface file, I’m going add a manufacturer member variable.

 

NSString *manufacturer;

 

Next, I’m going to add accessor methods.

 

-(void) setManufacturer:(NSString *)manufacturerArg;

-(NSString*) manufacturer;

 

The syntax “:(NSString *) manufacturerArg” for sending an argument to a function is strange at first glance, but you'll get used to it the more you code.

 

 

Now to “claim ownership” of an object, we use a setter method.

 

 

-(void) setManufacturer:(NSString *)manufacturerArg{

    if(manufacturerArg  != manufacturer){

       [manufacturer release];

       manufacturer = [manufacturerArg copy];

    }

}

 

The manufacturerArg is an object which we don’t own. Because we don’t own it, it might get deallocated at some point. This is one example, when you would want to “claim ownership” of an object. To accomplish this purpose, you can call “retain” or “copy”. Calling “retain” will copy the reference of the string object, and copy will return a new object that has the same content as the argument. Both “retain” and “copy” increase the reference count by one. Although when using strings, it’s preferable to use “copy”, because we don’t want external changes to affect the state of our variable. Before I call copy, I release the previous reference stored by calling “release”. 

 

 

 

The implementation of the getter method in the Camera.m file is:

 

 

-(NSString*) manufacturer{

    return [[manufacturer copy] autorelease];

}

 

I return a copy of the member variable, so external changes won’t affect the manufacturer member variable in my class. When I called copy, I increased the reference count by one. For this reason, I must balance the reference count by calling “autorelease”. In addition to this, when you call “autorelease”, the object is added to the current topmost autorelease pool. When the objects go out of context the autorelease pool calls the drain method, which decreases the reference count of all the objects by one. When developing iOS applications Xcode creates autorelease pools, for every event dispatch, you as a programmer don’t need to worry about it. You just need to know that one of the autorelease pools, will decrement the reference count of your object by one, at some point in the future. Of course, this auto release pools can also be created manually.

 

A corollary from the previous code is an important memory management rule.

 

You only call “release” or “autorelease” on objects you own.

 

Now since I added the manufacturer member variable, there is a reference to an NSString object now. In the setter methods, I’m retaining the object to prolong its life. However, the camera class as it stands now would leak memory. Why? Because, I’m not releasing the manufacturer object. Once the camera object is destroyed, there would be no way to reference the manufacturer variable. As a result, I’m creating a memory leak.  To fix this, I need to call the release method inside the destructor.

 

-(void) dealloc{//destructor of the camera object.

    [manufacturer release]; //fix the leak.

    [super dealloc]; //the only time you should ever call the "dealloc" method.

}

 

By calling “release”, I balance the reference count in the camera object, and fix the leak. The [super dealloc] calls the NSObject dealloc function to release it’s memory. The super keyword refers to the base class(NSObject) of the camera object. This is the only context from which the “dealloc” should ever be called. In a way, this leads us to another very important memory management rule:

 

 

You should never call another objects dealloc method directly.

 

Okay, so now that we know about memory management let’s use our method.

 

NSString *manufacturer = @"Canon";

NSString *autoreleasedManufacturer = [NSString stringWithFormat:@"%@", manufacturer];

 

[myFirstCamera setManufacturer:autoreleasedManufacturer];

  

  

The stringWithFormat is a constructor method that returns an “autoreleased” string. The method doesn’t start with “alloc”, “new”, “copy” or “mutableCopy”, as a result you don’t own the autoreleasedManufacturer object, and you shouldn’t call release. If you implement your own methods, you should also follow the same convention.

 

Now let’s look at a situation when you would release a string.

 

 

NSString *mustReleaseManufacturer = [[NSString alloc] initWithFormat:@"%@", manufacturer];

[myFirstCamera setManufacturer: mustReleaseManufacturer];

[mustReleaseManufacturer release];

 

The mustReleaseManufacturer string is created using alloc, so I must release the object after using it.

 

 

To conclude, let’s mention the most important rules:

 

·       You should only release or autorelease objects you own.

·       You must override dealloc method to release the objects you own.

·       Never call another objects dealloc method directly.