[Bluej-discuss] how to avoid duplicate codes
Davin McCall
davmac at bluej.org
Mon Jul 17 04:36:01 BST 2006
Hi Li,
You're doing both of the following at various times:
my_revSeq = new
StringBuilder(do_seq()).reverse().toString().toUpperCase();
my_reComSeq=new
StringBuilder(do_comSeq()).reverse().toString().toUpperCase();
Why not factor this out as a method? -
private String reverseSequence(String seq)
{
return new StringBuilder(seq).reverse().toString().toUpperCase();
}
... Then replace both of the original statements with calls to the method:
my_revSeq = reverseSequence(do_seq());
my_reComSeq = reverseSequence(do_comSeq());
The same general technique can be used in most situations where there is
duplicate code.
- Davin
chen li wrote:
> Hi guys,
>
> Sorry to bother again.
>
> I have a small program as following. What bother me
> is that method do_revSeq() and do_revComSeq () look
> like duplicate. I have problems to chain method
> do_revSeq() and do_comSeq to get the revComSeq. Any
> ideas on how to improve/correct it?
>
> Thanks,
>
> Li
>
> public class SeqReader
> {
> private String my_seq;
> private String my_revSeq;
> private String my_comSeq;
> private String my_reComSeq;
>
> public SeqReader()
> {
> my_seq="aatT ccGG";
> }
>
> private String do_seq()
> {
> return my_seq.replace(" ", "");//sequence
> clean up
> }
>
> public String do_revSeq()
> {
> my_revSeq=new
> StringBuilder(do_seq()).reverse().toString().toUpperCase();
> return my_revSeq;
> }
>
> public String do_comSeq()
> {
>
> my_comSeq=do_seq().toLowerCase().replace('a',
> 'T').replace('t', 'A').replace('c', 'G').replace('g',
> 'C');
> return my_comSeq;
>
> }
>
> public String do_revComSeq ()
> {
> my_reComSeq=new
> StringBuilder(do_comSeq()).reverse().toString().toUpperCase();
> return my_reComSeq;
> }
>
> public void print_results()
> {
> System.out.println(do_seq());
> System.out.println(do_revSeq());
> System.out.println( do_comSeq());
> System.out.println(do_revComSeq ());
> }
> }
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> mailing list bluej-discuss at bluej.org
> To unsubscribe or change your preferences, go to
> http://lists.bluej.org/mailman/listinfo/bluej-discuss
>
>
More information about the bluej-discuss
mailing list