0

I'm taking a bootcamp course and I know line 4 (zip_code = zip_code) isn't necessarily needed but I've been told it's useful for a simple reason, but I'm not sure what that is. Anyone know why? Thanks so much.

class AdoptADog::Scraper
  def self.scrape_dogs(zip_code)
    base_url = "https://www.petsmartcharities.org/find-a-pet-results?city_or_zip="
    zip_code = zip_code
    last_url = "&species=dog&color_id&geo_range=50&pet_size_range_id&sex&age=&breed_id=69"
    full_url = base_url + zip_code + last_url

    html = open(full_url)
    doc = Nokogiri::HTML(html)

    doc.css(".pet-result").each do |dog|
      name = dog.css(".pet-name").text
      breed = dog.css(".pet-breed").text
      sex = dog.css(".pet-sex").text
      location = dog.css(".pet-addr-city-state").text
      url = dog.css("a").attribute("href").value

      AdoptADog::Dogs.new(name, breed, sex, location, url)
    end
  end
end
5
  • 2
    It just looks like a readability thing to me. The full URL is defined in terms of three pieces, and there they are, in order. Commented Jul 27, 2018 at 22:39
  • 4
    there's no utility in assigning the same variable to the variable that's passed as an argument. Commented Jul 27, 2018 at 22:40
  • 6
    Actually, in Ruby, this is a really bad idea. If you forget to put zip code as an argument in the method definition, you won't get an undefined local variable or method error; instead, zip_code will just be nil. This is a poor practice. Commented Jul 27, 2018 at 22:43
  • 2
    I would be concerned when this code was written by a bootcamp trainer. Especially using string concatenation to build a URL is error prone. Furthermore, the code is long and complex enough to be extracted into several methods. Commented Jul 28, 2018 at 5:30
  • thanks for all the feedback... i wrote the code except for the zipcode = zipcode line Commented Jul 29, 2018 at 0:12

3 Answers 3

2

No, and the initial premise that it is useful is incorrect.

There is no functional reason for this, and I would argue against even the loose case one could make that it "increases readability".

This is pretty much bad practice in EVERY language.

The one and only possible reason for this would be to demonstrate variables to someone who is just starting to learn the core fundamentals of programming. Even that would be a bad example though, as it could be misunderstood to be good practice, when it most definitely is not, and there are FAR better ways to illustrate that without any risk of misconception.

Sign up to request clarification or add additional context in comments.

Comments

1

maybe zip_code = zip_code.dup ?, you should not change the passed params in your function.

Comments

0

Could it be that you missed .dup or .clone ?

something = something.dup can be useful if you work with mutable object and don't wanna mess with original one.

Anyway, if you have been told that it is useful for some reason, why don't you just ask that person to elaborate?

1 Comment

I have to research it and come up with an answer this week... I googled it and searched here, but couldn't find anything similar so I posted to get ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.