Refactoring Support for Attributes in Delphi 2010

I mentioned earlier that a lot of work in this release had gone into smaller features and feature tweaks. One of the results is that the IDE keeps giving me pleasant surprises. I’ll try to do something without even really thinking about whether it should work or not, and find that it not only works, but that it goes a little further than I might have thought.

One example is around Refactoring support for Attributes. If you look at my first, simple example of Attributes, one of the many things wrong with it is that the Attribute name is terrible. Unless I never plan on writing more than one Attribute, MyAttribute is probably not the best choice.

No problem, Delphi has supported a rename refactoring for awhile now, I’ll just use that. Right-click on the identifier, select Refactoring | Rename, give it a great new name like, oh, NameAgeAttribute (much better ;-)) and press OK.

At this point I expected it to rename all the actual occurrences of the class name in code, but I half expected it to not deal with the actual [MyAttribute…] occurrences. I definitely didn’t expect it to handle the places where I’d left off the Attribute suffix.

Well, look at the screenshot below and you’ll see that I underestimated it greatly. As a refactoring should, it handled every one of my cases, and left my code in a state that compiles and runs exactly as it did before.

Nice one guys!

RenameAttribute

10 Comments

  • Twitter Comment


    Refactoring Support for Attributes in Delphi 2010 [link to post]

    Posted using Chat Catcher

  • […] в блогосфере отметился Malcolm Groves, показавший, как хорошо работает рефакторинг с переименованием […]

  • The rename refactoring is based on CodeDOM, which, of course, supports attributes. Delphi language has supported attributes in .NET since this refactoring was introduced. So it’s not *completely* surprising that it works. 🙂

  • That’s brilliant. I’ve only spent a day with Delphi 2010 now, but I’m loving it.

    Lots.

  • Craig : Surprising, possibly not. Pleasing, definitely 🙂

    Corbus : Glad to hear that. I’m looking forward to seeing lots more people write about their discoveries with it.

  • can someone please illuminate me on this attribute thingy? i can’t seem to find it usefull, can’t we just use the old “if” condition instead ?

  • Delphigeist: I don’t understand the correlation to the ‘if’ statement, but apart from my articles http://www.malcolmgroves.com/blog/?p=476 and http://www.malcolmgroves.com/blog/?p=530, there’s an article here http://wings-of-wind.com/2009/08/17/rad-studio-2010-review-9-–-attributes-the-new-rtti-and-db-access/ that might help.

    Also, CodeRage is on this week and both Barry Kelly and Robert Love are doing sessions on Attributes. http://conferences.embarcadero.com/coderage/sessions

    Lastly, the docs are online and there is some info here http://docwiki.embarcadero.com/RADStudio/en/Attributes_Index although it’s more reference than tutorial.

    Hope that helps.

    Cheers
    Malcolm

  • Sorry Malcolm, but I can’t seem to find attributes being efficient it’s just like a kid script…
    I would upgrade to 2010 if IDE would have something like an A.I. so most of the times it would know what you want to do, for instance your trying to install a component but you don’t have all dependency units, in this case the IDE should contact Embarcadero for those units and provide you with a list of options or you type an identifier name which you didn’t declare, in this case the code completion should give you some options which should be customizable:
    – declare const
    – declare var
    – declare function
    – and so on.
    Now these(I bulive) are feauters that make up a good IDE, not attributes which are actually taking you considerable time to implement and are inefficient.
    But this is my opinion, feel free to contradict me.
    P.S. sorry for my English, it’s my second language.

  • Your comments make me think maybe you are missing the point: “IF statement”, “script kids”, etc.

    You asked for some more info, I gave you some links. My suggestion would be to read through/watch them and let it bake for awhile. Sometimes it takes awhile to “get” these new language features, give it some time.

  • Your right, maybe in the future I find something to use it for.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">