Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Booleans Build a Simple Todo List Program Part 6: Printing Todo Lists

William Bode
William Bode
7,105 Points

Coding Challenge: is this a good answer, or lazy hacking?

Hey,

I solved the coding challenge for the "contains?" method like this:

def contains? return !!find_index(name) end

Now I am doubting whether this is a good application of DRY, or if I am abusing the find_index method?

1 Answer

A more elegant answer might be something like:

def contains?(name)
  find_index(name) ? true : false
end

where you are asking whether the find_index method can locate the name. The question mark's job is to basically pose a question to what comes before it - asking find_index what it will return. If it returns a truthy value (such as an index number, or almost anything that isn't nil) it returns the first parameter – true. If it gets nil from find_index, or some other falsey value, it returns false.

Sorry if you already knew about this, it's nice and tidy and seemed a good solution to this question.

William Bode
William Bode
7,105 Points

Thank you. I did not know about this before, and it looks very pretty. My question however was more on the general side - is it considered to be "good programming" to "abuse" functions like this, or will I run into trouble if I will continue this style?