Development

Say it in code

Customer don’t know what they want

Product development is often about fusing a great customer requirement with a technical solution from the architects and the team who are to make it an reality. Both sides must be taken into consideration or it will end up a product that look great but can’t be used or is great to use but do not fill a requirement.

I think really great products come from melding two points of view—the technology point of view and the customer point of view. You need both. You can’t just ask customers what they want and then try to give that to them. By the time you get it built, they’ll want something new. —Steve Jobs in Inc

This come from an interview with Steve Jobs in Inc back in 1989 but it is as current today as it was when it was published. Every customer who enters the stage at a product company have a goal in mind. Something they want to achieve. They have been working on their own, trying to jolt down the end goal in as many ways they could. In sketches, in specifications and sometimes even in prototypes. The challenge for the product company is not to create what they bring to the table, the challenge is to find all areas of that goal they have in mind but never wrote down on the specifications.

Back to square one, the customer can’t tell you what they want. They will however let you know when you have created what they did not want. Or when you created the bare minimum of what they expected to see. All in all, we end up in a situation where the customer is unhappy and might even feel fooled since they have something they did not want and the producer is not understanding why they wont get payed since they did their part as they saw it. Is there a way to handle a situation like this before we get to this point?

It all comes down to iterations. Whether it is called Agile Development, Scrum, Kanban or something created in-house it all comes down to short goals and verifications together with bold product management. If we allow ourselves to do some wrongs to make a right it is no longer as scary to show a prototype to a customer and get back that is was all wrong. With short iterations we can just scrap what we had, create a new take on the same problem and show it instead.

If we allow ourselves to do some wrongs the entire software product market would mature in a way it is today to proud to do. There is few customers who would accept this way of working in a bigger scale since they have most likely been burned by other software producers in the past.

Try it out, throw yourself in front of a customer; show and tell. Before you know it, perhaps you even get better at demoing your other products if it is something you do over and over again.

Changing text editor is not a small thing, say hello to Sublime Text

When on a Windows machine I have been using Notepad++ for many years as my one-stop-editor for everything I need to do quick and easy. It has served me well and it’s minimalistic edge has really been something I’ve loved it for. A change is however on the way. I have found another one.

Say hello to Sublime Text 2. I am not even sure that all functionality is there which I’ve learned to love in Notepad++ but still it made me switch silently. I just noticed today that I was using it instead of Notepad++ when I was throwing in a quick code fix for xds.se and it was as natural as if I had been using it for a long time. The full screen option in it gives your zen a boost in the right direction and together with the minimap at the right side it fitted me needs as the hand fits the glove. I also found that Sublime Text even supported markdown both in syntax and in visual appearance so that you can hide away all grungy code-ui and make it look like a quite slick text editor for writing text.

Even if you are not ready to let go of Notepad++ yet, give Sublime Text a spin and see how it works out for you.

Links:
Sublime Text 2
Notepad++

Test regular expressions

Everyone who has been into development have at least once run into a problem which is to be solved by using a regular expression. The power of these can totally smash the most complex problem into a piece of cake, but the problem remain that very few people actually understand how these expressions work.

A little while ago I came across a flash-tool in which one can test out regular expressions on a specified text. All you do is to paste an example-text into the text-box in the tool and then you can start typing a regular expression and see what that specific expression will hit in the pasted text.

Easy and fast, use it all the time nowadays. Check it out!
http://www.gskinner.com/RegExr/

Note: You can also find this link under xds.se > Cheat sheets > Links often used

Arraylist vs. Array

ARRAYLIST and ARRAY are quite alike but in the same way very unlike. They can both keep data in a structure which you can walk through in different ways but the ArrayList is a bit more dynamic in how we can use it.

An Array is always declared with a static amount of data. In the example below we create an Array named myNumbers with space to hold three integers.

int[] myNumbers = new int[3];

UPON CREATION they will all be set to 0 so actually they exist directly after creation. If we try to access or add a fourth integer to this Array we would get an ArrayIndexOutOfBoundsException. You might think that all we need to do is to declare same myNumbers with more then three spaces for integers but if you do this the old myNumbers will be erased and a new created with more space.

int[] myNumbers = new int[4]; /* this would completly erase the
old myNumbers-array, creating a new one */

So if we would like to have the strength of an Array but feel that what we usually can do with it is to less we can as of Java 1.5 or later use an ArrayList. The declaration for an ArrayList look like this.

ArrayList name = new ArrayList();

THE CLASS should be what kind of variable you would like to store in the ArrayList, if you do not use any class here any object can be saved. Using no class at all is the same as using Object, i.e any object can be saved.

ArrayList myNumbers = new ArrayList(); /* can hold Integers only */
ArrayList collection = new ArrayList(); // can hold any object

When we have an ArrayList created we can use some built in functionallity to handle it. I will give short examples below in a code-snippet.

ArrayList myNumbers = new ArrayList(); /* can hold Integers only */
myNumbers.add(10); // adds the number 10 to the list
myNumbers.add(10,1); /* adds the number 10 to second element in
the list due to the fact that the first element have index 0 */
myNumbers.get(0); // get the first element from the list
myNumbers.size(); /* returns the number of elements in list,
in our case 2 */
myNumbers.isEmpty(); // true if empty
int myCheckedNumber = 10;
myNumbers.contains(myCheckedNumber); /* returns true if 10 is
within the list */
System.out.println(myNumbers); /* would print "[10,10]" in
console */

WORTH TO REMEMBER is that an Array with n elements would only need about 4*n bytes of memory. If we use an ArrayList for the same n elements would require about 20*n bytes of memory. The reason for this is that the ArrayList is an array of objects as well as an array of references. Due to this fact we always should use Array if we know how many elements we will need and memory is an issue for us. ArrayList should in this aspect only be used in cases where we need functionallity from it.

Basic exception handling

WHEN WE EXECUTE a bit of code there is almost always the possibility for something to break. Sometimes it can be the purpose of the code to break because if it didn’t do so the consequences might be more severe. When these cases arise we call them exceptions or exceptional events.

To explain this in it’s most simple state we can look at a very small program which consist of a parent-class (main) and a child-class (subMeth) in which the child-class tries to divide an integer with zero.

public class TopDog {
  public static void main(String[] args) {
    SmallDog(5,0);
  }

  public static void SmallDog(int first, int second) {
    int average  = first/second;
  }
}

WHEN DIVISION BY ZERO is executed the engine will scream from the top of it’s lungs at the child telling him/her that division by zero is fobidden. Of course the child will run to it’s closest parent and start weeping. And due to the fact that parents usually are quite bad at math the problems can not be solved even here and this makes the entire world collapse. I.e the application close.

So, how do we stop the sky from falling? We build a way for the application to solve the problem when it arise so that the parent don’t need to receive the problem at all. To do this we use the keywords try and catch.

public class TopDog {
  public static void main(String[] args) {
    try {
      SmallDog(5,0);
    }
    catch (Exception e) {
      System.out.println(”Exception : “+ e.getMessage());
    }
  }

  public static void SmallDog(int first, int second) {
    int average  = first/second;
  }
}

This would make the same exception to be thrown from division by zero but we will catch the error and let the application continue while explaining for the user what happened. Output to use would give the same error as before but the application do not crash.

SOMETIMES WE WANT to execute a bit of code after a try regardless of an exception is thrown or not. It could be a file we tried to read from that we of course would like to close to not let memory and sockets linger. In Java this is possible by using the keyword finally. The finally keyword always come just after a try or catch. No extra code can be written in between.

public class TopDog {
  public static void main(String[] args) {
    try {
      SmallDog(5,0);
    }
    catch (Exception e) {
      System.out.println(”Exception : “+ e.getMessage());
    }
    finally {
      System.out.println("The end of the line.");
    }
  }

  public static void SmallDog(int first, int second) {
    int average  = first/second;
  }
}

In this example the string “The end of the line” would be printed regardless if an exception was thrown or not.

Simple class-design in Java

WHEN I TRY TO LEARN a new subject I always thought the best way is to try to explain what I’ve learned to someone else. Therefore I’m continue my experience with Java here and try to xplain in steps what I have been doing and learning.

So let’s get to create an easy class with some basic content. We start with the skeleton of my ‘Car’ class.

public class Car {
  public Car(){
  }
}

We need to state what fields exist for our car, in other words what are the caracteristics of our car we see on all cars that exist.

public class Car {
  private int speed;  // current speed of our car
  private int passangers; // the number of passangers

  public Car(){
  }
}

If we want the passangers-variable to always be set to four when we create a new car we just place it within our constructor. We can also get values from the creator at this stage, we use this to set the speed.

public class Car {
  private int speed;  // current speed of our car
  private int passangers; // the number of passangers

  public Car(int speed){
    this.speed = speed;
    this.passangers = 4;
  }
}

AS YOU CAN SEE we use the keyword this to use the fields available to us within the class. Also noteworthy is that in our constructor we chose to call the incoming parameter for speed eventhough our field is called speed. This is no problems and we just use our this keyword to separate the one from the other.

Our fields was set to private, this means that only the class itself have access to the values stored within. Due to this we must create methods for getting and setting the values in some way. We call these methods getters and setters.

public class Car {
  private int speed;  // current speed of our car
  private int passangers; // the number of passangers

  public Car(int speed){
    this.speed = speed;
    this.passangers = 4;
  }

  public int getSpeed() {
    return this.speed;
  }

  public int getPassengers() {
    return this.passengers;
  }

  public void setSpeed(int speed) {
    this.speed = speed;
  }

  public void setPassengers(int passengers) {
    this.passengers = passengers;
  }

}

We now have the backbone of our class with getters and setters in place.