by psadauskas · 16y ago · 0 comments
The biggest difference there is that Ruby programmers frequently don't think twice about exposing their object's state to anyone and everyone.
It should be one line of code for each attribute in Java "public SomeClass foo;" if we're going for the same semantics as the Ruby example(admittedly, if you don't want to expose that it's a field, that's not quite as easy in Java, although really, there's no reason to not just do this:
private SomeClass foo_; // I don't know if Java allows methods with the same name as fields.
public SomeClass foo () { return foo; }
public SomeClass foo (SomeClass aFoo) { foo = aFoo; }
Yes, that's not what most Java programmers would write, and it's not too concise either, but it's not nearly as bad as you suggest.I prefer the Ruby. But, frankly, Ruby has too much boilerplate for attributes, too. I have to explicitly add attribute handling code in initialize? Why?
No, thanks, I'll use Perl 6:
has $.foo is rw;
There you go. An lvalue accessor method for an attribute, and the ability to set it in the constructor with the named argument ":foo(someValue)". And, if I want to add a type constraint, it's a simple matter of doing this instead: has Int $.foo is rw;
Or even this if I only want even numbers: has Int $.foo where { $_ % 2 } is rw;
If I really want to, I can manually implement my own constructor, but often, I won't need to.If I want the attribute to be private, I just replace "$.foo" with "$!foo" and it's only accessible within the class.
CLOS also makes this very simple, you just add an entry in the slots list of the class like so:
(foo :accessor foo :initarg :foo)
You can also do type-checking by adding ":type some-type-specifier", specify that it's either a class or instance(the default) slot with ":allocation class" or ":allocation instance", use :reader or :writer instead of :accessor to only generate a reader or a writer accessor or :initform to give the slot a default value.
Ruby is at a nice place on the accessor definition boilerplate continuum, but it's nowhere near the bottom.
'Attributes' are just methods like any other. What code do you think you need in initialize?
(Encouraging people to think of some methods as 'attributes' was a mistake, but also now a long-lost battle. It breaks the concept of the "messages only" Ruby object model, even though that's exactly what's happening. )
Of course, Ruby being dynamically typed makes it impossible to have most of the refactorings that statically typed languages take for granted, but that's a separate topic.
Look at it this way: you might think you are productive with Emacs/TextMate/vi, but you would be even more productive with an IDE of the Eclipse or IDEA caliber.
An IDE is nevery mandatory, it just makes you more productive.
Most of the refactoring tools that Java IDEs have originated in Smalltalk, an extremely dynamic language.
For more details and examples:
http://beust.com/weblog/2006/10/01/dynamic-language-refactor...
There's of course a limitation on this. To quote the paper: "The major drawback to this style of refac- toring is that the analysis is only as good as your test suite. If there are pieces of code that are not executed, they will never be analyzed, and the refactoring will not be completed for that par- ticular section of code. "
is usually quite sufficient for purpose if you have a reasonable idea what's going on in your codebase (and if you don't, you probably don't want to be renaming things yet).
Doesn't mean a real refactoring tool such as the ones people've been playing with for http://padre.perlide.org/ wouldn't be better, but it's amazing how well a simple brute force replace works on a reasonably tested codebase.
But I take it from your third paragraph that you agree with the OP and myself that IDEs with good refactoring tools are a good thing.