This could be a job for a template language like erb, but that might be overkill if the rest of the json data isn't very complicated.
I like the second form because it's more consistent, but I would put the if clause before the assignment, because IMO as part of the statement it causes confusion whether it's short-circuiting the whole assignment or just part an expression for what is being assigned.
My preference goes with this one as well. Declaring json_data = {} in advanced just isn't necessary for writing code that is easy to be able to follow along with.
A *very* seasoned software engineer, I wrote my first basic game, a lunar landing game, in Basic in 1969. Currently I am doing web development in Ruby on Rails, JavaScript, Elm.
This is my suggestion as well; it's extremely clear what is going on here, and I think this form conveys information to future maintainers quite well. The other suggestions are certainly doable. The tap form seems quite common, but if it's the only occurrence in your code base, it would likely be more confusing than helpful.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This could be a job for a template language like erb, but that might be overkill if the rest of the json data isn't very complicated.
I like the second form because it's more consistent, but I would put the if clause before the assignment, because IMO as part of the statement it causes confusion whether it's short-circuiting the whole assignment or just part an expression for what is being assigned.
Also if the functions are side-effect free, I would drop the
add_
from their names.I prefer this solution as well. It's simple and clear. Easy to understand.
My preference goes with this one as well. Declaring
json_data = {}
in advanced just isn't necessary for writing code that is easy to be able to follow along with.This is my suggestion as well; it's extremely clear what is going on here, and I think this form conveys information to future maintainers quite well. The other suggestions are certainly doable. The
tap
form seems quite common, but if it's the only occurrence in your code base, it would likely be more confusing than helpful.