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] corwyn-ap.livejournal.com 2008-10-31 07:21 pm (UTC)(link)
I wonder if it would depend on if it was copied code, as opposed to just identical code.

I would say it needs (at least) a dialog box saying what it did.

[identity profile] metahacker.livejournal.com 2008-10-31 07:30 pm (UTC)(link)
Huh. IntelliJ warns you about this step (you can click through it) and also gives you options for "find in comments/string constants/etc.", i.e., the non-code cases. But yeah, that's a bit weird... I like knowing where and how my software has changed my work under me. Good reminder of a design pattern, that. ("Ensure the effects of the user's actions are visible.")

[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.
cellio: (avatar-face)

[personal profile] cellio 2008-10-31 09:30 pm (UTC)(link)
Hmm. Yeah, that would concern me too. It should at least ask you first.

I wonder if it really searched all your code, or if it maintains some memory of recent actions and tries to be clever. (Hey, he just copied this and now he's saying extract method... let's fix it for him.)

[identity profile] hascouf.livejournal.com 2008-11-01 01:15 am (UTC)(link)
As I understand it this is the case in Eclipse. It has an action buffer, and refactors cut and pastes within that buffer set. I don't recall how large that buffer is though.

[identity profile] metageek.livejournal.com 2008-11-01 01:40 pm (UTC)(link)
Ow. I would hate that: the notion of having an IDE mung my code is nothing compared to the notion of having that munging depend on history, rather than the state of the code.

But that's me. I'd rather attack my code with a penknife than run it through a nanoassembler. If I need more complicated changes, I'll script them myself. (It helps to stick to high-level languages where small text changes can have large effects. The strategy is less useful in languages like Java, which lack metaprogramming and first-order functions.)
laurion: (Default)

[personal profile] laurion 2008-11-01 12:47 am (UTC)(link)
Whoa. I think that feels a little like having your second kidney taken out because you went on dialysis for the first one.... except most people intend to go off dialysis.

Maybe it's like taking your shirt off for the shower and finding your pants have come off suddenly too.