Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop mutating input parameters #22

Merged
merged 3 commits into from
Dec 21, 2015
Merged

Stop mutating input parameters #22

merged 3 commits into from
Dec 21, 2015

Conversation

olefriis
Copy link

Mutating input parameters is bad practice.

Background info: I was trying to send an event where I was passing strings from ENV. That gave me the error can't modify frozen String (RuntimeError), as environment variables are frozen. I was a little bit shocked to see that dogstatsd-ruby actually mutates the input parameters.

Mutating input parameters is bad practice.
@olefriis
Copy link
Author

BTW, thumbs up for the great spec coverage in the project!

@olefriis olefriis changed the title Remove usage of "gsub!" on input parameters Stop mutating input parameters Sep 22, 2015
@@ -328,17 +319,16 @@ def format_event(title, text, opts={})

private

def escape_event_content(msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep imperative verbs for method names, i.e do_something rather than something_done ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. I wouldn't prefer this myself, but I'll respect your coding style.

The reason I don't think it should be escape_event_content is that the method does not escape anything in the passed String (it used to, but not anymore), but simply returns a new string with the event content escaped.

I'll change the method name back.

@yannmh
Copy link
Member

yannmh commented Dec 16, 2015

I think, indeed, mutating the input parameters was a bad idea. Thanks a lot for taking care of the fix @olefriis !

Besides the nitpick, your changes looks great. Let's merge it this week and release a new version.

@yannmh yannmh self-assigned this Dec 16, 2015
@olefriis
Copy link
Author

Hi @yannmh! Thanks a lot for your response. Nitpicking is appreciated!

I'll implement your changes right away.

end

def rm_pipes(msg)
msg.gsub! "|", ""
def remove_pipes(msg)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had renamed this method to without_pipes, since it doesn't remove anything from the passed String, but since @yannmh prefers imperative verbs, I decided to rename it to remove_pipes. (I know this is not the original method name, but personally I consequently avoid abbreviations in method names. @yannmh: If you disagree with this change, just tell me and I'll change it back to the original method name.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree, I like remove_pipes a lot more than its abbreviated version.

@yannmh
Copy link
Member

yannmh commented Dec 21, 2015

Thanks again for the changes @olefriis ! Let's release a new gem this week 🚢

yannmh added a commit that referenced this pull request Dec 21, 2015
Stop mutating input parameters
@yannmh yannmh merged commit 630cfbe into DataDog:master Dec 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants