E
Eli Bendersky
Hi all,
The issue of abstract interfaces has been discussed extensively in
this group. The general consensus is that Ruby's duck typing coupled
with the power of mixins renders solves the vast majority of the
problems interfaces in general and abstract interfaces in particular
were designed to solve in static languages like Java and C#.
However, one issue that wasn't addressed is documentation. An abstract
interface has the advantage of a single place to actually document the
interface. Consider a couple Ruby classes that implement the same
interface:
class FooTypePlayer
def jump
...
end
def climb(arg1, arg2)
end
end
class BarTypePlayer
def jump
...
end
def climb(arg1, arg2)
end
end
And now I want to document what these methods do (with a block comment
describing the arguments and return values, presenting examples,
etc.), and I want the documentation to be reflected in the RDoc of my
code. What are my options ? As I see it, several:
1) Duplicate the same documentation for each class.
- This is hardly feasible for large class-families and violates DRY.
2) Don't document. Give the methods and arguments descriptive names,
and write test cases that show how to use them.
- I will write unit tests anyway, and I definitely try to give methods
and arguments descriptive names, but a talkative block of
documentation with examples is essential for anything but trivial
classes.
3) Create a "dummy" abstract class that doesn't really do anything
(and is never instantiated), something like:
# Documentation ! bla bla bla...
class AbstractPlayer
# bla bla bla
def jump; end;
# This method... and returns ..., the arguments are ...
def climb(arg1, arg2); end;
end
Now, I can inherit FooTypePlayer and BarTypePlayer from AbstractPlayer
to show certainly that they're related, I can also fill in the methods
of AbstractPlayer by throwing "NotImplementedError". Although this all
is unnecessary, it might be a good solution for a single place of
documenting this interface.
What do you think ?
Eli
The issue of abstract interfaces has been discussed extensively in
this group. The general consensus is that Ruby's duck typing coupled
with the power of mixins renders solves the vast majority of the
problems interfaces in general and abstract interfaces in particular
were designed to solve in static languages like Java and C#.
However, one issue that wasn't addressed is documentation. An abstract
interface has the advantage of a single place to actually document the
interface. Consider a couple Ruby classes that implement the same
interface:
class FooTypePlayer
def jump
...
end
def climb(arg1, arg2)
end
end
class BarTypePlayer
def jump
...
end
def climb(arg1, arg2)
end
end
And now I want to document what these methods do (with a block comment
describing the arguments and return values, presenting examples,
etc.), and I want the documentation to be reflected in the RDoc of my
code. What are my options ? As I see it, several:
1) Duplicate the same documentation for each class.
- This is hardly feasible for large class-families and violates DRY.
2) Don't document. Give the methods and arguments descriptive names,
and write test cases that show how to use them.
- I will write unit tests anyway, and I definitely try to give methods
and arguments descriptive names, but a talkative block of
documentation with examples is essential for anything but trivial
classes.
3) Create a "dummy" abstract class that doesn't really do anything
(and is never instantiated), something like:
# Documentation ! bla bla bla...
class AbstractPlayer
# bla bla bla
def jump; end;
# This method... and returns ..., the arguments are ...
def climb(arg1, arg2); end;
end
Now, I can inherit FooTypePlayer and BarTypePlayer from AbstractPlayer
to show certainly that they're related, I can also fill in the methods
of AbstractPlayer by throwing "NotImplementedError". Although this all
is unnecessary, it might be a good solution for a single place of
documenting this interface.
What do you think ?
Eli