Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   useful hints and tips (http://www.velocityreviews.com/forums/t387487-useful-hints-and-tips.html)

 richnjones@gmail.com 10-03-2006 07:11 PM

useful hints and tips

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

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.

Richard

 Deniz Dogan 10-04-2006 10:27 AM

Re: useful hints and tips

richnjones@gmail.com 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
>
>
> 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).

 Patricia Shanahan 10-04-2006 06:16 PM

Re: useful hints and tips

richnjones@gmail.com 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
>
>
> 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
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

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

 richnjones@gmail.com 10-04-2006 08:10 PM

Re: useful hints and tips

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 <p...@acm.org> wrote:
> richnjo...@gmail.com 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

>

>
> > 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
> 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
>
> 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

 shyam.natarajan@gmail.com 10-16-2006 05:04 PM

Re: useful hints and tips

richnjones@gmail.com 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 <p...@acm.org> wrote:
> > richnjo...@gmail.com 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

> >

> >
> > > 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
> > 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
> >
> > 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);
}
}

 All times are GMT. The time now is 08:43 AM.