![]() |
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 about this is the following post http://groups.google.co.uk/group/com...44e048850c4991 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 |
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 > about this is the following post > > http://groups.google.co.uk/group/com...44e048850c4991 > > 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). |
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 > about this is the following post > > http://groups.google.co.uk/group/com...44e048850c4991 > > 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 |
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 > > about this is the following post > > >http://groups.google.co.uk/group/com...mer/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 |
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 > > > about this is the following post > > > > >http://groups.google.co.uk/group/com...mer/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); } } |
| All times are GMT. The time now is 06:37 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.