3.1.2 - Never use return in the middle of a method
return is to be used at the end of a method only.
Reasoning: Using return in the middle of a method makes it difficult to later break the method into smaller methods. It also forces the developer to consider more than one exit point to a method.
3.1.4 - Never use break other than in a switch statement
break is used only for switch statement control.
Reasoning: Using break, other than for switch statement control, makes it difficult to later break a construct into smaller constructs or methods. It also forces the developer to consider more than one end point for a construct.
I'm trying do solve the following problem (from Project Euler):
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:
d2d3d4=406 is divisible by 2
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.
Assuming I have a pandigital number that I want to test, I've written the following method to test whether it fits the criterea above:
As you can see, I have a "return false" in the middle of the method. I could instead simply put in a boolean flag that goes to false when a counterexample is detected and return that, and run through all the cases in the inner loop. However we will be calling this method several million times, so performance is important. (Actually for this problem it'll probably be only a difference of a second or two, but it may be more important in similar code for other problems.)
How could I get this to conform to JavaRanch style, without degrading performance?
Instead of Strings, when you say dividing directly, do you mean something like
Would it make any sense not to hard code 2 and 8 in the loop? I use these numbers to correspond to the problem description and so make the solution more legible, debuggable and maintainable.
I use <= because in other languages a FOR loop is usually described as something like for a = 1 to 10, where 10 is the last value to be evaluated. It makes more sense if you can see what the last value to be evaluated is.
Then I changed the pows array as follows, switched the modulus and division round
* edited: had a bug in there so it wasn't completing propery - actually it's only about 1-2% quicker.
I remember an actual case of a bug I was trying to find, which turned out to be caused by an early return. It was a method that had grown over time, and somebody added some code at the bottom of the method that had to be always executed. But in the beginning of the method, there was an early return, and when that was reached the new code at the bottom was ofcourse not run.
Worrying about making it run as fast as possible is counterproductive 99.9999999999% of the time. The code will almost always be fast enough. If you have documented requirements on speed, and if your code doesn't meet it, THEN use a profiler to determine where your code is slow, and focus there - never guess what you think will make it run faster. You will almost never guess right.
Personally, I would approach this in an entirely different manner where dividing a number up into sub-digits isn't needed...
Luigi Plinge wrote:So would you go for the Strings option, Fred?
But I haven't tackled this one yet, so my initial thoughts may not work at all.
If i were trying to break apart a number, I would not convert it to a string to pick it apart. I would use / and %.
Campbell Ritchie wrote:What you can do is have divisors, and divide the divisors by 10 every time.
Do they really expect you to go through all pandigital numbers? Assuming you don't start with 0, there are 9 × 9! of them.
That's only about 3 million numbers, which won't take more than a second or two at most to compute. My problem at the moment is generating the pandigital numbers efficiently. Testing every number for padigitality is inefficient because there are 10 billion numbers to test and that could take a while (although probably not longer than the time taken to code an alternative). I wrote something long and complicated last night which didn't really work but I've thought of a more efficient way... just got to code it now.
You are right; 9 × 9! = 3265920.
If anyone wants to criticise my code, go ahead. It probably looks pretty horrible if you're a functional programmer since the methods have lots of side-effects (updating the instance variables), but well... it seemed like it would be more efficient just to have 1 digits array rather than 3 million of them (Fred will tell me off for this ).
Good point about the while loop.
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
The WEB SERVICES and JAX-RS Coursehttps://coderanch.com/t/690789/WEB-SERVICES-JAX-RS