4 ms·
FWIW, I've been coding Ruby going on 7 years now (well over 100,000 lines of Ruby across all projects, I'd wager), and my memory estimates fewer than 5 instance
by Pistos2 17y ago
FWIW, I've been coding Ruby going on 7 years now (well over 100,000 lines of Ruby across all projects, I'd wager), and my memory estimates fewer than 5 instances of "undetectable typo". Generally, the typos I made and tried to run produced very obvious, visible and traceable symptoms (i.e. a precise and accurate stack trace).
I was asked in an interview once (by a skeptic that didn't know Ruby) if it weren't a problem that mistyping a variable name could produce a bug that would be difficult to detect and debug. I informed him that, in practice, it is essentially a non-issue.
There are two aspects of Ruby (among others, I'm sure) that help protect against the sort of hidden bugs you're worried about:
1) You cannot reference a variable that has not been defined yet. Example:
% ruby
supercalifragilisticexpialidocious = "something quite attrocious"
puts "I'm thinking of #{supercalifragilisticexpialodocious}."
^D
-:2: undefined local variable or method `supercalifragilisticexpialodocious' for main:Object (NameError)
2) Ruby will not silently or automatically convert or cast data types. (Where it seems to do so, in actual fact underlying library code is explicitly calling the conversion methods (#to_s, #to_i, #to_f, etc.)) Example:
% ruby
count_str = "8"
count = count_str.to_i
puts count < 9
puts count_str < 9
^D
true
-:4:in `<': comparison of String with 9 failed (ArgumentError)
Contrast that with, say, Perl:
% perl
use strict;
my $count_str = "8";
my $count = 0 + $count_str;
print $count < 9; print "\n";
print $count_str < 9; print "\n";
^D
1
1
Combine all of the above with good testing practices, and your bugs turn out to be quite detectable on the whole.
- eru 17y agoRuby seems similar to Python in this regard. (Though Python seems a bit more concerned about special stuff in strings.)
- axod 17y agoIt's not just about potential bugs though. It's readability, maintainability etc. For me, the original code pasted is horrible in terms of readability. Sure, it's concise, but it's lots of small characters that you could misread etc.
- jamesbritt 17y ago"Sure, it's concise, but it's lots of small characters that you could misread etc." To each his own, but have you spent much time coding Ruby and encountering this problem? I ask because it's not a problem I've had or hear from people who have been using the language. It seems to be be an issue expected by people who have not yet spent much time using Ruby (much like concerns over the lack of static typing). From a distance it may seems as though things would get confusing, but in actual practice this seems not to be the case. Could be a selection bias, but I would like to hear from people who have truly used Ruby in earnest and stopped because of repeated annoyances.
- Pistos2 17y agoCan you provide example code written in another language that accomplishes the same (element mapping) which you find easier to read or looks better? I'm not sure Java, PHP, Perl, C# or Python could offer an improvement, besides catering to a preference of keywords ("begin", "end", etc.) over non-letter tokens (parentheses, set braces, etc.). And string interpolation doesn't seem like it would be the source of either more or fewer bugs than string/variable concatenation. Also bear in mind that, in Ruby, almost all blocks can be delimited with a "do/end" pair rather than set braces.
- chromatic 17y ago> You cannot reference a variable that has not been defined yet. That helps somewhat, but you can certainly assign to a variable you haven't defined. Detect that. > Contrast that with, say, Perl.... If you'd used the warnings pragma, Perl would happily have warned you about any problems numifying the string. In this case, there are no problems numifying the string, so there's no warning. (As Perl is an operator-oriented language, the use of syntax which provides numeric or boolean or string context on values indicates programmer intent.)