Readability is very important. It is very hard to explain shortly as agreement with that conclusion comes with experience.
With the experience also you shift amount of focus you give that single line to larger and larger forms: methods, classes, components, modules, applications, systems. The lower level the structure is the less time you want to spend writing it, andā¦ reading it after a year or two. With the experience also comes usage of the so called ādesign patternsā.
Companies try to follow some most commonly used conventions, so their developers will be more productive, and alsoā¦ replaceable.
There is nothing worse in software industry than reading/fixing a code written with total disregard of readability good practices and design patterns (in most cases it means that you are looking at a code of a person that was solving that kind of problem for a first time).
So for example:
if (position[x][y + 1] == 1) { numberofneighbors += 1; }
In Java, I think most of the conventions do actually force then statements to be not only in the next line but also
if (position[x][y + 1] == 1) {
numberofneighbors += 1;
}
But you do not want that? No problemā¦
numberofneighbors += position[x][y + 1]==1? 1: 0;
That do not actually fix readability, nor the multi line "if " would, because as someone mentioned you forgot one case. So how not to forgot, and made intention more readable:
for (int xShift=-1; xShift<=1; xShift++ ) {
for (int yShift=-1; yShift<=1; yShift++ ) {
if (xShift !=0 || yShift!=0) {
numberofneighbors += position[x+xShift][y + yShift]==1? 1: 0;
}
}
}
That kind of example more or less also shows that you stop thinking about only one single line / statement at a time (as a CPU or compiler does). You also actually stop being a āmonkeyā that 8 times writes almost the same instructions accordingly to the algorithm and you actually start writing the algorithm to the compiler. And let the CPU execute it 8 times (not you).
And going furtherā¦
Someone still can have an itch that āxShift !=0 || yShift!=0ā do not describe it self good. And you thing that this might be a good place to put a documentation comment. However there is an concept of writing self explanatory code that can be easily applied here:
for (int xShift=-1; xShift<=1; xShift++ ) {
for (int yShift=-1; yShift<=1; yShift++ ) {
if ( isNeighbor(xShift, yShift) ) {
numberofneighbors += position[x+xShift][y + yShift]==1? 1: 0;
}
}
}
private static boolean isNeighbor(xShift, yShift) {
return xShift !=0 || yShift!=0;
}
Is that too much linesā¦ yes, it is:
for (int xShift=-1; xShift<=1; xShift++ ) {
for (int yShift=-1; yShift<=1; yShift++ ) {
if ( IS_NEIGHBOUR.test(xShift, yShift) ) {
numberofneighbors += position[x+xShift][y + yShift]==1? 1: 0;
}
}
}
private static final BiPredicate<Integer, Integer> IS_NEIGHBOUR = (x,y) -> xShift !=0 || yShift!=0;
And that would be OK for any application with exception for low-latency and high performance applications where that boxing/unboxing of primitive values actually matters.
And lets not forget about ānumberofneighborsā: useTheCamelCase
And be good at it: rememberThatCamelCaseMeansHtmlNotHTML
Then you can actually start thinking the Object-Oriented way and enclose position array into theā¦
Interrupting note: I think creating class for Cell not board is like telling the problem that Iām solving is a GOL of one Cell and one of the tools to solve that Cell problem is to use board (while that is the other way around - you solve the problem of Board, that contains Cells - probably creation of Cell object is not needed )
ā¦ object:
public class LiveTable {
private boolen[][] table;
...
boolean isThereALiveInPosition(x,y) {
return position[x][y];
}
}
So now, our few lines of code will look like:
for (int xShift=-1; xShift<=1; xShift++ ) {
for (int yShift=-1; yShift<=1; yShift++ ) {
if ( IS_NEIGHBOUR.test(xShift, yShift) ) {
numberofneighbors += liveTable.isThereALiveInPosition( x + xShift, y + yShift ) 1: 0;
}
}
}
private static final BiPredicate<Integer, Integer> IS_NEIGHBOUR = ( x, y ) -> xShift !=0 || yShift!=0;
Please note that I applied selected set of rules, on selected lines of code.
One problem I have that I cannot easily solved is to provide you good sources for all of that - as
- experience is the best teacher
- there are many sources, I simply no longer visit them