The rise of billion dollar startups that get acquired in 6 months (or longer, perhaps) from launch gives a false impression that programming is easy, and hence shouldn’t take very long to learn. After all, you have a billion dollar idea you would like to start working on.
Let’s conduct a small study. Why don’t you spend an hour on StackOverflow? Why? To observe how users, without doing any research, ask the same questions over and over and over again. They just want to learn how this works, not why this works and not something else. I’m possibly guilty of the same mistakes, and I have been trying to amend them ever since.
You need this
As a result of these observations, and experience in teaching, I’ve come up up with a list of few qualities that can make you a better programmer:
Learner - Learning never stops for a programmer and that’s the fun of it. You will have to spend extra hours every week to brush up your skills, gain new skills, and get better at your trade.
Committed - It’s not easy to become a programmer. It will require a lot of commitment, practice, and more programming, of course.
Investigative - You may end up spending days trying to fix a Javascript memory leak, or why your Rails app is running slow and leaking memory. It will require a lot of investigation, research and thinking about every aspect of the application, and it’s behaviour. You will have to read API documents, source code, and so on.
Years ago, I was discussing some issue regarding GemCutter (now that makes
it ancient in programming age), and we were talking about global
variables in Ruby, for example, $; and $/. At the time, we couldn’t
really find a place to look them up, even Google isn’t very effective given
the nature of the query.
Anyways, while looking through Ruby’s standard library, I found the file
English.rb.
This library has English names for all the cryptic global variables. For
example: $ERROR_INFO represents $!.
If you ever have to look up the English names, which I suggest you do as
it makes code easier to read, just refer to that file.
With Ruby 2.1.0, one can easily trace the original exception.
Previously, on rescuing an exception one would have no reference to the original exception
(thrown by a gem/library). There are a couple of gems that can help you
keep track of the exceptions, but with Ruby 2.1.0 you can work with
nested exceptions without any issues. Here’s some trivial code to
achieve the same:
class Car
def self.start
begin
1/0
rescue => ex
puts "Exception: #{ex}"
raise StandardError.new "Can't start the car"
end
end
end
begin
Car.start
rescue => ex
puts "Cause: #{ex.cause}"
puts "Exception: #{ex}"
end
This will produce the following output:
Exception: divided by 0
Cause: divided by 0
Exception: Can't start the car
You can play around with the code yourself.
It’s not as sophisticated as the gems out there, but it’s getting there.
I was looking into Ruby’s TracePoint class recently. TracePoint is an objectified Kernel#set_trace_func method. TracePoint was
added in Ruby2, but before that there was a gem that had same function as TracePoint
class. Surprisingly, it was also called tracepoint.
Anyways, TracePoint is not the scope of this post. This post is all about AOP,
or Aspect Oriented Programming.
Wikipedia defines it as “aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. AOP forms a basis for aspect-oriented software development.”
There are couple of things worth noting, Modularity and Cross-cutting concerns.
Modularity
In English, Modularity means based on modules, easily assembled, or repaired
and the reason it’s easily repaired is because modules are
self-contained and talk to each other via a defined interface. Interface
could be hardware pins, RAM slots, or intangible ones, defined in your
Ruby or Java class.
In Ruby world, modules and modularity is the go-to thing to achieve
separation of concern. You got a piece of code that is used in two
different places and has no state of its own, just create a
module to be included/extended or prepended.
Cross-cutting concerns
Cross-cutting concern can be defined as any piece of code that’s more widely
used across the application, for example, logging, security, or authentication, perhaps.
Something, like a before_filter in Rails controllers that’s applied to a set of actions.
There are libraries that one could use to achieve same and even more
than before_filter functionality outside of Rails. The one that I
briefly looked at is called
Aspector. It provides a lot of examples
as well just in case you are stuck.
Why not just use Ruby Modules?
Ruby modules are similar but not exactly same as the AOP concept. One
important difference is that you can apply an aspect (aspect is the
piece of code with common functionality, like a module) to a class from
outside, without opening the class. Here’s some aspector code snippet to
elaborate the point:
TestAspect.apply A
Here A is the class, and TestAspect is the aspect. As you can see, you
can just apply it from outside. Sorry, not very clear, but I didn’t want
to tie the concept to a particular library implementation.
One good use case of using AOP concepts would be with something like
debugging, for example, a user performed an action and you want to check
the log for parameters that are getting passed in to methods, or what
methods are getting called when certain action is performed. But, that’s
what TracePoint does, right? Well, it definitely allows one to hook into
the events and print debugging information. With AOP, one can create
more focussed debugging. Imagine, a request going through Rails stack
will hit a lot of methods and you don’t want to enable tracing and then
having to go through a long console output.
These are just some of the initial thoughts I had on reading AOP and TracePoint.
Hope this post will encourage you to investigate and learn more about these topics.