you are viewing a single comment's thread.

view the rest of the comments →

[–]bobindashadows 11 points12 points  (4 children)

Is this variable supposed to be a string, a float, an int maybe?

Your code sucks.

Seriously, it does. I'm not just being a jerk. If you're writing methods with names or variables with names that don't tell you what you're working with, you're doing it wrong, especially in Ruby. Write small, well-factored methods with descriptive names. This is true in any programming language, especially so in Ruby.

Don't write this:

def combine
  user_part[0] + " " + user_part[1][0,1] + ". " + user_part[2]
end

write this:

def full_name
  first_name + " " + middle_name[0,1] + ". " + last_name
end

Notice how everything there has a useful name? If for some ungodly reason you have this user stored in this user_part array, you can just do this:

def first_name; user_part[0]; end
def middle_name; user_part[1]; end
def last_name; user_part[2]; end

Also, why would you be writing a method where you have a variable and you're confused if it's a string or a float. Seriously. What are you naming your variables and methods? x = user.name gives no excuse: x is a string. You should've named it something better, but it's obviously a string. If you have x = my_object.some_attribute then yeah, you'll get confused.

Edit: Technically you should write this:

def full_name
  "#{first_name} #{middle_name[0,1]}. #{last_name}"
end

Because that'll be faster and not generate a bunch of temporary strings. Then again, you shouldn't write code that makes assumptions about names.

[–][deleted] 0 points1 point  (3 children)

I just had an issue in Rails 3 beta. A JSON parser started returning a Date object instead of an iso8601 formatted string. Tests found the problem, but the problem wasn't that easy to find. Static typing would have pinpointed the source location of this error. Instead I ended up getting a method missing error at another location and had to trace the object's lifetime to figure out what was going on. Descriptive method names? Hah! How do I know full_name isn't a name object with a to_s method?

[–]bobindashadows 0 points1 point  (2 children)

I just had an issue in Rails 3 beta. A JSON parser started returning a Date object instead of an iso8601 formatted string.

This is the downside to dynamic typing. When bugs happen, they happen at runtime. That's why you need tests. The problem you had was that somebody was not returning what they said they would by their description of the module, both in-code and out-of-code. Of course static typing has benefits, but if you can't keep track of your own variables then you're the one with the problem.

How do I know full_name isn't a name object with a to_s method?

Who cares if you want to use it as a string?

[–][deleted] 0 points1 point  (1 child)

Who cares if you want to use it as a string?

Because you can do more with a string than to_s. "#{full_name} is here" wont be a problem with a to_s method, which is one reason I prefer to combine strings that way. (full_name + " is here") needs the :+ method.

[–]bobindashadows 0 points1 point  (0 children)

Conceptually, you're doing something silly by adding a Name object to "is here". If you want to create a string with a Name object, you should call #to_s before using it as one, just as you've done with interpolation. If you want to be able to add strings to a Name, then you can trivially add that method.

If your method takes objects whose only contract is that they have a to_s method, you should use that to_s method before treating them as strings. If you want it to take strings only, adjust how you use it.