Ruby: Prevent Multi-line Statement Bugs

Published in category Programming
on Christian Mayer's Weblog.

What’s wrong here?

if @foreign_id_opt.nil? && @unset_foreign_id_opt.nil?
  @name_opt.nil? && @description_opt.nil? &&
  @estimation_opt.nil?
  
  raise TaskCommandError, "No option given. See 'timr task -h'."
end

Straight at the end of the first line there is an AND && missing. The second and third line is pointless. They are not part of the if condition.

To prevent such a mistake append a backslash \ to each line of the multi-line statement:

if @foreign_id_opt.nil? && @unset_foreign_id_opt.nil? && \
  @name_opt.nil? && @description_opt.nil? && \
  @estimation_opt.nil?
  
  raise TaskCommandError, "No option given. See 'timr task -h'."
end

See the original issue on Timr@41e9cd commit.

Recent Posts

About the Author

Christian is a professional software developer living in Vienna, Austria. He loves coffee and is strongly addicted to music. In his spare time he writes open source software. He is known for developing automatic data processing systems for Debian Linux.