Best practice of writing Rails code

Hello guys,

I’m dealing with some issue about best practice of how to use Rails
code.
So, what’s the best way to write the following lines that basicaly does
the same thing?

Let’s say we have a link that needs to be a button with some parameters
(theme, size and icon)

  1. = link_to “Login”, “#”, class: “button”, data: {theme: “green”, size:
    “large”, icon: “small-arrow”}
    vs.
  2. = link_to “Login”, “#”, class: :button, data: {theme: :green, size:
    :large, icon: “small-arrow”}

both of them should generate something like this
Login

Personaly I prefer 1. because when it comes to maintanability is easier:
let’s say that we have to add a second class “login” to this link/button
and to change the theme to “dark-red”

  • adding the class: just click after the “n” letter after the word
    “button”, hit a space and write the second class “login”
  • changing the theme: double click on “green” word and write “dark-red”
  • adding the class: delete the “:” character, select the word “button”,
    wrap it in quotes, add a space and after that the class
  • changing the theme: select “:green”, add quotes and write the
    “dark-red” theme

I find the first version to be more faster to perform than the second
one -> improves mentainability

Other points could be:

  • using “:word” notation doesn’t work with spaces and dashes (other
    characters), but the “quoted string” notation works
  • keeping a single way of coding (the one that handles all the cases) ->
    more as a standard

So, what do you think? How do you code when you have something like
this?
I would like some opinions about this from some experts in coding Rails.

Thank you very much!

XPlay E. wrote in post #1064453:

I’m dealing with some issue about best practice of how to use Rails
code.

Rails questions should be raised on a Rails mailing list, but the
question you discuss is also just something about Ruby in general.

So, what’s the best way to write the following lines that basicaly does
the same thing?

Let’s say we have a link that needs to be a button with some parameters
(theme, size and icon)

  1. = link_to “Login”, “#”, class: “button”, data: {theme: “green”, size:
    “large”, icon: “small-arrow”}
    vs.
  2. = link_to “Login”, “#”, class: :button, data: {theme: :green, size:
    :large, icon: “small-arrow”}

both of them should generate something like this
Login

The difference is that :button is a Symbol, whereas “button” is a
String.

There is a small performance difference:

  • Symbols are singleton objects. Whenever you use :button in your code,
    the same (immutable) object instance is returned. The symbol persists
    indefinitely.

puts :foo.object_id
puts :foo.object_id # same

  • Strings are mutable objects. The literal “button” returns a new object
    every time it is executed. Try this in irb:

10.times { puts “foo”.object_id }

So the symbol version is technically more efficient where the same
symbol is being used over and over again. It is also quicker to test for
equality (two Symbols are equal if their object_id is the same, you
don’t have to compare them character by character). For this reason they
are often used as Hash keys.

In practice, the difference in almost all real programs is negligible,
so you should not get hung up over the performance issue. If you’re
talking Rails apps, almost certainly things like database accesses will
be many orders of magnitude slower.

As you have found, symbols which contain special characters need quoting
anyway, so
Symbol :“foo bar” is actually less convenient than String “foo bar”

So use whatever feels best to you, i.e. makes you more productive as a
programmer. Ten minutes of your development time saved is worth much
more than 10 microseconds of runtime.

Thank you very much for the explanation!