I started my career as Ruby developer and I have not been in C++, Java (except for some Clojure) or C# land. In my company Diatom most of people are .NET developers and I was selling them Ruby and they where selling me C#. I learned to appreciate static typing, interfaces and other features along the way. In this post I wanted to share how I got in to 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 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#) and for each platform and the speed is almost the same as for the native approach. The idea seemed really cool to me and I asked if I could be of any help in mobile front. That's what got me started and now I can share my findings with you.
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 to call a method on the object as long as it responds to it. But with great power comes great responsibility.
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 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:
Ruby does not have explicit interfaces. We can always define 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 interface with a single click. :
And if you have not implemented single method in interface, you won't be able to compile. This is only great if you have small objects - I can't imagine implementing interface for something like ActiveRecord::Base class :).
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 mobile platform - even with Ruby.
Another thing I really find out missing is that there is nothing like interactive ruby console or irb for short (It is understandable why tough). As the beginner in C# I have to look up documentation every time I want to do some trivial expression. I was so used to irb, that I forgot how good it is for small experiments, where you would just need to try out some action on string or array.
On another hand there are really strong debugging tools available inside IDE's that help you and hold your hand.
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 it's benefits and draw backs. The same goes for Ruby. Care for your craft and write awesome code!