Is Scala worthwhile?

23 Sep

If you’re a Java developer and start digging into some Java blogs, forums, books, etc. you’ll  surely find some people arguing in favor of Scala, and read a lot of people talking about its functional approach and how cool it is.

Here I won’t attempt to cover Scala’s features in depth, though I will point out why I think it’s worth giving it a try.

Some history

Scala was designed in 2001 and its first version was implemented in 2003 on the Java platform, by Ph D. Martin Odersky. He is a  professor of programming methods at EPFL, implemented Java generics and also the current generation of javac, so I am pretty sure this guy has a lot of Java Knowledge :)

Later, on January 2011, the Scala team won a five year research grant of over €2.3 million and with this funds, in May, Odersky and his collaborators founded Typesafe Inc. In 2012 Typesafe received a $3 million investment from Greylock Partners.

Though you may wonder, is this just a theoretical research from some university? or,  Is it a platform to build enterprise software? Well, after looking at who’s using it, it turns out that many companies are building new services based on Scala, including Siemens, Novell, Sony, LinkedIn, Foursquare and Twitter.

Let’s get into some code

Syntactic sugar

As a newer language it provides some syntactic constructions aiming at the developer efficiency. Some quick examples:

Scala aims at not disrupting object oriented principles, that’s why it has an object construction which is exactly what it says: one instance of a class. Provided that, and bearing in mind that you can name an object the same name as a class, there’s no more static keyword, just call an object by its name.

Functions

As you can read everywhere, Scala is an hybrid, it’s both an imperative object oriented language as well as a turing complete functional one. There’s a lot of interesting theory you can read about functional programming written by excellent authors, so here we’ll just see some examples again:

A function taking another function as a parameter. predicate is a function that takes one element of the same type A of the list and returns whether it should be filtered or not.

Here we’re creating another function utilizing the previous one but fixing one of its parameters, this returns a new function, which I called filterList. Note that it could have been declared as a val since in functional languages, functions are so called first-class citizens.

Finally,  we can call the filterList function with a predicate, which given an element of type String (inferred by the List of Strings) computes the standard java.lang.String.startsWith(String).

Pattern matching

Some people introduce pattern matching as a switch statement with some enhancements. In my opinion this is  like an entire new feature since it’s more powerful and less restrictive.

Let’s see an example:

Here we can see a lot of things going on. The first one is that we’re pattern matching on list value, and Scala will compare its value with the left side of each case expression, from top to bottom. In the first case we compare against a literal list, there we ask if it’s a list and if it contains those exact two strings. Then we simply compare to a string literal, or we can ask for the value’s type like the Int case. We can also add a conditional guard like the string with the if s startsWith “A” condition.

Everything is an expression

Another great thing to add is that the whole match we just saw is an expression and it’s returning the value on the right side of the matched case,  meaning that we could write:

And the compiler will infer the type of myListString, in this case being String,  because all right sides are Strings. Though they could be of other types,  and Scala would  take the base class that all of them extend.

In fact, all sentences in Scala are expressions, meaning we can get the result, for example of an if expression:

Traits

You can think of traits either as Java interfaces where you can implement methods,  or as abstract classes with the advantage that you can extend as many as you want.

Whatever way you see it, traits are a pretty way of encapsulating behavior and reutilizing code.

Imagine you have a Bird class like this:

Then you could use it like this:

That was easy. Now suppose you need a bird like the Penguin, who can swim but which can’t fly. Then you could make an abstraction like FlyingBird so Pigeon and Hawk would extend it while Penguin could directly extend Bird.

Ok, now there comes the Frigatebird, a bird that can fly but cannot swim. This bird will surely drive you crazy because he can’t swim and, in a language like Java, you can’t create a class hierarchy modeling all these birds, without duplicating code. The problem we are facing here is the well known problem of multiple inheritance and how Java handles it, you can create two abstract classes one for flying and another one for swimming, but you can’t extend both.

Here’s how you would likely solve it if you’re using Scala:

This is how birds would look like:

And now you could execute things like this:

But something like this won’t compile:

In this last case, Scala would complain that fly is not a member of Bird. That’s because it infers that Bird is the only class that Frigatebird and Penguin have in common, assigning Bird as the type parameter of the List.

So, why Scala?

Besides these features we’ve quickly looked at, Scala introduces implicits, structural types, extractors, and many more, taking the best from both worlds (Object Oriented and Functional Programming). Having that said, it could be stated that Scala is concise, flexible and Scalable.

I haven’t mentioned performance, but you could be worried about some functional constructions that create new instances instead of writing values in-place. Well, we know that the garbage collector is usually pretty good with short-lived objects but also, thanks to the concise language, you can spend your time optimizing critical parts instead of dealing with tedious non-performance critical parts. There are a few benchmarks that compare Scala to Java and they confirm that it performs fast.

As I mentioned earlier, Scala is accepted by the industry, so you can be sure it will last long and its community will be supported.

Finally it’s good to know there are plenty of tools to empower Scala, from plugins for almost every IDE and a very cool building tool, to web frameworks and database access libraries.

In the worst case scenario you could end up writing Java-style code in Scala and it would be compiled to almost the same binary code and it’d be run in the same JVM. The question we should ask ourselves is: why don’t we just give Scala a try?

References


5 Basics To Master Scrum

9 Sep

Scrum Samurai

Agile has been part of our lives for a while now, and most of the time we use it in our own kind of way. Several agile frameworks have been a subject of study by our teams throughout the years and we have learned a great deal from each of them.
As it turns out, in the last couple of months we felt like our agile mixin’ needed some kind of structure in order to really make us grow, so we decided to give Scrum a chance.

But there is one thing about Scrum that didn’t quite make it for us. The software field is full of Scrum Fanatics who are always trying to evangelize you and make you “buy” this framework. The thing is that we normally question everything and like to draw our own conclusions; thus their fanaticism turned out to be kind of counterproductive. Well, some of us signed up for a ScrumMaster Certification to find out for ourselves.

To our surprise, this experience was completely different from what we had expected. We all thought we would be sitting for two days, listening to a guy talk about Scrum and the whole framework (roles, techniques, etc), but the exact opposite happened.
Alan (our trainer) had a much more philosophical way of teaching. He didn’t sell us Scrum; he explained to us how it worked and gave us the tools for us to apply it. He didn’t put thoughts in our heads; he guided us. We didn’t just listen about the framework; we learned about life itself.

I am not just going to talk about Scrum right now, I want to do more than that. Scrum is something you can continue to learn and investigate by yourselves. I want to share with you all the other things that fascinated us, the ones that really got us thinking and realizing, that there is much more to Scrum than just a framework. I will try to be as concrete as possible; it is up to you to make these concepts grow and change your life.

Perspective

First of all, we have to be intellectually humble. And by humility we mean, the ability to evaluate ourselves correctly. It is really important that we are convinced that we don’t know everything, that we are probably wrong as many times as we are right, and that others have as much chances to be right as we do.
It is most important that we learn to put ourselves in someone else’s shoes. We won’t achieve this until we gain enough humility. Thinking as if we were the team, the client and the product, will lead us to richer and better decisions.

Yes, and…

We could say there are three kinds of people in this world. There is the negative ones; who will always give “No” for an answer, no matter what we say. We need to avoid teaming up with them.
Then there is the frightened ones; they will always find an excuse, a way to avoid challenge, and they are not committed. Their favourite line is “Yes, but…”, and we know they might still be saved if we all try very hard.
Finally there is the positive ones; they not only answer “Yes”, they always manage to answer “Yes, and…”. These ones always look at the bright side of things, find ways to make ideas grow and they are committed 100%. These are the ones we want to have by our side in our teams, in our lives. These are the ones we want to be.

Always Deliver Value

Every customer seeks to get some kind of value in return, and software is not the exception. When someone comes to us asking for a product or service, we not only have to give them what they asked for, we need to make sure it will still be of value to them once we deliver. That is why Scrum tells us to apply the concept of organic growth.
Organic growth defines a process in which we iteratively make our deliverables evolve from the previous one, with one little characteristic; we need to assure that every deliverable is functional and that it adds value to our customer. If it does not, it is no good.
We need to understand that by having functional and frequent deliveries, we gain the ability to detect errors, bugs or enhancements much earlier, making errors cheap.

Continuous Improvement

We won’t grow unless we suffer. The only thing we will achieve by being comfortable is decay. We need to get out of our comfort zone to better ourselves, try to break the status quo.
Alan defined the concept of Pain-Driven Facilitation, in which he stated that the best way to grow is to challenge ourselves with small, tolerable and constant changes. Once they stop being a challenge, we need to introduce new ones, and so on.
There is a Japanese word called “Kaizen” that means “change for the better” which helped us make that concept sunk in.

Perfection

Some people say that perfection does not exist. I say it does. Whether we are able to achieve it or not is another story. People always try to get what they can’t have, and maybe perfection is one of those things.
That is what makes it so unique and desirable. We will always try to get there, because it is hard, because we know we probably won’t. Our ambition is what makes us try harder, learn more and grow to be successful.
Scrum is very similar. We know we won’t be able to make everything work as Scrum expects, but it challenges us to do so, and we like it. We will always be improving ourselves to get there.


Keep Thinking

I don’t feel there is much need to write a big conclusion. There is only one thing I would like to ask you: keep thinking about these concepts and sleep on them for a while. Then, if you think it is worth it, try to spread them out as much as possible, as our trainer did with us.
Let’s help people be more humble, have a positive perspective, deliver value, change for the better and be ambitious.
Stay tuned, there is more to come!



Photo credit: graphistolage


A story about Gemified Engines

21 Aug

Needless to say, Engines are a core aspect of the Ruby on Rails framework (versions ~>3.1 and ~>4.0, for now). Having a basic understanding of what they are and how they work is what we wanted to address in these slides. This is a short introduction we made for one of our “Friday Talks”, together with “empanadas” for lunch.


Performance Matters!

22 Mar

Performance Matters!

Here are the slides from my talk on Performance at http://rubyconfuruguay.org

  Below you can find the mentioned references and tools.

Why does performance matter?

Google Search uses site speed in search Ranking Marissa Mayer at web 2.0 The Google Gospel of Speed Nielsen Norman group on web response times Articulo pdf de la Unversidad de Nebraska sobre tolerancia de dos segundos

Tools to measure:

jMeter Rails guides for performance testing

Gems that will help you detect problems

Query Reviewer Rails Footnotes New Relic Perftools

Test

The xkcd comic that inspired the test example. The subscription link to Ben Orenstein’s emails with suggestions to speed up the test cycle.

Remember!

Measure, know where to look and most important write enough unit/functional tests to avoid breaking havoc on your app!


Ruby And Exception Notification

1 Feb

The problem

In a project we are working on right now we needed to have a way to be notified when a crash on our application occurred, something that we though was going to we easily archived by a simple:

  gem install 'notify_me'

After some research we came to the conclusion that it was not going to so easy.

Existent solutions

If we where on rails we would just have done:

  gem install 'exception_notification'

And even tough i tried to run that gem outside of a rails application with some hacking i was unsuccessful and even i could have done it i felt like there where way too much dependencies for a simple notification email.

Exception Notification Dependencies


i18n
multi_json
activesupport
builder
activemodel
erubis
journey
rack
rack-cache
rack-test
hike
tilt
sprockets
actionpack
mime-types
polyglot
treetop
mail
actionmailer
eventmachine
multipart-post
faraday
faraday_middleware
hashie
json
http_parser.rb
simple_oauth
twitter-stream
tinder
So we started to take a look to other options like sinatra-enotify, it didn’t seem to cover our needs (you can’t even configure the email settings) and did not seem like a production ready gem. We also investigated bugsnag and airbrake again they seemed overkill for a simple email and even required an account (in some cases paid) on an external site to get them to work.

Path Taken

Having not found any existent solution and in the spirit to do some research and learning i embarked in the process of writing a simple exception notification gem for ruby.
Thus [RU]by [S]imple [E]xception [N]otification (rusen) was born.
The idea behind it was that it should we really easy to pickup and use but also provide more advanced options and control if it was needed.
The simplest way to use it is the following:

  gem install 'rusen'

then use it inside of your ruby script:

It also comes with a rack middleware, how to setup that and the more complex ways to use it can be found in the documentation.

Conclusion

In the end it was an enjoyable experience in witch i learned a lot of things and the end result is something simple yet flexible that we can actually use in our production environment.


We proudly announce our new website

17 Oct

We want to thank everyone who was involved in this challenging and creative process, and truly hope you like the results. We certainly enjoyed the creation.

Part of the celebration was thanks to Mariana de Carli, who brought some delicious homemade cupcakes. Thanks Mariana! They were yummy!

Please have a look at our new website, and leave us your comments if you have a chance.

Keep on mooving!


Moove-iT is meeting up.

16 Oct

We say YES to the Tech Meetup taking place this 3rd November, 2012 in Montevideo, Uruguay.

This is an opportunity for you to share your IT experience and learn from others, grow as professionals and constantly improve your work.

Through the story of seven speakers, we will learn from the experience of professionals from Uruguay and the region in the most different environments.

Some of the topics are: Development Processes (teamwork, scrum, Agile, continuous integration, code review, continuous delivery, testing, QA), Infrastructure and Production (monitoring, scaling and server redundancy, track changes, versioning, deployment) and Others (remote teams, real experiences).

If you want to share your own experience you still have time to sign up as a speaker (whether you work in a large company or from home). We want to hear from you! You just have to complete the form you will find at www.meetup.com.uy.

The event is $ U400 and you can book your place through “Red Pagos” (collective number 34264). If you are a student, you can request a free pass for the event. Just apply through the event website.

Do you like the idea? We will see you next November 3rd, 2012 at 9:00 am at the Telecommunications Tower (Rondeau Ave. and Guatemala St.).

Please contact info@meetup.uy for more information.

Afiche difusión meetup.uy


Handsoap & testing Web Services

3 Sep

HandsoapIf you’re using the Handsoap gem to implement a web service client you may be interested in knowing how to stub the call to the webservice and returning the xml you want.Here is a quick example showing how to do just that.

 

Let’s dive in!

I used Mocha to stub the methods. But you can use whatever library you like.

When using Handsoap normally you have a class that inherits from Handsoap::Service where you configure the Web Service client.
The aim is to use a xml string to mock the web service’s response.
When we want to call a webservice we use the invoke method supplied by the Handsoap::Service class.
Example:

So that method is exactly what we want to stub. That way we make sure we test all our code, not worrying about the gem’s code and of course without actually hitting the remote web service.

The xml string we would like to get as a response might look like this.

 

You can get this xml string if you print out the response of a webservice call.

Looking for the request call

Digging into the handsoap gem I found that the response object of the Service#invokemethod is the one returned by:

How does parse_http_response work?

Building the response object

If you look at Service#parse_http_response it calls Service#parse_soap_response_document where you can see how it builds the xml object from the raw xml string.
So this is exactly how we will build the response object for our stubbed method, namely:


This is safe to do since we are using Handsoap’s configuration to parse the string and it will work just as if you are using it in production.
Now again in Service#parse_http_response you can see the final line doing:


So when stubbing we need to respond with:


But before that, make sure that if you did override the on_response_document method, you will have to call it explicitly since the parse_http_response won’t be calling it for us:


Finally, we are ready to stub the web service with our xml document:

Bringing it all together


Now, whenever you call invoke on your web service class, it will respond with the xml string you passed in to the XmlQueryFront#parse_string.


Running Selenium on a Headless server and running tests with PHPUnit

16 Jul

When it comes to functional testing, please have a look at Selenium, a testing framework that provides tools and asserts on a webpage running on a web browser.

The aim of this article is to show you how to set up a Selenium server, with or without Xserver, and run PHP unit tests on a web page.

Before starting you will need:

  • Linux running machine (with or without Xserver)
  • Java
  • Selenium server jar file (can be found here)

You will learn how to install the following:

  • Xvfb (check this) [just if we dont have a Xserver]
  • PHPUnit – PHP – Pear - Selenium TestCase extension (PHPUnit_Extensions_SeleniumTestCase)

I am going to use Fedora for this example, but bear in mind that the only thing that might change in other Linux ditros could be the package manager. [...]


Why Uruguay?

27 Jun

Outsourcing to Uruguay does make sense.

According to CUTI, the Uruguayan Chamber of Information Technologies, Uruguay’s strategic location, cultural affinity and economic stability are some of the key factors that make this country one of the top technology producers in Latin America.

Please have a look at the infographic below for further information.