I started my career as Ruby developer and I have not been in C++, Java (except for some Clojure) or C# land. Here at Diatom most of the people are .NET developers and I was selling them Ruby and they were selling me C#. In this post, I wanted to share how I got convinced to use C#, what I learned to appreciate about it and what it means for me as a Rubyist. Please note, that this is not a rant against Ruby, I love the language and I still think it is one of the best tools out there.
My intro to the mobile world
I wanted to learn a mobile long time ago, but for me, the road always seemed kind of quirky – You would need to learn Java for Android and ObjectiveC for iOS and I am not a fan of both languages, to be honest. Our company started using Xamarin framework, which allowed to write business logic once for all apps using C# and to leverage native view approach (That part also is handled in C#). For each platform, the speed is almost the same as with the native approach. The idea seemed really cool to me and I asked if I could be of any help in the mobile front. That’s what got me started and now I can share my findings with you.
Static typing
Static typing is something we do not have in Ruby. It seems strange for us to pass type to every method call, property or variable declaration. In Ruby we leverage Duck typing – It’s a great power when you can just call a method on the object as long as it responds to it.
class Duck def make_noise "QUACK" end end class Cow def make_noise "MOO" end end class Human def speak "HELLO" end end class NoiseMaker def initialize(animal) @animal = animal end def make_noise @animal.make_noise end end puts NoiseMaker.new(Duck.new).make_noise puts NoiseMaker.new(Cow.new).make_noise puts NoiseMaker.new(Human.new).make_noise
Press ENTER or type command to continue QUACK MOO file.rb:23:in `make_noise': undefined method `make_noise' for #<Human:0x007f914c87e328> (NoMethodError) from file.rb:29:in `<main>' shell returned 1
On the other hand, we can hurt ourselves by passing a wrong object and it might take a considerable amount of debugging time in order to understand what has happened. IDE can leverage static typing by helping us with the types that we need to pass:
Interfaces
Ruby does not have explicit interfaces. We can always define an interface with an abstract class or module, but the environment and compiler does not help us with that:
class AbstractUserClass def greeter raise NotImplementedError end end class User < AbstractUserClass attr_accessor :username, :password def initialize(username, password) self.username = username self.password = password end end User.new("username", "pass").greeter
When executing this code you would get and error in runtime:
Press ENTER or type command to continue file.rb:3:in `greeter': NotImplementedError (NotImplementedError) from file.rb:15:in `<main>' shell returned 1 Press ENTER or type command to continue
On the other hand, Xamarin studio allows you to implement the interface with a single click. :
And if you have not implemented a single method in the interface, you won’t be able to compile. This is only great if you have small objects – I can’t imagine implementing the interface for ActiveRecord::Base class :).
IDE
I am a VIM user and we have auto-completion, CTags and other goodies, but what I found out is that IDE is a really powerful tool when you are developing with statically typed language. Because of the type system, it helps you to autocomplete your methods, implement interfaces and help you to write code in general. It is not something you can easily do in Ruby.
What do I miss when writing C#?
I really miss something like rails console, where you have absolute freedom to interact with the environment – even production. It can come really handy when you need to debug or evaluate problematic part of your code on the server, but I would need to mention that this is not something you can do on a mobile platform – even with Ruby.
Lessons learned
I do not think that language is so important, as long as you care for the craft of code and follow best OO practices as well as do Test Driven Development. C# is a great language with its benefits and drawbacks. The same goes for Ruby. Care for your craft and write awesome code!