Syntax "surprise"

I don’t know if this qualifies as a real “surprise”, but it was to me. I had thought this syntax would work:

opts = GetoptLong.new(
[ “–verbose”, “-v”, GetoptLong::NO_ARGUMENT ],
[ “–yesterday”, “-y”, GetoptLong::NO_ARGUMENT ],
)

Now that fails… but the following works:

opts = GetoptLong.new(
[ “–verbose”, “-v”, GetoptLong::NO_ARGUMENT ],
[ “–yesterday”, “-y”, GetoptLong::NO_ARGUMENT ]
)

It’s the one solitary COMMA at the end that did it. I remember reading in my Perl books that they allow the oddity of an extra comma at the end so one can cut and paste over and over and not have to strip that extra annoying comma. I thought Ruby did that, but not here…

Why not?

David Douthitt
CUNA & Affiliates
UNIX Systems Administrator
ddouthitt@cuna.coop

David Douthitt wrote:

opts = GetoptLong.new(
[ “–verbose”, “-v”, GetoptLong::NO_ARGUMENT ],
[ “–yesterday”, “-y”, GetoptLong::NO_ARGUMENT ],
)

Now that fails…

Ugly workaround using array expansion:
opts = GetoptLong.new(*[
[ “–verbose”, “-v”, GetoptLong::NO_ARGUMENT ],
[ “–yesterday”, “-y”, GetoptLong::NO_ARGUMENT ],
])

But I’d have to argue against allowing trailing commas in method calls
(and definitions), on the basis that it could make it easier to write
methods that take a lot of parameters. IIRC, a large number of
parameters to a method is a “code smell”, and allowing the trailing
comma would be like spraying some fresh pine scent on it to cover it…
IMHO.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Kent Dahl kentda@stud.ntnu.no writes:

But I’d have to argue against allowing trailing commas in method calls
(and definitions), on the basis that it could make it easier to write
methods that take a lot of parameters. IIRC, a large number of
parameters to a method is a “code smell”, and allowing the trailing
comma would be like spraying some fresh pine scent on it to cover it…
IMHO.

There’s also an ambiguity:

do_something 1,2,3,
puts “done”

Do we call do_something with 3 parameters, or pass the nil result from
puts as well? You’d only be able to have trailing commas with
parenthesized parameter lists.

Cheers

Dave

Hi,

Ugly workaround using array expansion:
opts = GetoptLong.new(*[
[ “–verbose”, “-v”, GetoptLong::NO_ARGUMENT ],
[ “–yesterday”, “-y”, GetoptLong::NO_ARGUMENT ],
])

Wow, I was just writing the same workaround. You’re a mind reader.

But I’d have to argue against allowing trailing commas in method calls
(and definitions), on the basis that it could make it easier to write
methods that take a lot of parameters. IIRC, a large number of
parameters to a method is a “code smell”, and allowing the trailing
comma would be like spraying some fresh pine scent on it to cover it…

Wow, in fact, it used to accept trailing comma in argument list, but I
removed that syntax rule for the reason you described here.

						matz.
···

In message “Re: Syntax “surprise”” on 02/07/18, Kent Dahl kentda@stud.ntnu.no writes:

Much as I love Ruby over Perl, this is one feature of Perl that I
thought was great and I wish Ruby copied it. It makes it easier
eg. to swap arguments around if you realise the order is wrong. So in
some ways this feature makes it easier to get your code smelling
sweet.

Regards,

Jeremy Henty

···

In article 3D358854.2E032977@stud.ntnu.no, Kent Dahl wrote:

… I’d have to argue against allowing trailing commas in method
calls (and definitions), on the basis that it could make it easier
to write methods that take a lot of parameters.

   do_something 1,2,3,
   puts "done"

pigeon% ruby
def do_something(*arg)
   p arg
end

do_something 1,2,3,
puts "done"
^D
done
[1, 2, 3, nil]
pigeon%

pigeon% ruby -v
ruby 1.6.7 (2002-03-01) [i686-linux]
pigeon%

For the original message, this is a difference 1.4 / 1.6

Guy Decoux

Still, its a shame ruby adopts the same shortcoming as java.

Its not a language’s business to prevent users from writing “smelly” code.

Long argument lists are common in data initialization contexts, and
misplaced commas a extremely common problem, that’s actively
manufactured by this rule.

@@instance = SomeClass.new(
	"127.0.0.1",
	8083,
	"hostname.com",
	blah,
	blah,
	whoops,
);
···

At 12:44 AM +0900 7/18/02, Yukihiro Matsumoto wrote:

Hi,

In message “Re: Syntax “surprise”” > on 02/07/18, Kent Dahl kentda@stud.ntnu.no writes:

Ugly workaround using array expansion:
opts = GetoptLong.new(*[
[ “–verbose”, “-v”, GetoptLong::NO_ARGUMENT ],
[ “–yesterday”, “-y”, GetoptLong::NO_ARGUMENT ],
])

Wow, I was just writing the same workaround. You’re a mind reader.

But I’d have to argue against allowing trailing commas in method calls
(and definitions), on the basis that it could make it easier to write
methods that take a lot of parameters. IIRC, a large number of
parameters to a method is a “code smell”, and allowing the trailing
comma would be like spraying some fresh pine scent on it to cover it…

Wow, in fact, it used to accept trailing comma in argument list, but I
removed that syntax rule for the reason you described here.

  					matz.


Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
o For industrial age goods there were checks and credit cards.
For everything else there is http://virtualschool.edu/mybank
o Interactive Learning Environment http://virtualschool.edu/ile
o Java Web Application Architecture: http://virtualschool.edu/jwaa

report.define(0, {
:column => ‘ABC’,
:label => ‘ABC_D’,
:width => -5,
})

This is an actual code snippet from a test case of mine.

– Dossy

···

On 2002.07.18, Jeremy Henty jeremy@chaos.org.uk wrote:

In article 3D358854.2E032977@stud.ntnu.no, Kent Dahl wrote:

… I’d have to argue against allowing trailing commas in method
calls (and definitions), on the basis that it could make it easier
to write methods that take a lot of parameters.

Much as I love Ruby over Perl, this is one feature of Perl that I
thought was great and I wish Ruby copied it. It makes it easier
eg. to swap arguments around if you realise the order is wrong. So in
some ways this feature makes it easier to get your code smelling
sweet.


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Much as I love Ruby over Perl, this is one feature of Perl that I
thought was great and I wish Ruby copied it. It makes it easier
eg. to swap arguments around if you realise the order is wrong.

Nah. Maybe I’m a masochist, but I reckon if you do something stupid, it
should be HARD to fix, so you remember and don’t do it next time … or at
least the time after that :-).

ts decoux@moulon.inra.fr writes:

do_something 1,2,3,
puts “done”
^D
done
[1, 2, 3, nil]

Agreed - the poster though was asking for trailing commas to be
permitted in argument lists

do_somthing(1,2,3,)

would be a call with three parameters. I was suggesting that if this
was allowed, it could only work if the parameter list was
parenthesized, as there would otherwise be the ambiguity shown above.

Dave

Its not a language’s business to prevent users from writing “smelly” code.

Um, right. When you believe this, you end up with Perl.

I love Perl dearly, but there’s a reason why I’ve stopped doing any
new development in Perl and everything in Ruby now. I don’t think
a language should constrain you from being able to do things, but I
don’t feel constrained by Ruby at all. If the constraints keep
people from writing smelly code – which I may, some day, have to
interface with, read, understand, change or modify – but the
constraints don’t feel constraining to me … I can only see that
as a Very Good Thing.

Long argument lists are common in data initialization contexts,

Bad code is common. You’re very right.

I recently took an #initialize in a class that took 5 args
(4 Arrays and one Hash) and refactored it a few times so that
the constructor only took one Array of Hashes (to be specific,
all of the DBI::Row objects for the result set of a SQL query
via DBI).

Then, I added methods to the class to set the various other
attributes – anything “unset” got a sane default value, so
I only had to “override” things that needed to be changed,
as to explicitly setting them in the other 4 Array args as
I was doing before.

The design is much cleaner, certainly easier to read and
understand and is far less smelly. It’s still smelly (there’s
a strong feeling of duplication that I’m trying to eliminate)
and I think it’s time to introduce polymorphism or someother
such refactoring soon, but I’ll do that when I write the next
test case that feels difficult to implement because of the
way things currently are.

and misplaced commas a extremely common problem, that’s actively
manufactured by this rule.

Yes, and misplaced commas are more often a mistake than an
intention. So, I prefer the language to at least warn me
that I’ve done it – making it a fatal error isn’t a big deal,
anyway.

How hard is it to find a misplaced comma when the program won’t
even run because of it? Trivially easy to find.

How hard is it to track down a bug because of a misplaced comma
which the language finds perfectly acceptable and within the rules?
Pretty damn near impossible sometimes, especially if you’re tired.

If you’re really desperate for having "optional trailing commas"
the Array expansion trick described earlier in the thread gives
it to you with no change to the language itself. Wrapping the
arg. list in *[] is a way to express the intentional desire to
support an optional trailing comma which is good. Having that
behavior by default is too magical and will only cause more harm
than good when people make honest mistakes.

– Dossy

···

On 2002.07.18, Brad Cox bcox@virtualschool.edu wrote:


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

C allows a trailing comma in initializers:

int foo[] = {1,2,3,};

The justification (iirc) was that it made it easier to write programs
that generate C language statements.

···

On Wed, 17 Jul 2002 16:42:09 GMT, Brad Cox bcox@virtualschool.edu wrote:

Still, its a shame ruby adopts the same shortcoming as java.

Its not a language’s business to prevent users from writing “smelly” code.

Long argument lists are common in data initialization contexts, and
misplaced commas a extremely common problem, that’s actively
manufactured by this rule.

@@instance = SomeClass.new(
“127.0.0.1”,
8083,
hostname.com”,
blah,
blah,
whoops,
);

Brad Cox wrote:

Its not a language’s business to prevent users from writing “smelly” code.

I strongly disagree. Improvements in structure that higher-level
languages bring along have for the most part been about removing the
need for “smelly” code. (Ok, so perhaps goto wasn’t referred to as
"smelly", but it stank nonetheless)

In the extreme case of this, I even like Java for its sandboxing
capabilities. No, not the security-wise ones, but the sandbox and
confindments that novice programmers can wallop around in like newborn
babies, but still enforce some structure (such as only one public class
per sourcefile etc). Sure, Ruby is alot more pragmatic, practical and
lax, but think you’re talking perlish pragmatism here. :slight_smile:

Long argument lists are common in data initialization contexts, and
misplaced commas a extremely common problem, that’s actively
manufactured by this rule.

I find long argument lists to be an abomination. A problem I fall into
much more frequently than missing a comma, is ordering and remembering
them all. The current way Ruby automagically makes a hash at the end may
not be the best solution, but I have spent more time on stupid mistakes
using libraries with many arguments (that didn’t allow the hash variant)
than I do on finding back to the parse error of a missing comma, which
tends to be pretty accurately pointed out. (And no, the parse error
won’t be lingering for a long time before it rears its ugly head if you
have thorough unittests.)

I’d also dare to go out on a bit of an extreme limb as to claim that the
non-keyword comma separation of arguments is a fundamental flaw of many
programming languages, for there is no linear, sequential structure at
work here, like with arrays(for which comma trailing is allowed). The
linearity is a throwback to compiler-feeding.

On the other hand, I’m also not fond of the “creative” diversity that
keyword arguments bring, where the ordering is completetly up to the
programmer. The possibility of leaving out optional parameters in the
middle of the argument list is neat. Perhaps it is false to say that
there is no linear structure in a method call. It might be closer to the
truth to claim that the argument list linearity often fails to map to
the “storytelling” linearity when there is alot of arguments. I’m not a
SmallTalker but here I think SmallTalk was onto something, with a
defined order of keywords that both separate and describe the arguments,
making a very readable story.

I can’t say the same for:

    @@instance = SomeClass.new(
            "127.0.0.1",
            8083,
            "hostname.com",
            blah,
            blah,
            whoops,
    );

I just love how Ruby does the Right Thing in alot of cases here, (such
as File.open which is more readable than File.new) but it doesn’t
currently extend into argument lists. When keyword argument design is
finalized, I prophetize it will.

(I cut this rant short here, as Dossy posted a reply that has the
remainder of what I had on mind, but alot shorter and neater than I
could write it.)

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 4. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Oops! Brad Cox bcox@virtualschool.edu was seen spray-painting on a wall:

Still, its a shame ruby adopts the same shortcoming as java.

Its not a language’s business to prevent users from writing “smelly” code.

Long argument lists are common in data initialization contexts, and
misplaced commas a extremely common problem, that’s actively
manufactured by this rule.

@@instance = SomeClass.new(
“127.0.0.1”,
8083,
hostname.com”,
blah,
blah,
whoops,
);

If you have a procedure with ten parameters, you probably missed some.
– Alan J. Perlis

Common Lisp’s :key parameters (that can have defaults) are an
interesting approach to the problem.

I seem to recall Objective C having a pretty nice approach to it,
too. (Somehow your name tickles some memories concerning ObjC… You
didn’t have some involvement with it, did you?)

If there are THAT many parameters, I think it’s critical for them to
be named otherwise it starts being a bit like programming in Morse
Code, Dit-Dah-Dah-Dah Dit Dah-Dit, with lots of risk of pieces getting
out of sorts.

And at that point, Perlis isn’t just right, he’s ferociously right…

···


(concatenate 'string “cbbrowne” "@ntlug.org")
http://www3.sympatico.ca/cbbrowne/languages.html
Rules of the Evil Overlord #44. “I will only employ bounty hunters who
work for money. Those who work for the pleasure of the hunt tend to do
dumb things like even the odds to give the other guy a sporting
chance.” http://www.eviloverlord.com/

Well, I’m something of a perfectionist, so I don’t need the language
to punish me for stupidity. I do that perfectly well myself. :slight_smile:
More seriously, not all mistakes are stupid. Something that makes it
hard for me to correct my mistake is stopping me from going on to
making the next mistake and learning even more.

Regards,

Jeremy

···

In article 200207180658.12096.harryo@zip.com.au, Harry Ohlsen wrote:

[Jeremy Henty wrote:]

Much as I love Ruby over Perl, this is one feature of Perl that I
thought was great and I wish Ruby copied it. It makes it easier
eg. to swap arguments around if you realise the order is wrong.

Nah. Maybe I’m a masochist, but I reckon if you do something stupid, it
should be HARD to fix, so you remember and don’t do it next time … or at
least the time after that :-).

Neat! I’ll keep in mind when I next hit this issue.

Jeremy

···

In article 20020717180630.GJ5537@panoptic.com, Dossy wrote:

On 2002.07.18, Jeremy Henty jeremy@chaos.org.uk wrote:

In article 3D358854.2E032977@stud.ntnu.no, Kent Dahl wrote:

… I’d have to argue against allowing trailing commas in method
calls …

… It makes it easier eg. to swap arguments around if you realise
the order is wrong. So in some ways this feature makes it easier
to get your code smelling sweet.

report.define(0, {
:column => ‘ABC’,
:label => ‘ABC_D’,
:width => -5,
})

Hi,

···

In message “Re: Syntax “surprise”” on 02/07/18, Brad Cox bcox@virtualschool.edu writes:

Its not a language’s business to prevent users from writing “smelly” code.

Despite I agree with this principle most of the cases, I’m not sure
whether allowing trailing comma in argument list enhance / reduce
astonishment, considering most of popular languages (except Perl) does
not allow trailing comma.

						matz.

Yep, quite a bit.

Didn’t mention keywords because that’s really the best tool for this
job, but unsupported in ruby. When that’s fixed I’ll withdraw my
objection.

···

At 2:48 AM +0900 7/18/02, Christopher Browne wrote:

I seem to recall Objective C having a pretty nice approach to it,
too. (Somehow your name tickles some memories concerning ObjC… You
didn’t have some involvement with it, did you?)


Brad Cox, PhD; bcox@virtualschool.edu 703 361 4751
o For industrial age goods there were checks and credit cards.
For everything else there is http://virtualschool.edu/mybank
o Interactive Learning Environment http://virtualschool.edu/ile
o Java Web Application Architecture: http://virtualschool.edu/jwaa

Brad Cox wrote Objective C.

···

On Wednesday 17 July 2002 10:48 am, Christopher Browne wrote:

I seem to recall Objective C having a pretty nice approach to it,
too. (Somehow your name tickles some memories concerning ObjC…
You didn’t have some involvement with it, did you?)


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

Yukihiro Matsumoto wrote:

Its not a language’s business to prevent users from writing “smelly” code.

Despite I agree with this principle most of the cases, I’m not sure
whether allowing trailing comma in argument list enhance / reduce
astonishment, considering most of popular languages (except Perl) does
not allow trailing comma.

I agree that trailing commas are strange. A comma means something,
to the language and to people who read a script in that language.
A trailing comma means something is missing.

If I could raise a similar issue,
I noticed this in the 1.6.7 TODO file:

  • compile time string concatenation, “hello” “world” => “helloworld”

I know that some other popular languages like to do that,
but I think it would be better to leave it as two arguments.
After all, someone wrote those two literals separately on purpose.

I would even go so far as to make commas optional, if that is possible.
(other popular languages use white-space delimiters: sh, Tcl, Lisp)
There would be less symbol clutter, and the script might be easier
to read.

obj.method a, b, c {|i| xyz}

vs
obj.method a b c {|i| xyz}

obj.method "a", "b", "c"

vs
obj.method “a” “b” “c”

Just something that would make Ruby even better, I think.

···

In message “Re: Syntax “surprise”” > on 02/07/18, Brad Cox bcox@virtualschool.edu writes:


Mike Hall
http://www.enteract.com/~mghall