Sunday, 15 September 2013

How to configure Gradle build on Jenkins

This annoyed me a bit because of course if you read it fully it becomes clear but the GUI is terrible.  



It looks like the options 'Build step description', 'Switches', 'Tasks' etc belong to the lower radio button but actually they belong both.  There should really be a line in there between 'Use Gradle Wrapper' and 'Build step description' to make it clear.  Bad GUI!

So, for example, to run tests with the Gradle build simply write 'test' in the 'Tasks' field and it's done. 

Friday, 13 September 2013

C# extension methods example

Extension methods can be used extend an existing class without creating a new derived type of the class to be extended or modifying and recompiling the class you want to extend.

Say we have a Utils project in our solution and in that project we have a class with a static method which converts a string to camel case.

To call this method we must add a reference to this project and pass a string to this method:


However, what we can do is change the ToCamelCase method to be an extension method. We do this by modifying the signature of the ToCamelCase method:


To call it now, we call it like we would call any other string method. It even appears in Intellisense!




Note that in an extension method the first parameter is the one to which you apply the 'this' keyword. Any normal parameters that the method should take follow that first parameter.


Wednesday, 11 September 2013

Ref keyword example in C# with structs

In C# if you define a class, then an instance of that class is passed around by reference as it is a reference type.


The above code shows a class called 'Range' which has two int properties, X and Y.

However, if you are familiar with Mircrosoft's suggestions for when to use a struct, you might realise that we should use it here. Microsoft say that a struct should have all of the following characteristics:

  • It logically represents a single value, similar to primitive types (int, double, etc)
  • It has an instance size under 16 bytes (remember that an int is 4 bytes)
  • It is immutable
  • It will not have to be boxed frequently
Ok so let's define our Range.cs as a struct instead.  All that we need to do is change the keyword 'class' to 'struct'.

Easy peasy. 

Struct defines a value type and so instance of our range object will be passed around by value instead of by references. 

[Note: 
Passing by value: When the object itself is passed around
Passing by reference: Passing a reference to the object in memory]

To illustrate this point imagine that we have 2 move methods, one that takes in a Range and one that takes in a StructRange and moves them diagonally upwards to the right.

Now let's calls these methods:

The output is the following:

          [Range: X=1, Y=1]
          [Range: X=2, Y=2]
          [StructRange: X=1, Y=1]
          [StructRange: X=1, Y=1]

What what? It worked on the range object and moved the coordinates from (1,1) to (2,2) but on the structRange object nothing changed.

When the Move method was called with the structRange, the method was passed copies of the values 1 and 1.  It incremented those values, but the original objects were untouched. To fix this, we need to pass a reference to the struct object to the Move method.  Then the method goes and looks in memory for the ints stored at that memory location and increments them.

Passing by references is shown in the following snippet:

The output is then correct; the move method has been applied:

          [StructRange: X=1, Y=1]
          [StructRange: X=2, Y=2]

Java API for Websphere MQ

There are 2 ways of interacting with WMQ from Java.

1. A set of proprietary Websphere MQ classes for Java
2. A set of WMQ classes for JMS

I strongly recommend the latter because -
  • then you are not tied in to using WMQ.  You can change the message queuing software you are using without having to change your Java code. You may think this is unlikely but I've seen it done. 
  • you get to use spring JMS which is awesome.  It completely removes the need to write any boilerplate code and makes your configuration simple and your code minimal.  
Receiving Messages

You can either use a MessageListener or receive message explicitly. 

Receiving using a MessageListener

First you need to create a class which extends javax.jms.MessageListener and override the onMessage(Message message) method. 

 public class AlexandrasMessageListener implements MessageListener {  
    @Override  
    public void onMessage(final Message message){  
      if (message instanceOf TextMessage){  
         // process your message  
      }  
      else {  
         // throw some kind of error   
      }  
    }  
 }  

This needs to be plugged into a jmsContainer.  I used org.springframework.jms.listener.DefaultMessageListenerContainer rather than the SimpleMessageListenerContainer. The Default is used more commonly (probably because it allows XA Transactions).

 <bean id="alexandrasListener" class="com.alexandra.AlexandrasMessageListener"/>  
   
 <bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">   
   <property name="channel" value="${mq.channel}"/>    
   <property name="hostName" value="${mq.hostName}"/>   
   <property name="port" value="${mq.port}"/>   
   <property name="queueManager" value="${mq.queueManager}"/>    
   <property name="transportType" value="1"/>   
  </bean>   
   
 <bean id="alexandrasContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
    <property name="connectionFactory" ref="mqConnectionFactory"/>  
    <property name="destinationName" value="${mq.queue}"/>  
    <property name="messageListener" ref="alexandrasListener"/>  
    <property name="concurrentConsumers" value="1"/>  
    <property name="maxConcurrentConsumers" value="1"/>  
    <property name="idleTaskExecutionLimit" value="4"/>  
    <property name="maxMessagesPerTask" value="4"/>  
    <property name="receiveTimeout" value="5000/>  
    <property name="recoveryInterval" value="5000/>  
 </bean>  


Receive using a JmsTemplate

Create a receiver class which contains a JmsTemplate.
 public class AlexandrasReceiver {  
   
    @Autowired
    private final JmsTemplate alexandrasTemplate;  
   
    public TextMessage receiveMessage(){  
      final Message message = alexandrasTemplate.receiveMessage();  
      if (message instanceof TextMessage){  
         return (TextMessage) message;  
      }  
      else {  
         // do something else  
      }  
    }  
 }  

Configure your JmsTemplate in spring and wire it in.

 <bean id="alexandrasTemplate" class="org.springframework.jms.core.JmsTemplate"  
 p:defaultDestinationName="${mq.queue}" p:receiveTimeout="2000">  
 <property name="connectionFactory" ref="mqConnectionFactory"/>  
 </bean>  



Best Practices for using var in C#

Var was introduced in version 3 of C# and Resharper was updated such that now, any time you don't use a var, Resharper shouts at you and highlights the offending line by underlining it in a squiggly green line.




However, sometimes C# developers can get a little bit 'var happy' and turn everything into a var, which isn't always a good idea.  The most 'non-functional requirement', if you will, that a developer should fulfil is to produce code that is readable and easy for another person to understand quickly.  The extra minute or two it takes to understand a simple line of code may seem like nothing, but if this extra time is required to understand a whole chunk of code the time adds up.

Here are a few examples of when and when not to use var.

Example 1


Consider the following code:

Dictionary<string, List<Person>> myDictionary = new Dictionary<string, List<Person>();

Talk about LONG WINDED.  This is clearly a good candidate for var.

                      var myDictionary = new Dictionary<stringList<Person>();

I think we all agree that it it obvious what time the variable 'myDictionary' is.

Example 2

var hobby = person.Hobbies.First("baseball");

This is slightly more ambiguous that the previous example.  What are we returning? Is is perhaps an object of type 'Hobby' or maybe 'IHobby' or what about 'Baseball'?  It is obvious that we are returning something to do with someone's hobby but without knowing the codebase or going and looking inside the Person class we don't know what object we are dealing with.

If there are a string of method calls then avoid assigning the result to a var.

Example 3

var i = 1;
var s = "hiya";

Apart from the fact that I think that 1-character variable names are of the devil (except inside a loop) this is fine. It is obvious that these are an int and string respectively.

Summary

In general, follow your instincts.  I think it is far worse to write ambiguous code overusing var that to be a bit long winded and spell everything out.

It is also worth mentioning that if you are using var, it is generally a good idea to make sure that your variables are named appropriately (though of course you should be doing this anyway regardless of whether or not you are using var :) ). 

A short introduction to delegates in C#

Imagine that you have some code that iterates over a list of ints and prints them.

Now, suppose that you decide that you want your display method to display them in a different way.  You actually want to write your list to a file rather than printing to the console, but you need to be backwards-compatible, so you need to retain the ability to simply print to console.

The first thing that you think of might be to create some sort of Handler object and call the equivalent of the handler.handle() method on it (this is essentially the strategy pattern, if you are interested). This is shown in the following example:

We have classes implementing the IDisplayHandler interface which display objects in different ways.

Example usage is shown below:

Lovely, you say.  Where on earth do delegates come into this?  Well, in C# you can use delegates to do make this solution much simpler.

Introduction to Delegates


Essentially, a delegate is a type which defines a method signature. An instance of a delegate can point to any method with the corresponding signature and delegates are used to pass around methods as parameters.

For example:

public delegate int ExecuteCalculation(int x, int y); 

This defines a method signature which takes in two ints and returns one int. Let's understand this more clearly with a simple example based on our strategy pattern display example above.

Simple Delegate Example


Define a delegate using the delegate keyword.

 private delegate void DisplayIt(object o); 

This delegate will need to point to a method which takes in any object and returns void.

I know a method like this! The plain old Console.WriteLine().

var display = new DisplayIt(Console.WriteLine());

All that remains is to pass it into the Display method which, instead of calling Console.Writeline() explicitly will simply pass the objects to the delegate which in turn calls its 'Invoke' method and VOILA, you have printed to the console.  

The example below shows the complete code:






Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning...