jducoeur: (Default)
jducoeur ([personal profile] jducoeur) wrote2008-10-31 03:11 pm
Entry tags:

Okay, that may qualify as too clever

There I was, coding along, and realized that this clause -- to unsubscribe from someone's presence when they change their IM handle -- also needs to be over there, for the case where they simply delete their IM handle. I copy the code from place A to B, think about it a minute, slap myself mentally, and decide that I should be extracting a method instead.

So I go to code A, invoke Eclipse's "Extract Method", and *poof*, I have a new method. I go over to B, to replace my copied code with a method call...

... and it's already been done for me.

On the one hand, that's exactly what I wanted. OTOH, it's kind of spooky. It hadn't even occurred to me that Extract Method would go looking for duplicate code and refactor that as well. It's probably usually correct, but managed to hit a button I wasn't even aware of -- the "Waitaminnit, that's *my* code! Don't screw with it!" response was quite visceral...

[identity profile] learnedax.livejournal.com 2008-10-31 08:14 pm (UTC)(link)
Hmm, yes, I can see both parts of that response. Ideally it should give you options on how smart it tries to be, just in case. The FlexBuilder form of Eclipse, as an example of being too clever and not smart enough, likes to automate your includes, to the point that if you comment out some code it will strip out any classes that were only referenced there... but isn't smart enough to re-import them when you uncomment the code. There is likely some setting to control that behavior, but since it's an Eclipse that option does not seem to be at all obvious.

I do remember that back when I was using Idea for Java, it had little optional helpers that would point out when two pieces of code seemed to be doing the same thing, and suggest that you let it extract the method. What surprised me in particular was that it would generally work even if the two chunks of code operated on different variables; it would just turn them into method arguments.