Code Tips #10: Ruby – Compare objects

In a previous post I wrote about how to compare objects in Javascript. In Ruby we can do the same using the Comparable module. We include the comparable module in our class and we define a pseudooperator-method < =>. Let’s say we hava again a Car class that looks like this:

class Car
  attr :speed

  def initialize(speed)
    @speed = speed
  end
end

And now let’s make their instances comparable by Continue reading Code Tips #10: Ruby – Compare objects

Code Tips #9: Javascript – Compare objects

Many times we are in situations where we want to compare instances of a class, aka objects. Let’s see it with an example

class Car {
  constructor(speed){
    this.speed = speed
  }
}


const car1 = new Car(100)
const car2 = new Car(120)
const car3 = new Car(90)

console.log(car2.speed > car1.speed) // true

const sorted = [car1, car2, car3].sort((a,b) => {
  return a.speed - b.speed;
});

console.log(sorted) // [ Car { speed: 90 }, Car { speed: 100 }, Car { speed: 120 } ]

This works perfectly fine but we can do better Continue reading Code Tips #9: Javascript – Compare objects

Code Tips #8: Null Object Pattern

Many times functions accept object references that can be null, and we tend to add if statements to treat the special case where null is passed to a function, and either provide a default response or do nothing. In the following example our calculateSpeed function expects an object that has a getSpeed function. In Javascript we will have to do something like:

class Car {
  getSpeed(){
    return 100;
  }
}


const calculateSpeed = function(vehicle){
  if(vehicle && typeof vehicle.getSpeed === 'function'){
    return vehicle.getSpeed();
  } else {
    return 50;
  }
}

const car1 = new Car();

console.log(calculateSpeed(car1)); // 100
console.log(calculateSpeed()); // 50

But there is a better way to achieve that. Using the Null Object Pattern we can Continue reading Code Tips #8: Null Object Pattern

Code tips #7: Ruby – Keeping state across class instances and the problem with class variables

Class variables is a typical way to share state across instances of a class. Let’s say we have a Playlist class and we want to count how many instances of this class we have. We can write the following:

class Playlist
  @@number_of_playlists = 0

  def initialize
    @@number_of_playlists += 1
  end

  def play
    p "Playing..."
  end


  def get_number_of_playlists
    @@number_of_playlists
  end
end

The problem comes when Continue reading Code tips #7: Ruby – Keeping state across class instances and the problem with class variables

Code tips #6: Ruby – The clear way to declare static methods in a Class

In Ruby to define a static method in a Class, you can write the following:

class Song
  def Song.start
    p "Music start...!"
  end

  def Song.stop
    p "Music stop!"
  end
end

This works fine but in every function definition, you have to retype the name of the class, meaning that in case you decide to rename the Class you have to change all of these method names. A better solution would be Continue reading Code tips #6: Ruby – The clear way to declare static methods in a Class

Rage against flexibility

Often while I am reviewing code I stumble upon pieces that are trying to do way more things than the requested feature demands. We all have been there, either reading or writing code that does more things than the ticket/issue we are working on requires. Many times we try to predict the future, because we guess that touching this specific part of the system now is easier than doing it later when a new requirement comes up. In my opinion this mentality is completely wrong and can lead to slowing down a team instead of moving things faster.

Let’s image that your boss asks you to create a component that accepts an array and renders a list of items, and you guess Continue reading Rage against flexibility

Book Review: Front-End Reactive Architectures: Explore the Future of the Front-End Using Reactive JavaScript Frameworks and Libraries

(2 / 5)

I had high expectations, but this book doesn’t really cover the topic in depth, its more or less a list of reactive libraries and frameworks. If you want to learn more about the topic its probably better to read related papers, or what’s the talks about RX on youtube.