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_thread/thread/
90cfbe8bce6bf65c/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
Deniz Dogan - 04 Oct 2006 11:27 GMT
> Hi all,
>
[quoted text clipped - 19 lines]
> 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 - 04 Oct 2006 19:16 GMT
> Hi all,
>
[quoted text clipped - 6 lines]
> 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
richnjones@gmail.com - 04 Oct 2006 21:10 GMT
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
> richnjo...@gmail.com wrote:
> > Hi all,
[quoted text clipped - 26 lines]
>
> Patricia
shyam.natarajan@gmail.com - 16 Oct 2006 18:04 GMT
> Thanks for the replies.
>
[quoted text clipped - 32 lines]
> >
> > 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);
}
}