What are the Twelve Rules of Sinatra?

by on July 19, 2009

The Twelve Rules of Sinatra

The Twelve Rules of Sinatra: Download this as a Free Report.

Recently, I was reading Scott Adams’ (of Dilbert fame) blog post “Rule of Twelve” where he stated:

The Rule of Twelve states that if you know twelve concepts about a given topic you will look like an expert to people who only know two or three. If you learn more than twelve concepts about a topic, the value of each additional one drops off considerably. Allow me to be the first to confess that twelve is not a magic and inviolable number.

He also wrote a follow-up post to support his statement: “Twelve Rules of Energy Efficient Building“.

This made me wonder, could we apply the same “Rule of Twelve” to Sinatra?

Jeremy EvansHere is Jeremy Evans’ take on this:

  1. Just like Rails, keep your controller/actions simple, and put most of your business logic in your models. This makes testing and code reuse easier.
  2. Also like Rails, avoid excess logic in your views. Add helper methods that the views call to keep the views clean.
  3. Unlike Rails, read the Sinatra source. The main part is a single file that’s around 1000 lines of quite understandable Ruby code. Just reading it will probably make you a better programmer.
  4. If you have a problem that you think other people probably have (e.g. a Rails-like flash), look first for a Rack middleware that handles it, rather than recreating the wheel.
  5. Untested code will probably break sooner than later, so if you want the code to work in the future, write tests.

Well, Jeremy has set the ball rolling. What’s your take on this? Kindly post your thoughts as comments to this blog post. Looking forward to some interesting read.

Technorati Tags: , , , ,

Posted by Satish Talim

Follow me on Twitter to communicate and stay connected

{ 6 comments… read them below or add one }

Amr N Tamimi July 20, 2009 at 5:58 am

just got my hands dirty with Sinatra!! I i really love it! easy, clear and simple!!
Well, its for micro and small projects/sites. ummmm It’s cool!

Reply

Matt Todd July 21, 2009 at 8:42 am

RE: #3, I would certainly recommend reading the Rails sources too! Therein is some of the most well-organized and well-tested Ruby code as well as numerous examples of great Ruby code. It’s certainly a testament to how to organize a massive framework.

Sinatra itself really is a beauty in terms of code quality and clarity. It’s really impressive just how much you can accomplish with so little code in Ruby.

Reply

Julio Javier Cicchelli July 23, 2009 at 8:21 pm

Hi there everybody! I’ve been seriously thinking about this matter for the last couple of days and here I’ll leave you my conclusions about it:

1. Unlearn Rails: Sinatra doesn’t require you to understand the MVC philosophy or it doesn’t want you to learn any kind of convention. You’re free to decide how you want to write your Sinatra application.

2. Read the source code: Sinatra is a small framework and DSL written in 2000 lines of code so you don’t have any excuse for not reading it. I will help you understand how exactly Sinatra works, avoiding any magic.

3. Know Rack: Sinatra is a thin layer on top of Rack. The latter provides extreme flexibility and unprecedented extensibility that you can use as middlewares between your Sinatra application and the Web Server. Rack will unleash the power of your Sinatra apps, so use it wisely!

4. Test before you code: Misunderstood by most of the developers out there, Testing will assure your application does what it needs to be done by making you focus on its functionality, behavior or results. Ruby have made this very easy so please, write your test before you write your application using any of the available libraries (RSpec, Cucumber, Shoulda. etc.). Think of it as a long-term investment that you and your colleagues will benefit!

5. Master the REST concept: No other framework uses REST so easily as Sinatra but you should first focus on answer the following question: What’s REST? It’s not just unintelligent URL so please, read about it before start using it with your Sinatra application.

6. Use the Gems: probably the most underrated feature Sinatra provides, Sinatra can make use of the extensible gems without wrapping up as plug-ins.Of course, you can write and use Sinatra specific gems for very specific means but I will discuss that later.

7. Think of your project: Before start writing your test cases and the respective code, you should think of your application. Sinatra allows you to create virtually anything. Will it be a website, a web app, a service or something wicked? The answer to this question will let you focus on the development.

8. Befriend data support: Files, databases, caches, etc. Think of them as providers of the content and meta-content for your application. Ruby have gems that will interface any kind of data support and Sinatra/Rack will help you use them! Just be careful when uploading files from your app!

9. Deliver your ideas: Sinatra + REST will help you spread the content of your application easily. You just need to properly use one or multiple delivery formats, depending of the client that will get the content. It’s important to know how to use XML, JSON, HTML, YAML and other formats for your purposes.

10. Understand Javascript: If you’re going to build web sites or applications. Javascript will be the friend you require in order to make it fancy and interactive. Understanding Javascript is a huge advantage to build the front-end of your app. You should get to know further and add to your project libraries such as Prototype or JQuery. They’ll definitely add value to your project!

11. Turn into a Gem: Deployment can be done with libraries such as Capistrano or Vlad but I like the idea to package a complete solution within a gem. Learning how to wisely use the Rubygems library will let you master one of the required tasks of every Ruby programmer. Master this and you will be able to spread your application anywhere!

12. Help the Community: Sinatra is a relatively new framework and it is still ignored or misunderstood by most of the Ruby community. Undoubtedly, Rails is the king of the Ruby web frameworks but I think it’s just a matter of time and effort of the few Sinatra developers to show the rest of the world what Sinatra is made of. Your idea can have an impact in the Sinatra community so post your app on Github and share it! You will learn a lot from this experience.

Reply

Chris Strom July 24, 2009 at 6:34 am

1. Security – You are not a master of any web framework without a very solid understanding of web security. You should be familiar with the major attack vectors on the web (SQL injection, XSS, CSRF). You should understand how to mitigate or eliminate those attack vectors, both in principal and with Sinatra.

2. Rack – You do not need Rack when just getting started with Sinatra, but you definitely need to understand it, and how Sinatra fits into it, to become proficient with Sinatra.

3. Sinatra source code – It is lovely code and there is not too much of it. Read through it and learn a thing or two – about programming and about Sinatra.

4. Rails – Sometimes Sinatra is not the right choice. You should be familiar enough with Rails so that you might possess the wisdom to know when to use a particular framework.

5. HTTP – You should be comfortable enough with the HTTP protocol to run HTTP sessions from a telnet session. Every now and then, there is not substitute for a good telnet session when trying to troubleshoot.

6. REST – Rest is not strictly necessary for simple applications, but you should be very comfortable building REST-like APIs..

7. Ruby (enumerable, blocks, procs, lambdas etc.) – Maybe this goes without saying, but you cannot be a Sinatra master without first being proficient at Ruby. Read and understand the Enumerable documentation. Learn the difference between blocks and lambdas.

8. ORMs (besides ActiveRecord) – Chances are, if you develop enough Sinatra applications, you will need a backend data store. A master is familiar with several so that he or she is able to recognize the right tool for the job.

9. Testing (unit and integration) – If you are tossing together a prototype, you can do without testing. If you are doing anything
other than learning, you must test. Driving development by example (BDD) will produce simpler, more elegant design. A combination of unit and integration testing (e.g. Cucumber) will catch regressions. I didn’t say it first, but I repeat it as much as possible: robust, accurate, maintainable code is the goal. It is impossible to achieve that goal without testing.

10. Templating systems – You should have a good understanding of the templating options available to you (Haml, Erb, etc.)

11. 3rd party API gems – Sinatra is great for building interfaces to 3rd party applications, but you need to be familiar with gem libraries that interact with those applications. If there are no such gems – you ought to be comfortable rolling your own.

12. Solid foundation of web principles (sessions, cookies, javascript) – If you are using Sinatra for end-user consumption, you need to be competent with the technologies of the browser.

Reply

James Edward Gray II July 24, 2009 at 6:57 pm

I really think Sinatra makes understanding the value of REST a bit easier, just because it puts you a little closer to the requests with a few less helpers. That’s not to say it doesn’t help you get work done, but instead it’s just the right amount of help.

Reply

saurabh purnaye September 23, 2009 at 4:30 pm

1. Simplicity
Sinatra is very simple

get ‘/hi’ do
“Hello World!”
end

2. Very Powerful
Its simple but very good for creating web services.

3. RESTful
Its very RESTful

4. Easy Configuration
set :sessions, true
set :clean_trace, true
set :logging, false
set :dump_errors, false
set :some_custom_option, false

5. Easy Deployment
Its very easy to deploy Sinatra apps.

Reply

Leave a Comment

{ 23 trackbacks }

Previous post:

Next post: