Skip to main content
edited body
Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90

Naming temporary variable is troublesome because [...] sometimes it costs time to read and understand a large block of code meanwhile it doesn't give any meaning.

Well, then give it some meaning. The name value doesn't describe much of anything. Instead, you could do something like

def some_method new_click = TopicLinkClick.create_from(new_params) return new_click unless new_click.nil? # do something else end 

You can shorten it a bit, but I wouldn't recommend it, as it's not quite as clear in my opinion

def some_method return new_click if new_click = TopicLinkClick.create_from(new_params) # do something else end 

Your other options are of course to not return early, but use a good old conditional instead

def some_method new_click = TopicLinkClick.create_from(new_params) unless new_click # do something else end end 

Alternatively, if you've got your code factored really nicely into methods, you can do

def some_method TopicLinkClick.create_from(new_params) || some_other_method(...) end 

Or, if you need a bit more logic, you can use a begin..end block, but again it's not as nice in my opinion.

def some_method TopicLinkClick.create_from(new_params) || begin # do something else end end 

Overall, though, your current approach is fine. Just use more descriptive names.

If you have a method that's simply a shortcut for calling either method A or method B, then I'd recommend using the method_a || method_b trick. Otherwise, like @schism, I'd recommend against doing too much on one line (as in code blocks 2 and 45)

Naming temporary variable is troublesome because [...] sometimes it costs time to read and understand a large block of code meanwhile it doesn't give any meaning.

Well, then give it some meaning. The name value doesn't describe much of anything. Instead, you could do something like

def some_method new_click = TopicLinkClick.create_from(new_params) return new_click unless new_click.nil? # do something else end 

You can shorten it a bit, but I wouldn't recommend it, as it's not quite as clear in my opinion

def some_method return new_click if new_click = TopicLinkClick.create_from(new_params) # do something else end 

Your other options are of course to not return early, but use a good old conditional instead

def some_method new_click = TopicLinkClick.create_from(new_params) unless new_click # do something else end end 

Alternatively, if you've got your code factored really nicely into methods, you can do

def some_method TopicLinkClick.create_from(new_params) || some_other_method(...) end 

Or, if you need a bit more logic, you can use a begin..end block, but again it's not as nice in my opinion.

def some_method TopicLinkClick.create_from(new_params) || begin # do something else end end 

Overall, though, your current approach is fine. Just use more descriptive names.

If you have a method that's simply a shortcut for calling either method A or method B, then I'd recommend using the method_a || method_b trick. Otherwise, like @schism, I'd recommend against doing too much on one line (as in code blocks 2 and 4)

Naming temporary variable is troublesome because [...] sometimes it costs time to read and understand a large block of code meanwhile it doesn't give any meaning.

Well, then give it some meaning. The name value doesn't describe much of anything. Instead, you could do something like

def some_method new_click = TopicLinkClick.create_from(new_params) return new_click unless new_click.nil? # do something else end 

You can shorten it a bit, but I wouldn't recommend it, as it's not quite as clear in my opinion

def some_method return new_click if new_click = TopicLinkClick.create_from(new_params) # do something else end 

Your other options are of course to not return early, but use a good old conditional instead

def some_method new_click = TopicLinkClick.create_from(new_params) unless new_click # do something else end end 

Alternatively, if you've got your code factored really nicely into methods, you can do

def some_method TopicLinkClick.create_from(new_params) || some_other_method(...) end 

Or, if you need a bit more logic, you can use a begin..end block, but again it's not as nice in my opinion.

def some_method TopicLinkClick.create_from(new_params) || begin # do something else end end 

Overall, though, your current approach is fine. Just use more descriptive names.

If you have a method that's simply a shortcut for calling either method A or method B, then I'd recommend using the method_a || method_b trick. Otherwise, like @schism, I'd recommend against doing too much on one line (as in code blocks 2 and 5)

Source Link
Flambino
  • 33.3k
  • 2
  • 46
  • 90

Naming temporary variable is troublesome because [...] sometimes it costs time to read and understand a large block of code meanwhile it doesn't give any meaning.

Well, then give it some meaning. The name value doesn't describe much of anything. Instead, you could do something like

def some_method new_click = TopicLinkClick.create_from(new_params) return new_click unless new_click.nil? # do something else end 

You can shorten it a bit, but I wouldn't recommend it, as it's not quite as clear in my opinion

def some_method return new_click if new_click = TopicLinkClick.create_from(new_params) # do something else end 

Your other options are of course to not return early, but use a good old conditional instead

def some_method new_click = TopicLinkClick.create_from(new_params) unless new_click # do something else end end 

Alternatively, if you've got your code factored really nicely into methods, you can do

def some_method TopicLinkClick.create_from(new_params) || some_other_method(...) end 

Or, if you need a bit more logic, you can use a begin..end block, but again it's not as nice in my opinion.

def some_method TopicLinkClick.create_from(new_params) || begin # do something else end end 

Overall, though, your current approach is fine. Just use more descriptive names.

If you have a method that's simply a shortcut for calling either method A or method B, then I'd recommend using the method_a || method_b trick. Otherwise, like @schism, I'd recommend against doing too much on one line (as in code blocks 2 and 4)