How much would variable declarations in Ruby make you wince?

"MonkeeSage" <MonkeeSage@gmail.com> wrote in message
news:989bc966-6305-4149-a2b2-755558932688@o6g2000hsd.googlegroups.com...

    The two most popular sources of bugs for me when programming in Ruby
are:

1) Passing the wrong object as a parameter to a method.
2) Accidentally creating a new variable.

1.) How does predeclaration help?

var l = [1,2,3]
var h = {:a=>"b"}
wants_an_array(h) # => error

    It wouldn't help at all and no one claimed that it would. Did you read
my post?

2.) While that may alleviate the typo problem, it introduces another
class of bugs which are just as subtle:

var list = create_useful_list
if should_modify_list(list)
   # I meant to modify the variable "list" here...
   var list = modify_list(list)
end
use_list(list)

Once one became accustomed to typing "var", there is every chance that
it would be used places where a pre-existing variable was meant, but
the "var" just got typed out of habit.

    You don't really believe this, do you? Do you program in C/C++ at all?
Do you find yourself accidentally typing "int" when using a variable or
function in that language?
    Just because you need this keyword for declaring variables doesn't mean
you're going to get so used to typing it that you'll accidentally type it
out all the time. That's just silly...

···

On Dec 8, 6:51 pm, "Just Another Victim of the Ambient Morality" > <ihates...@hotmail.com> wrote:

What gets me, is that so many people respond to threads like this.
While other, much more reasonable ideas get so little attention.

T.

···

On Dec 11, 12:44 am, "Gerardo Santana Gómez Garrido" <gerardo.sant...@gmail.com> wrote:

On 12/8/07, Just Another Victim of the Ambient Morality<ihates...@hotmail.com> wrote:

[snip]

> What do you all think?

I think 47 messages (48th with mine) is A LOT. I don't think the
subject deserves such attention. There's already TOO MUCH NOISE in
this mailing list.

And I think I've got a solution for your problem. It's a crazy idea:

  what about if instead of bending a language to your preferences you
actually, you know, learn to type.

P.S. I wonder if these people are real. Sometimes I think they're just
[Perl|Python|Java] trolls testing our patience or wasting our time on
purpose.

"Gerardo Santana Gómez Garrido" <gerardo.santana@gmail.com> wrote in message
news:b06e22130712102144t73c2ab04p79f046f7fc7c3399@mail.gmail.com...

[snip]

    What do you all think?

I think 47 messages (48th with mine) is A LOT. I don't think the
subject deserves such attention. There's already TOO MUCH NOISE in
this mailing list.

    First of all, if you don't think the subject deserves any attention, why
pay it any?
    Secondly, why do you think there's too much "noise" in the mailing list?
Are you not capable of threading the discussions with your mail client? Do
you think there's too much noise _despite_ threading the discussions? I can
understand if you think there's noise but... too much noise?

And I think I've got a solution for your problem. It's a crazy idea:

what about if instead of bending a language to your preferences you
actually, you know, learn to type.

    That's about as helpful as responding to someone who's having trouble
with something by saying "grow a brain." It sounds like you're the one
making noise, here...

P.S. I wonder if these people are real. Sometimes I think they're just
[Perl|Python|Java] trolls testing our patience or wasting our time on
purpose.

    If you check Google Groups, you'll see that I've been here a some time,
now. I'm quite real and sincere...

If you are honest, JAVotAM, get a grip: that change you're thinking
of, won't happen.

    If you read the subject line for this thread, you might get an inkling
of how confident I am that this change will ever happen.
    I thought it's been an interesting thread. I don't see why it has
bothered you so much. If you don't like the subject matter, don't read it.
How hard is that? It seems that you're the one who's lost a grip...

···

On 12/8/07, Just Another Victim of the Ambient Morality > <ihatespam@hotmail.com> wrote:

Try out Index of /~eivind/ruby/types/

It adds a fairly flexible syntax for doing these kinds of checks,
about as compact as the above. You can find out if they are useful to
you or not. (It also allows more appropriate type checks, like
respond_to?)

Eivind.

···

On Dec 12, 2007 4:21 AM, Roger Pack <rogerpack2005@gmail.com> wrote:

> The two most popular sources of bugs for me when programming in Ruby
> are:
>
> 1) Passing the wrong object as a parameter to a method.
> 2) Accidentally creating a new variable.

I agree with number 1. I like duck typing and respect it, but I have
seriously thought of suggesting 'voluntary' type checks to Ruby before,
since it is a very common problem to pass a String instead of an
Integer.
I would have except I thought I might be bludgeoned by the community or
something. I still would use something like
def func_1(a ==> String, b ==> Fixnum)
end
if I had it. If only to ensure expected use at the beginning of coding
creation.

> > A solution to my problem would be to require variable declarations.
> > Something like the "my" keyword of a strict PERL script. Ruby would
> > probably not use "my," despite its PERL roots. Maybe "var?"

> I think it would be useful especially for people coming to ruby from
> non case sensitive languages. I certainly don't see any harm in
> setting up something like strict. If you don't want to use it you
> don't have to.

Once you make something like that optional it has a way of becoming
mandatory in a cultural sense. Go ahead, write a Perl program without
strict and try to get Perl community support. You'll be berated (and
probably insulted) within 2 seconds.

  For Perl that may not be a bad thing. And there's a lot loaded into
the words 'something like that.' Safety features? Optional
extensions? Ruby is one big collection of optional "somethings." I
don't think a *lack* of safety features should be culturally
mandatory, either.

No, the solution is to stop writing monolithic top-down scripts where
you can trip yourself up with such a mistake and/or write tests that
would catch something so simple anyway.

  This sounds like Trans's objection. But it's a post facto solution.
Relentlessly refactoring and testing during development could fall
into the 'premature optimization' category.

In all the years I've
programmed in Ruby I have never been bitten by the lack of strict
variable declaration.

That's just the thing, some of us get bitten. Please think of the
children.

Where it *can* be an issue is with instance variables.

I have issues with those, too.

  Cheerfully yours,

       -Jonathan

···

On Dec 9, 9:40 am, Daniel Berger <djber...@gmail.com> wrote:

On Dec 9, 3:27 am, "Tim Uckun" <timuc...@gmail.com> wrote:

>> So, I had a conversation with a colleague of mine and he brought up a
>> feature request for another language that is a lot like Ruby but is not
>> Ruby. It was an interesting request and, after I had thought about it a
>> bit, I discovered that I would like this feature, too!

> Hum. Why don't you try comp.lang.ruby.like.but.not.ruby instead? I
> hear it's "phat."

>> The two most popular sources of bugs for me when programming in Ruby
>> are:

>> 1) Passing the wrong object as a parameter to a method.
>> 2) Accidentally creating a new variable.

> Seriously, as someone prone to lack of concentration, I agree. A
> great many programmers have concluded, probably from painful
> experience, that typed variables reduce error.

Item 2 above was about declared variables, not necessarily typed ones.

       Sorry, that was intended as a précis. I probably should have
said 'typed and declared' so it would have applied to both.

> Also I think that
> building error-testing into the code will scale better than reliance
> only on unit testing.
> 1). I'm not yet convinced that some kind of sophisticated DBC thing
> amenable to duckies can't be created.

IMHO DBC and duck typing do not mix very well because DBC takes away the
flexibility which is one of the strengths. Fact is, apparently there
are people who are able to write complex applications in Ruby - even
with more smart automatisms added.

  The paradigms are antipodal, hence the conflict. But I *am not yet
convinced* that there is no acceptable middle ground between the
approach of trying to second-gues absolutely everything--which fights
the language--and the "you know it works because it works, that is,
doesn't blow up" approach. Permit me to think further on this.

> 2) would be helpful, but it may be a mistake to insist on a keyword.
> I think we should concentrate on putting everything that can be into a
> DUIIYDLI module ('Don't use it if you don't like it'). Then the
> skeptics can't complain.

That will we hard because you want the missing declaration be caught
during parsing and compiling. If you put this into a module then you
probably need to go back to the source and reparse it which is not
always possible. I don't know how Perl does it but frankly, I do not
really see Perl as the ideal example to model Ruby after.

  I don't need compile-time checking and would never ask to jam that
into the language. I was more imagining something modular that would
mimic a lvar. Don't care about Perl.

Bottom line: if you want a safer language with typed and declared
variables use one.

      Thanks, but I really like Ruby!

<snip>

I also suggest to keep methods
short. This will help improve structure of the code and avoid all sorts
of other bugs as well.

  Troo. But then you gotta test 'em all, because it's no help if you
have a lot of little methods but don't know which one the bug is in.

     Sincerely,

         -Jonathan

···

On Dec 9, 11:00 am, Robert Klemme <shortcut...@googlemail.com> wrote:

On 09.12.2007 16:11, Dumaiu wrote:
> On Dec 8, 7:51 pm, "Just Another Victim of the Ambient Morality" > > <ihates...@hotmail.com> wrote:

<snippy>

> > 2) Accidentally creating a new variable.

This is a danger that will plague any language, IMO.

    To be fair, it has to be said that it is less of a problem in
languages that require variables to be declared or typed.

<snip>

Todd

     -Jonathan

···

On Dec 9, 12:19 pm, Todd Benson <caduce...@gmail.com> wrote:

This comment caught my eye. I think good contracts are based on behavior
and not on implementation (as are good tests) and that would seem to
fit right in with duck typing vs. static typing.

Gary Wright

···

On Dec 9, 2007, at 11:05 AM, Robert Klemme wrote:

IMHO DBC and duck typing do not mix very well because DBC takes away the flexibility which is one of the strengths.

There are actually at least two measures that help preventing this: 1.
use an editor with proper syntax indentation and highlighting. 2.
Exercise the habit to enter matching pairs always together before you
enter the content (some editors will actually help with this as well
because they can enter the matching closing element when you enter the
opening element).

Kind regards

robert

···

2007/12/10, Marc Heiler <shevegen@linuxmail.org>:

> "The most annoying bug I ever have is tracking an errant "end"."

I too often complain about missing end's.
But I was one of those coders that, in PHP, often forgot a ';'

I cant tell you how happy I am with ruby. No more need for ';'

About the missing end's, over time I kinda developed a habit of
knowing where I made an error and can fix it up quickly.
It is not really that problematic for me. HOWEVER, I would actually
not mind some magical per-file way to omit end's based on
indent. :>

--
use.inject do |as, often| as.you_can - without end

"Robert Klemme" <shortcutter@googlemail.com> wrote in message
news:5s2hosF174p90U1@mid.individual.net...

Bottom line: if you want a safer language with typed and declared
variables use one. This is such a fundamental change to the language that
it does not fit in very well. I also suggest to keep methods short. This
will help improve structure of the code and avoid all sorts of other bugs
as well.

    Typed variables would be a huge change to Ruby but I disagree that
declaration would be. It's a small, simple change and a feature that many
languages have, including other dynamically typed languages and Ruby's
precurser (if one can call it that). It's a little against Ruby culture but
it's not hard to implement nor would it change the language much...

That'd be the wrong way, then.

Try testing it instead! (See my previous post about TDD vs static typing.)

···

On Mon, 10 Dec 2007 16:02:05 GMT, Just Another Victim of the Ambient Morality wrote:

How are you testing your code?

    The usual way: by running it?

--
Jay Levitt |
Boston, MA | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://www.jay.fm | - Kristoffer

    This is interesting but kind of overkill. Checking for a contract is a
non-trivial, especially if you want to keep duck-typing. This would
increase programmer effort so much that it would become comparable to
programming in C, I think...

Sure, but you asked :wink:

> Why do you have troubles like this? How big are your methods? Keep
> them small and this, I think, would be very rare.

    They're not that big. Having said that, it's often hard to keep a
method definition too small 'cause, well, sometimes there's a lot to do!
Sometimes there's a lot to do that's inherently related and splitting that
up would serve no logical purpose other than to keep the method definition
small. Finally, I've actually worked with entire programs where no function
body was more than six lines of code. It wasn't understandable nor easy to
work with. It was extremely difficult to tell what any function did and it
was difficult to modify code to do something else. I don't think
artificially keeping method definitions small necessarily solves anything...

Nah... Sure, a lot of small methods requires a little more tracking
around, but if they are named well and organized well the whole will
be much easier to work with and understand. The method names should
actually help self document the program, not obscure it. When you
divide a problem into small components it becomes much more manageable
and easier to test too.

> The most annoying bug I ever have is tracking an errant "end".

    I've had this problem, before, but luckily it's quite rare for me.
Perhaps it's because I've gotten into the habit of typing out my loop bounds
before filling in the loop body. I've always considered this a bad habit of
mine but perhaps it's not so bad...

Of course it is fairly rare (it usually occurs during refactoring, not
first write). But when it does happen I haven't found a better way to
track it down then to just look for it or comment out sections of
code, which sucks. Highlighting would help, but alas my editor does
not support it --even so, it wouldn't completely solve the problem.

T.

···

On Dec 10, 11:15 am, "Just Another Victim of the Ambient Morality" <ihates...@hotmail.com> wrote:

    It wouldn't help at all and no one claimed that it would. Did you read
my post?

[...]

    You don't really believe this, do you? Do you program in C/C++ at all?

[...]
That's just silly...

Tip: When proposing a fundamental, controversial, philosophy-shaking change
to the very people who would be making such a change, you're going to want
to sound nicer.

···

On Mon, 10 Dec 2007 16:38:18 GMT, Just Another Victim of the Ambient Morality wrote:

--
Jay Levitt |
Boston, MA | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://www.jay.fm | - Kristoffer

Sounds reasonable. However, I didn't mention the languages'
provenance, but that people continue to be interested in them. And
not just for static typing. People like... you, for example?

···

On Dec 10, 11:21 am, "Just Another Victim of the Ambient Morality" <ihates...@hotmail.com> wrote:

"Dumaiu" <Dym...@gmail.com> wrote in message

news:399c73dc-226e-4e6c-bda5-37104b24dd16@v4g2000hsf.googlegroups.com...

> On Dec 8, 7:51 pm, "Just Another Victim of the Ambient Morality" > > <ihates...@hotmail.com> wrote:
>> So, I had a conversation with a colleague of mine and he brought up a
>> feature request for another language that is a lot like Ruby but is not
>> Ruby. It was an interesting request and, after I had thought about it a
>> bit, I discovered that I would like this feature, too!

> Hum. Why don't you try comp.lang.ruby.like.but.not.ruby instead? I
> hear it's "phat."

>> The two most popular sources of bugs for me when programming in Ruby
>> are:

>> 1) Passing the wrong object as a parameter to a method.
>> 2) Accidentally creating a new variable.

> Seriously, as someone prone to lack of concentration, I agree. A
> great many programmers have concluded, probably from painful
> experience, that typed variables reduce error. Also I think that
> building error-testing into the code will scale better than reliance
> only on unit testing.

    I actually don't think this is the case. Statically typed languages
didn't come from long experience with bugs, it was just happenstance...
    If I'm not mistaken, typed variables came from a world where compiled
programming languages failed to completely shield the user from the
hardware. Back then, you had to declare variables so that the compiler can
know to allocate them from static store when creating the executable image.
Later on, when functions became more popular, they also had to do this for
the stack. A lot of C syntax comes from how close it is to assembly
language...

Thank you. That's a good suggestion.

          -J

···

On Dec 10, 7:33 am, Eivind Eklund <eekl...@gmail.com> wrote:

On Dec 10, 2007 12:10 PM, MonkeeSage <MonkeeS...@gmail.com> wrote:

> 2.) While that may alleviate the typo problem, it introduces another
> class of bugs which are just as subtle:

> var list = create_useful_list
> if should_modify_list(list)
> # I meant to modify the variable "list" here...
> var list = modify_list(list)
> end
> use_list(list)

> Once one became accustomed to typing "var", there is every chance that
> it would be used places where a pre-existing variable was meant, but
> the "var" just got typed out of habit.

This happens to me in perl, possibly about as often as the typo
problem in Ruby, possibly more. I'll note that I have a habit
intended to avoid typo problems: I fairly consistently use Vim's
"Ctrl-N" (complete based on string search in other parts of code),
even for short strings, just to avoid typos.

Eivind.

I don't really think it's equitable to liken Perl, where all you
do is use 'my', 'my', 'my', to C++, where the typing forces you to
really think about what you're instantiating. There is more room for
error in Perl.

···

On Dec 10, 11:38 am, "Just Another Victim of the Ambient Morality" <ihates...@hotmail.com> wrote:

"MonkeeSage" <MonkeeS...@gmail.com> wrote in message

news:989bc966-6305-4149-a2b2-755558932688@o6g2000hsd.googlegroups.com...

> On Dec 8, 6:51 pm, "Just Another Victim of the Ambient Morality" > > <ihates...@hotmail.com> wrote:

>> The two most popular sources of bugs for me when programming in Ruby
>> are:

>> 1) Passing the wrong object as a parameter to a method.
>> 2) Accidentally creating a new variable.

> 1.) How does predeclaration help?

> var l = [1,2,3]
> var h = {:a=>"b"}
> wants_an_array(h) # => error

    It wouldn't help at all and no one claimed that it would. Did you read
my post?

> 2.) While that may alleviate the typo problem, it introduces another
> class of bugs which are just as subtle:

> var list = create_useful_list
> if should_modify_list(list)
> # I meant to modify the variable "list" here...
> var list = modify_list(list)
> end
> use_list(list)

> Once one became accustomed to typing "var", there is every chance that
> it would be used places where a pre-existing variable was meant, but
> the "var" just got typed out of habit.

    You don't really believe this, do you? Do you program in C/C++ at all?
Do you find yourself accidentally typing "int" when using a variable or
function in that language?
    Just because you need this keyword for declaring variables doesn't mean
you're going to get so used to typing it that you'll accidentally type it
out all the time. That's just silly...

Although I don't think it is the case, if what Mr. Santana Gómez
Garrido said in his above post--which I presume he intended as a sort
of coup de grâce for the thread--is true, and the original poster is a
troll, then the real problem is that there are a bunch of us
respondents hanging around who are too stupid to know better. So you
really wouldn't want us participating in the 'more reasonable'
discussions, would you? The problem takes care of itself.
  If you still aren't convinced that we are a lost cause, by all means
recommend such a topic.

  -J

···

On Dec 11, 8:01 am, Trans <transf...@gmail.com> wrote:

On Dec 11, 12:44 am, "Gerardo Santana Gómez Garrido" > > > > <gerardo.sant...@gmail.com> wrote:
> On 12/8/07, Just Another Victim of the Ambient Morality<ihates...@hotmail.com> wrote:

> [snip]

> > What do you all think?

> I think 47 messages (48th with mine) is A LOT. I don't think the
> subject deserves such attention. There's already TOO MUCH NOISE in
> this mailing list.

> And I think I've got a solution for your problem. It's a crazy idea:

> what about if instead of bending a language to your preferences you
> actually, you know, learn to type.

> P.S. I wonder if these people are real. Sometimes I think they're just
> [Perl|Python|Java] trolls testing our patience or wasting our time on
> purpose.

What gets me, is that so many people respond to threads like this.
While other, much more reasonable ideas get so little attention.

T.

Yawn.

···

On 12/11/07, Just Another Victim of the Ambient Morality <ihatespam@hotmail.com> wrote:

    First of all, if you don't think the subject deserves any attention, why
pay it any?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Marc Heiler wrote:

About the missing end's, over time I kinda developed a habit of
knowing where I made an error and can fix it up quickly.
It is not really that problematic for me. HOWEVER, I would actually
not mind some magical per-file way to omit end's based on
indent. :>

I disagree, If you indent your code in the first place I find the
missing 'end's pretty easy to pick up. If you indent 2 spaces, pass a
block, backspace 2 and there is your end. I know it wouldn't be that
easy on larger applications, but the same idea still stands IMO. This
isn't Python.

1) Passing the wrong object as a parameter to a method.

So why not spend a couple of lines testing the object first?

2) Accidentally creating a new variable.

This can happen a lot no matter what language you code, I don't think
setting up the equivilant to Perls 'strict' would be the way to go, in
my opinion. But hey, I'm nothing but a humble Ruby programmer. :slight_smile:

Regards,
Lee

···

--
Posted via http://www.ruby-forum.com/\.

No, the solution is to stop writing monolithic top-down scripts where
you can trip yourself up with such a mistake and/or write tests that
would catch something so simple anyway.

This sounds like Trans's objection. But it's a post facto solution.
Relentlessly refactoring and testing during development could fall
into the 'premature optimization' category.

No. Optimizing almost invariably makes code more obscure and more
complex. Thus the reason to do as little optimization as possible,
as late in the project as possible.

The drivers for refactoring are completely opposite from those of
optimization.

The point of refactoring is to improve and simplify the design of
existing code. Refactoring tends to become more difficult the longer
it is put off, and is more risky in the absence of good test
coverage.

As such, it is ideal if refactoring can become a straightforward daily
occurrence; and a comprehensive test suite helps make this possible.
Some kinds of applications are harder to test than others, but it's
almost always worth it to find a way to write automated tests.

In any case, comparing frequent refactoring to premature optimization
is just wrong. . . .

Regards,

Bill

···

From: "Dumaiu" <Dymaio@gmail.com>