Ken Austin wrote:
I added isOutOfBounds(n) to the original. That makes it a lot easier to read.
Junilu Lacar wrote:I noticed that you are doing bounds checking in a private method. Bounds checking should be done in methods that are part of the class API, that is, any method that can be called by someone else and you have no way of controlling the validity of the values being passed in. There should be no need to do bounds checking in private methods since you have control over the calls made to it. If an exception occurs because out of bounds values have been passed to a private method, that's either a bug or a lack of bounds checking in the calling method.
Junilu Lacar wrote:You probably tried to just reference N directly, right? Did you try perc.N? If you keep the N name, that might be a compelling reason to provide a better-named accessor like getSize() or getGridSize(), even if it only just returns the value of N directly.
Edit: I was looking at your use of getN() again and realized that you don't even need access to N. Just print out GRID_SIZE -- that should be the same as N anyway. If it isn't, then you'd have a bug.
Junilu Lacar wrote:
Ok, now that you've seen what wonders Extract Method can do for your code's readability, what expression(s) can you extract from the section of your code quoted below to make it more readable? Hint: an intention-revealing name such as isOutOfBounds() is almost always better than a comment. Comments tend to get ignored whereas you always pay attention to a method name.
You'll probably go for the easy win and extract the conditional expressions of the if statements but see what you can do to clarify and generalize the intent of the statements inside the if statements because those look like they could use a little DRYing out.
Ken Austin wrote:I'm going to sleep on this one and try again in the morning. Something like...
Ken Austin wrote:So the private method would be a good place for an assert statement, rather than the bounds checking?
A full site is an open site that can be connected to an open site in the top row via a chain of neighboring (left, right, up, down) open sites .
We say the system percolates if there is a full site in the bottom row.
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|