Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
    end
    def draw
        @reference.draw
    end
    def rotate
        @reference.rotate
    end
    def clear
        @reference.clear
    end
end

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?

share|improve this question
    
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. –  thorsten müller 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...) –  ratchet freak Jan 22 '13 at 9:15

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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