useful hints and tips

R

richnjones

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
 
D

Deniz Dogan

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).
 
P

Patricia Shanahan

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
 
R

richnjones

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
 
S

shyam.natarajan

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top