Rodrigo Vieira

Perspectives & thoughts by a humble Software Developer

Getting started with Clojure

I‘ve been playing with Clojure for a little while and, although it seems very new to me (due to its Lisp ancestry, and I come from a Ruby and JavaScript background), I’m eager to learn it and to start building software with it. This post is a guide and overview of the language for beginners.

First, Clojure runs on the JVM. Yes, it means you can use Java libraries (including the standard library) from inside your Clojure projects, pack them as jars, etc.

REPL

Clojure comes with a read/eval/print loop tool called clj that lets you evaluate code instantly. Unfortunately, the REPL doesn’t come with some handy features like historic and tab completion by default, but you can add these via IClojure.

I couldn’t resist the idea of aliasing the REPL as clojure to call it via the command-line, so here’s what I did:

Leiningen

There’s a build and automation tool for Clojure projects called Leiningen, or lein. Leiningen may be used for open source and private code. It’s widely adopted throughout the community and is very mature.

Clojure open source projects are often shared as Java Archives in the Clojars repository.

A project using lein must have its specification defined within a file called project.clj similar to a package.json in Node.js projects or to a .gemspec file in a Ruby Gem.

Here is a sample project.clj from Leiningen’s README:

There was also another build tool for Clojure called cake. But apparently it has been discontinued.

ClojureScript

ClojureScript is a compiler for JavaScript, it’s intended to generate highly optimized JS code, based on Google Closure compiler. It can also target other JavaScript environments other than browsers like Node.js.

The project’s GitHub Wiki has a Start Guide with detailed information about the compilation methods and options.

A more in-depth look at ClojureScript is provided by this blog post by Stuart Sierra on the Clojure Blog.

Web Development

The current de facto web framework for Clojure is Noir. Noir is very straightforward and easy to use, it also seems to be inspired in Ruby’s Sinatra so if you come from Ruby you’ll have a good time using it.

Alternatively, composure is another web framework that apparently was inspired by Sinatra, too.

Also, there is clj-http, a HTTP library with a very concise API. If you want to send request to web pages, get response and etc, I highly recommend you to use it.

ORMs

Clojure has some good ORMs, at the time of this writing, there are already mature libraries for connection to many databases:

Of all these, Korma is the one that amazes me more. What a beautiful API it provides! Anyway, if you think that some adapters are missing, remember that you can use a library written for Java such as the riak-java-client by Basho.

Concurrency

You may already have heard about how Clojure approaches concurrency. You may have heard good things ;). In fact, one of the goals of the language is to offer full multi-threaded programming ability, via its functional programming techniques and other features like Atoms and Refs.

IBM is got a very complete article on Clojure’s concurrency model.

Deployment

Heroku announced, several months ago, that it runs Clojure apps. Also, remember that Clojure projects can be easily packed as jars thus it can easily run on Google App Engine. In fact, whenever JVM is available, Clojure may run.

More

If you read this post until here, then I assume you’re kind of excited with Clojure, too. So I have here some links to resources on the Internet that will help you:

I hope this blog post not only left you enthusiastic about Clojure but also helped you get started with the language and its ecosystem. See you in the next posts, I’ll write more about Clojure!

My Chief's answer to my post on Generalism

Yesterday I wrote a post about Generalism on Software Development, that was a reply to an argument of my chief and coworker. I wrote the post, sent it to him and asked for his answer, and there it is, as promised:

There are differences between big and small companies. The kind of professional that, in this moment, we need everyone (say, people working on the company) to be is someone adaptable and open to deal with various kinds of technologies and techniques.

If we had structure (we, Atela, are a startup), we’d hire a marketeer, a salesman, etc. But as we don’t have, we struggle to fill – even slightly – this gap.

Someone who, in small companies, works with the Web and don’t know CSS, is fated to projects with serious delay and time loss, for he/she won’t be able to change an alignment when necessary. Besides, it’s a programming language (whether you like it or not), and has not to do with Design. HTML/CSS is the technical translation of the layout. Even another coworker of ours (a Computer Engineer) is studying HTML/CSS/JS. You’ll still have to pass to many paradigms, get away from the (programming) languages and see many other concepts in order to become a Computer Scientist, and this development is part of your internship. If we wanted someone who can only work with Ruby or only JavaScript, with no quality as a “snooper”, generalist or proactive, we’d have other options than you.

The good engineers learn calculations and science that they’ll never use in their lives, because there is already software giving stuff for them, for free, but they can make it work without the existing software.

On Software and Generalism

Today I had a brief discussion with my coworkers/chiefs regarding the importance of generalism in a Software company and/or to an individual developer. For example, let’s say you develop web applications: you write Python code that deals with the database and the server, sends the HTML to the browser when the request finishes. You also write the HTML itself and the JavaScript for the AJAX-based features. Should you also write the CSS, know how to place the elements, set the backgrounds of the pages, know what grid system to use?

My short answer is no. Generalism isn’t a requirement, nor should it be.

The main related question is: Should software shops and software developers be generalist? My answer is neither. For this very reason, there are (great) software shops that take projects and do not design it, instead, they outsource this part of the work. In the same way, we shouldn’t expect web designers to write Python code, should we?

Analogically, if you have a Civil Engineering firm, should you expect your engineers to also design (even partially) the buildings? Could you even expect them to know what are the best windows, doors and whether the main entrance should have a Ancient Greek statue or not? A Civil Engineer must be good at Architecture, right?

Well, I don’t think so.

The argument I heard was*:

For a good Web Company, it is impossible for one not be a generalist. CSS may get you stuck sometimes, but there is not Web without it, so in order anyone of us (company members) to be able to deploy a website, we have to know how to build whatever is necessary, even if not in the most efficient way…that’s another history. So you can’t escape it.

Again, the argument sounds right to me except for the fact that it presents Generalism as a requirement, which I refute.

In a software company, although not as a requirement for existing, design may be expected to be present, but in a situation where there are dedicated professionals for Design and Programming matters, as you’d expect a Civil Engineering firm to get a building built and designed when there are Engineers and Architects.

When we go the the individual side, it just sounds implausible to me. To claim that a developer should know Python and JavaScript, and also CSS, for example.

This discussion also reminds me of a tweet by Chris Dixon where he stated:


I think there is no problem for a Programmer to know Web Design, or to master it. But he/she must want it, as it is a plus, not a basic/must-have skill.

  • This argument was translated from Portuguese and I think it’s a precise translation. Anyway, my chief and coworker (author of the argument) has already been notified about the post and invited to read and reply (I promise I’ll post his answer).

UPDATE

He has already replied to this post.

Now at the University

I’ve been programming for almost 3 years now. Until now, it was all about “autodidactism”, as I never participated in any course related to programming. Actually, no course at all.

TL;DR

This is not a technical post.

I’ve been working professionally for one year and a half now, and sometimes I feel the lack of formal education. My coworkers notice that too, and gently report to me.

I recently submitted to a selection process of a Brazilian Federal University (UFPE) for their Computer Science program/course, for the second time. In Brazil, the best Universities are usually the state ones. Well, this time I was approved! And I’m excited and anxious to begin! Seems like a great opportunity to learn the building blocks of Computing, among many Mathematical fields.

I must express my gratitude for Atela, the company I work for for giving me full support while I was preparing myself to the tests. Especially my chief and coworker, André Braga, who even let me stay totally away from work during the very week before the final tests, and at that time (November 2011) the company had several projects to deliver.

I think the University is going to help me. Filling my knowledge gaps, at least some of them. The classes for me only start on August, but I’m so eager for them already!

Mutate method visibility on the go in Ruby

Ruby is extremely flexible – for programmer´s joy or madness – it lets you change almost anything at will. Well, method visibility too.

Look at the following code snippet:

In the ShamelessSon class, we told Ruby to mark the bank_account method as public, making it available to this class’ instances to use, in this case, dillinger.

Needless to say, this technique is dangerous. Use it wisely, only redefining visibility of the methods from the classes that you know and trust.

Turning a file into an array of lines in Ruby

Getting lines of a file stored in an array might be something very useful. In Ruby you can do this with admirable ease with the basic IO class:

That’s it. So simple and beautiful, yet powerful.

The most beautiful Ruby factorial method

Ruby sometimes let you write beautiful code. I think I’ve come to one of those moments today implementing a factorial method. As you’d expect, this method takes a number as argument and return it’s factorial. Ohhh

Seriously, isn’t it beautiful?

Ruby Method Visibility Alternative Notation

Programmers have the recurrent necessity to control access to methods and variables in their code. In Ruby we generally do it all by declaring the keywords public(not often, though), protected and private before method definitions. The file below illustrates what I’m talking about:

Here the second and third methods are made protected and the last two ones, private. The first method is public, we don’t need to specify that because it’s not preceded by any access declarator and defaults to public.

There is nothing wrong with the approach taken in the GreekAlphabet class, however there is another notation for access declaration in Ruby that sometimes may be easier to read:

Notice that we did not pass the alpha method to neither protected nor private and so it’s public.

A final note

Actually, the two notations aren’t two different things. In fact, these three functions work by setting their visibility state to all the subsequently defined methods, or to those passed directly to it as symbols. A similar behavior may be observed in the method Module#module_function.

Generating XLS files in Rails

Often we want to export data from our Rails apps in the form of external (and downloadable) documents. If XLS (Microsoft Excel) is the format you need, a gem called to_xls lets you do that without too much effort. Let’s get started!

We must precede everything adding the gem to the Gemfile:

Then, tell Rails that you need another Mime Type for the XLS files:

Now we start having fun. In the reports method, we define how our (XLS) document should look like. Since we probably don’t want all the attributes of our objects to be displayed, we pass to the columns option only those that we want in the file:

Use the headers option to specify the text that we want the headers to display, as shown in line 10 of the file above.

After setting our document’s content, we pass it to the send_sata method so the browser will download the file when a request to the XLS format is performed.

Also, pass the filename option to customize – guess what – the filename. In this code the filename should look like Berkeley-reports-20-12-2011.xls.

Now let’s go to our view and add a link to download the report:

Done! Now go generate some XLS!

Use Ruby to find out some file's encoding

Sometimes you may wanna know how a file is encoded. You can quickly accomplish that in Ruby by creating a file named, let’s say, encoding.rb with the following content:

And use it like this:

 $ ruby encoding.rb file1.rb file2.js file3.html 

If you like things the hackish way you can instead name the file without the extension and:

 $ chmod +x encoding 

and now:

 $ ./encoding file1.rb file2.js file3.html 

This may be useful. See you in the next post!

UPDATE (December 21, 2011): I released a stupid small gem that works as a command-line tool, check it out.