Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.java.* > Newsgroup comp.lang.java.help



Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2009, 11:26 AM
Mark Smith
Guest
 
Posts: n/a
Default String literals into constants automagically?

Hi,

I am using netbeans. One feature that I love is the insert code
feature, for automatically making getters and setters it has saved me
hours of braindead tedious work.

I am looking at some code now that has gardually evolved into a mess
and I think it is time to tidy it up. It does a lot of string
comparisons. There are over 200 string literals in the main class! It
strikes me as another braindead task that could be almost completely
done by a computer.

Is there a feature in netbeans (or any ide) that can extract String
literals from a selection and replace them with centrally defined
constants?

Thanks.

Reply With Quote
Alt Today
Advertising
Google Adsense
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 11-26-2009, 12:15 PM
John B. Matthews
Guest
 
Posts: n/a
Default Re: String literals into constants automagically?

In article
<21686352-958b-4b66-b24b-83901c1b569c@g26g2000yqe.googlegroups.com>,
Mark Smith <marksmith5555@jungle-monkey.com> wrote:

> I am using netbeans. One feature that I love is the insert code
> feature, for automatically making getters and setters it has saved me
> hours of braindead tedious work.


I had to override hashCode() the other day, and the equals() it offered
to generate was excellent.

> I am looking at some code now that has gardually evolved into a mess
> and I think it is time to tidy it up. It does a lot of string
> comparisons. There are over 200 string literals in the main class! It
> strikes me as another braindead task that could be almost completely
> done by a computer.
>
> Is there a feature in netbeans (or any ide) that can extract String
> literals from a selection and replace them with centrally defined
> constants?


See if "Introduce Constant" would be apropos:

<http://wiki.netbeans.org/TS_60_Refactoring2#Test_suite:_Introduce_Constant>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Reply With Quote
  #3 (permalink)  
Old 11-26-2009, 07:05 PM
Roedy Green
Guest
 
Posts: n/a
Default Re: String literals into constants automagically?

On Thu, 26 Nov 2009 03:26:08 -0800 (PST), Mark Smith
<marksmith5555@jungle-monkey.com> wrote, quoted or indirectly quoted
someone who said :

>
>Is there a feature in netbeans (or any ide) that can extract String
>literals from a selection and replace them with centrally defined
>constants?


There is a tool to do it in IntelliJ Idea called "introduce constant".
The catch is if you were to extract the literal 1, it would by default
turn all the literal 1s into the same named constant. This buries a
mine, that will hit you later if you change the value of the
"constant". You have to keep your wits about you when you use it, and
review all uses of the new named constant.

see http://mindprod.com/jgloss/intellij.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
I mean the word proof not in the sense of the lawyers, who set two half proofs equal to a whole one, but in the sense of a mathematician, where half proof = 0, and it is demanded for proof that every doubt becomes impossible.
~ Carl Friedrich Gauss
Reply With Quote
  #4 (permalink)  
Old 11-26-2009, 09:20 PM
Lew
Guest
 
Posts: n/a
Default Re: String literals into constants automagically?

Mark Smith wrote:
> I am using netbeans [sic]. One feature that I love is the insert code
> feature, for automatically making getters and setters it has saved me
> hours of braindead tedious work.
>
> I am looking at some code now that has gardually evolved into a mess
> and I think it is time to tidy it up. It does a lot of string
> comparisons. There are over 200 string literals in the main class! It
> strikes me as another braindead task that could be almost completely
> done by a computer.


NetBeans and Eclipse (and its derivatives) have this feature.

> Is there a feature in netbeans [sic] (or any ide [sic]) that can extract String
> literals from a selection and replace them with centrally defined
> constants?


As John B. Matthews said of NetBeans:
> See if "Introduce Constant" would be apropos:


NetBeans:
Menu: "Ref_a_ctor / Introduce _C_onstant"

Eclipse:
Menu: "Refac_t_or / Extr_a_ct Constant..."

Both IDEs will refactor all instances of the same constant, but you must have
a constant selected for the feature to know what to extract. Both give you
the choice of access qualifier. Both preselect a suggested constant
identifier that you can override.

--
Lew
Reply With Quote
  #5 (permalink)  
Old 11-27-2009, 03:35 PM
Mark Smith
Guest
 
Posts: n/a
Default Re: String literals into constants automagically?

On Nov 26, 9:20*pm, Lew <no...@lewscanon.com> wrote:
> Mark Smith wrote:
> > I am using netbeans [sic]. One feature that I love is the insert code
> > feature, for automatically making getters and setters it has saved me
> > hours of braindead tedious work.

>
> > I am looking at some code now that has gardually evolved into a mess
> > and I think it is time to tidy it up. It does a lot of string
> > comparisons. There are over 200 string literals in the main class! It
> > strikes me as another braindead task that could be almost completely
> > done by a computer.

>
> NetBeans and Eclipse (and its derivatives) have this feature.
>
> > Is there a feature in netbeans [sic] (or any ide [sic]) that can extract String
> > literals from a selection and replace them with centrally defined
> > constants?

>
> As John B. Matthews said of NetBeans:
>
> > See if "Introduce Constant" would be apropos:

>
> NetBeans:
> Menu: "Ref_a_ctor / Introduce _C_onstant"
>
> Eclipse:
> Menu: "Refac_t_or / Extr_a_ct Constant..."
>
> Both IDEs will refactor all instances of the same constant, but you must have
> a constant selected for the feature to know what to extract. *Both giveyou
> the choice of access qualifier. *Both preselect a suggested constant
> identifier that you can override.
>
> --
> Lew


Thanks all for the help.

Introduce Constant feature worked great!
Reply With Quote
 
Reply

Popular Tags in the Forum
automagically, constants, literals, string

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
KirbyBase : replacing string exceptions Brendan Newsgroup comp.lang.python 2 11-23-2009 05:05 PM
substring assignment in fortran, C, etc. LC's No-Spam Newsreading account Newsgroup comp.lang.fortran 57 05-26-2009 12:21 AM
substring assignment in fortran, C, etc. LC's No-Spam Newsreading account Newsgroup comp.lang.c 42 05-23-2009 10:32 AM
String parsing question. SpreadTooThin Newsgroup comp.lang.java.programmer 15 04-21-2009 04:30 PM
Regular Expressions - need a little help Charles Patridge Newsgroup comp.soft-sys.sas 0 09-27-2006 02:24 PM



Language 1 | C | C++ | Php | Python | Lisp | Perl | Ruby | Java | Pascal | Basic | Language 2 | Databases | Oracle | Mysql | Access | Drupal
All times are GMT. The time now is 02:00 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.