I am using JRuby.

In my Java code, I have a class called Texture, capable of doing some graphic manipulation stuff.

In my Ruby code, I will usually need to draw things, so though I should simply call Java's Texture class to do the drawing for me.

However, I ended up making a Ruby class called Texture, which wraps an instance of Java's Texture. Somehow like this (just an example, but rather accurate):

class Texture
    def initialize
        @reference = getMeAnInstanceOfAJavaTexture
    def draw
    def rotate
    def clear

As you can see, all this Texture class does is... well, tell a Java Texture to do the job for it. Just that.

Subconsciously, I think I wanted to do it this way because it looks pretty (lol).

Now, getting a bit more technical, a possible advantage I see is that this way I don't have to interact much with the Java-side of the project. Most of the work I will do will be in Ruby, and maintaining a constant interaction between Ruby and Java can be confusing. If I make a Ruby class that handles this stuff for me, I might feel more comfortable in the future when I have to make like a hundred textures, but instead of interacting a hundred times with Java, I do it with Ruby, my main environment.

Does my reasoning make sense or is it a lame excuse to do something that looks pretty? Or perhaps there is indeed a good reason to do what I just did?

  • In the Ruby community "looks pretty" always had very high value. Your code will be more readable for sure. I don't know how important it is from a technical sense, since I don't know Java and the environment you use. Jan 22 '13 at 8:48
  • 1
    this is a valid approach and (as a bonus) allows you to substitute the Texture with a custom ruby class later (if you ever feel the need to...) Jan 22 '13 at 9:15

Yes, this probably makes sense.

Without such a class, you will need to remember the details of calling out to the Java class at each place where you use this, and get those details right at each place -- and even if this is a simple enough use of Java to make this not a burden, those lines of code will "feel" more in the Java idiom than the Ruby idiom, breaking the flow of your program when reading the code. As you find yourself repeating this usage in more places, the value in fixing this grows (i.e. one use of a Java class probably does not justify a wrapepr).

Bottom line: creating a class or method to wrap something you find yourself doing over and over again in a simple interface which feels right in the context of your program can make your code easier to read and maintain.

  • 1
    This can also aid in encapsulation. If the java object has functionality which is unused, excluding it from your interface can simplify your coding. This is not specific to cross-language wrappers; providing intra-language interfaces to a subset of an object's functionality can also aid encapsulation.
    – Brian
    Jan 22 '13 at 22:50
  • Strongly agreed.
    – jimwise
    Jan 22 '13 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.