jueves, 22 de agosto de 2013

Open Close Principle

The father of this principle is Bertrand Meyer, this principle was first presented in Bertrand’s Book Object Oriented Software Construction. What this principle states is that a module or class should be open for extension, but close for modification. But how’s that possible, what do we should understand for extension and close.

Extension: It should be easy to change the behavior of that class or module.
Close: Source code should not change.

The main benefits of use this principle is:
Help with the maintenance of code.
Clearer code

As an example of the violation of this principle we can consider a class that is in charge of drawing different shapes, it should be clear that this violates this principle because each time we want to add a new shape we have to modify the code.

Code Violating the OCP

class ShapeEditor{
       public void drawshape(Shape  shape){
               if (shape.type == 1)
                   drawtriangle(shape);
               else
                    if (shape.type == 2)
                        drawcircle(shape);
}
         public void drawcircle(Circle circle) {....}
         public void drawtriangle(Rectangle rectangle) {....}
}

class Shape{
        int type;
}

class Triangle extends Shape {
         triangle() {
             super. type=1;
                        }
 }

class Circle extends Shape {
      circle() {
           super. type=2;
                }
 }

A possible solution could be:

class ShapeEditor {
  public void drawShape(Shape shape) {
  shape.draw();
  }
 }

 class Shape {
  abstract void draw();
 }

 class Triangle extends Shape  {
  public void draw() {
  // draw the triangle
  }
 }
class Circle extends Shape  {
  public void draw() {
  // draw the circle
  }
 }

jueves, 8 de agosto de 2013

Single Responsability Principle

This term was first introduced by Robert C. Martin, the author of the Clean Code Book. And what he wants to teach us is that a class must have only one responsibility. But wait!!, what  is a responsibility? Well Robert C Martin says that a responsibility is a reason to change.
The advantages of applying the SRP in our work are:  
We are going to obtain classes more robust and focused.
Code complexity is reduced.
Readability
Clearer code

As an example we can consider a system of a company in which there is a class that has methods that deal with business rules, reports and also the database (the example showed by Uncle Bob in the video of Clean Code):

public class Employee{
         public Money calculatePay(){…}
         public String reportHours(){…}
         public void save(){…}
}

As we can see here is violated the SRP because  calculatePay() will change whenever the business rules change for calculate the salary. In reportHours(), the case for change is when someone wants a different format to do a report. And finally save()  will change when the schema of the DB changes.
So how do we fix that, and fulfill this principle?
Well, we have to split this into different classes, in this case 3 because the class below has 3 reasons to change as I explained earlier. So the result could be:

public class Employee{
         public Money calculatePay(){…}
}

public class EmployeeReport{
         public String reportHours(Employee e){…}
}

public class EmployeeStore{
         public void save(Employee e){…}
}

domingo, 4 de agosto de 2013

Clojure learnings

Well this post as the title says it’s going to be about clojure. Clojure is a functional programming language and it is a dialect of lisp. In this language every expression goes into parenthesis. And its syntax is kind of different because here for example to do add number you have to write ( + 2 3), or if you want to know the length of a vector you have to do (count vector) instead of vector.length as you do it in ruby.  I’m starting with this programming language, it is completely new for me, and it is very interesting.
Now I’m going to show you some simple examples to get started with Clojure.
Well let’s start with hello world:
If you do it directly from console you can write: print “Hello World”
But if we do it making a function, we can write:

(defn hello [] "Hello World")

and then just call it from console like this: (hello)
Also from what I’ve been doing I can tell that if you have to define a global variable, you have to make it an atom. So for example in the next example I did a function that counts the number of elements of a vector, but first I defined the vector.

(def example_array (atom (vector 1 2 3 4 5)))
(defn largo []
  (count  @example_array))

Other of the examples that I did was to insert an element into a vector, so in the next function I call the function with the number that I want to add to the vector.

(def example_array (atom (vector 1 2 3 4 5)))
(defn insert [arr number]
  (conj arr number))

Another thing, was trying to do the factorial function I made a try, but when I was testing it, I found that if I give number greater than 29 it says integer overflow, I’m still figuring out, but it is a good start I think.

;; for numbers < 30
(defn factorial [number]
  (if (<= number 1)
    1 (* numeber (factorial (- number 1)))))

And also, I have to tell you that comments in Clojure start with “;;”
The next function prints the numbers from 10 to 1.

(def a (atom 10))
(while (pos? @a) (do (println @a) (swap! a dec)))

Also I made a function which you give it a vector and its length, and it prints a vector with the odd numbers.

(defn oddnumber [x nums]
    (take x
      (for [n nums
            :when (odd? n)]
        n)))

Moreover, if you have a vector you can ask if it contain a determined element.

(def example_array (atom (vector 1 2 3 4 5)))
(contains? example_array 2)

And here are some more examples:

(defn square [number]

    (* number number))

(defn add [number number2]

    (+ number number2))

(defn whenexpression [number]
  (when (= 1 number) true)
)