useful hints and tips

Discussion in 'Java' started by richnjones@gmail.com, Oct 3, 2006.

  1. Guest

    Hi all,

    I have only started programming recently and am interested in some of
    the "tips n tricks" that can be used in programming. What made me think
    about this is the following post

    http://groups.google.co.uk/group/co...e8bce6bf65c/7244e048850c4991#7244e048850c4991

    It describes how to swap two integers using the xor operation to remove
    the need of an extra variable.

    I have also seen the modulus operator used to loop through a set number
    of integers.
    eg to print out 1,2,3,1,2,3,1,2,3 .....

    int i = 0;
    %start of loop%
    system.out.println(i++ % 3);
    %end of loop%

    My question is: are there any other quick fixes that people may know
    of. I know this is quite an open question but any hints/resources would
    be good.

    Thanks in advance

    Richard
     
    , Oct 3, 2006
    #1
    1. Advertising

  2. Deniz Dogan Guest

    wrote:
    > Hi all,
    >
    > I have only started programming recently and am interested in some of
    > the "tips n tricks" that can be used in programming. What made me think
    > about this is the following post
    >
    > http://groups.google.co.uk/group/co...e8bce6bf65c/7244e048850c4991#7244e048850c4991
    >
    > It describes how to swap two integers using the xor operation to remove
    > the need of an extra variable.
    >
    > I have also seen the modulus operator used to loop through a set number
    > of integers.
    > eg to print out 1,2,3,1,2,3,1,2,3 .....
    >
    > int i = 0;
    > %start of loop%
    > system.out.println(i++ % 3);
    > %end of loop%
    >
    > My question is: are there any other quick fixes that people may know
    > of. I know this is quite an open question but any hints/resources would
    > be good.


    The XOR way of "removing the need of an extra variable" is not at all
    intuitive and thus I wouldn't use that way of coding, ever. And the use
    of the modulo operator to "loop" through a set of numbers is barely a
    trick, it's used pretty much all the time.

    But to answer your question, I can't come up with any other tricks
    (because those are for kids).
     
    Deniz Dogan, Oct 4, 2006
    #2
    1. Advertising

  3. wrote:
    > Hi all,
    >
    > I have only started programming recently and am interested in some of
    > the "tips n tricks" that can be used in programming. What made me think
    > about this is the following post
    >
    > http://groups.google.co.uk/group/co...e8bce6bf65c/7244e048850c4991#7244e048850c4991
    >
    > It describes how to swap two integers using the xor operation to remove
    > the need of an extra variable.


    Here is a really useful programming tip: Always use the clearest, most
    straightforward, readable way of doing anything, unless you have a very,
    very good reason to prefer another method. And when you do have to write
    something at all obscure, add comments to explain what you are
    doing, and why it really has to be done that way.

    A clear, simple piece of code is more likely to get implemented
    correctly in the first place and is easier to modify correctly as the
    program changes. Perhaps most important of all, it is easier to read.
    That affects both your future self, attempting to understand and modify
    code you wrote previously, and other programmers who need to work on
    your code.

    The intent of the swap-via-temp, to exchange two values without
    transforming them, is obvious from the form of the code. The xor-swap
    looks as though the values are being changed, even though it has the
    same net effect. For that reason, swap-via-temp should be preferred
    whenever it is feasible.

    Patricia
     
    Patricia Shanahan, Oct 4, 2006
    #3
  4. Guest

    Thanks for the replies.

    I agree Patricia. I think I will try and make my classes small and
    self-contained. This should help me with my code
    Richard

    On Oct 4, 7:16 pm, Patricia Shanahan <> wrote:
    > wrote:
    > > Hi all,

    >
    > > I have only started programming recently and am interested in some of
    > > the "tips n tricks" that can be used in programming. What made me think
    > > about this is the following post

    >
    > >http://groups.google.co.uk/group/comp.lang.java.programmer/browse_thr...

    >
    > > It describes how to swap two integers using the xor operation to remove
    > > the need of an extra variable.Here is a really useful programming tip: Always use the clearest, most

    > straightforward, readable way of doing anything, unless you have a very,
    > very good reason to prefer another method. And when you do have to write
    > something at all obscure, add comments to explain what you are
    > doing, and why it really has to be done that way.
    >
    > A clear, simple piece of code is more likely to get implemented
    > correctly in the first place and is easier to modify correctly as the
    > program changes. Perhaps most important of all, it is easier to read.
    > That affects both your future self, attempting to understand and modify
    > code you wrote previously, and other programmers who need to work on
    > your code.
    >
    > The intent of the swap-via-temp, to exchange two values without
    > transforming them, is obvious from the form of the code. The xor-swap
    > looks as though the values are being changed, even though it has the
    > same net effect. For that reason, swap-via-temp should be preferred
    > whenever it is feasible.
    >
    > Patricia
     
    , Oct 4, 2006
    #4
  5. Guest

    wrote:

    > Thanks for the replies.
    >
    > I agree Patricia. I think I will try and make my classes small and
    > self-contained. This should help me with my code
    > Richard
    >
    > On Oct 4, 7:16 pm, Patricia Shanahan <> wrote:
    > > wrote:
    > > > Hi all,

    > >
    > > > I have only started programming recently and am interested in some of
    > > > the "tips n tricks" that can be used in programming. What made me think
    > > > about this is the following post

    > >
    > > >http://groups.google.co.uk/group/comp.lang.java.programmer/browse_thr...

    > >
    > > > It describes how to swap two integers using the xor operation to remove
    > > > the need of an extra variable.Here is a really useful programming tip: Always use the clearest, most

    > > straightforward, readable way of doing anything, unless you have a very,
    > > very good reason to prefer another method. And when you do have to write
    > > something at all obscure, add comments to explain what you are
    > > doing, and why it really has to be done that way.
    > >
    > > A clear, simple piece of code is more likely to get implemented
    > > correctly in the first place and is easier to modify correctly as the
    > > program changes. Perhaps most important of all, it is easier to read.
    > > That affects both your future self, attempting to understand and modify
    > > code you wrote previously, and other programmers who need to work on
    > > your code.
    > >
    > > The intent of the swap-via-temp, to exchange two values without
    > > transforming them, is obvious from the form of the code. The xor-swap
    > > looks as though the values are being changed, even though it has the
    > > same net effect. For that reason, swap-via-temp should be preferred
    > > whenever it is feasible.
    > >
    > > Patricia


    Here's the crude way of doing it :

    package com;

    public class Test {

    public static void main(String[] args) {
    StringBuffer val1 = new StringBuffer("-100");
    StringBuffer val2 = new StringBuffer("20");

    swap(val1, val2);

    System.out.println("Val 1 : " + val1);
    System.out.println("Val 2 : " + val2);
    }

    public static void swap(StringBuffer val1, StringBuffer val2) {
    val1.append("~" + val2);
    val2.append("~" + val1.substring(0,val1.indexOf("~")));

    val1.delete(0 , val1.indexOf("~")+1);
    val2.delete(0 , val2.indexOf("~")+1);
    }
    }
     
    , Oct 16, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa

    Java performance hints and tips?

    Ahmed Moustafa, Jul 14, 2003, in forum: Java
    Replies:
    9
    Views:
    664
    Ahmed Moustafa
    Jul 16, 2003
  2. Mark C
    Replies:
    0
    Views:
    346
    Mark C
    Feb 2, 2007
  3. Replies:
    2
    Views:
    474
  4. Replies:
    1
    Views:
    206
    Dave Burt
    Mar 24, 2005
  5. Replies:
    1
    Views:
    3,289
    Arne Vajhøj
    Jul 31, 2012
Loading...

Share This Page