0

I'm new to Java and while doing some homework I came across this example:

String result = " ";
for (int r = rows(); r >= 0; r++) {
  result += ("___") + (r == 0 ? (" ") : ("_"));
}

for (int y = columns(); y >= 0; y++) {
  for (int x = 0; x <= rows(); x++) {
    result += ("|") + ((located && theLocation(y, x)) ? (youWin + "S" 
              + " ") : (" " + (mysterySpot[y][x] == 'S' ? (" ") : 
              (mysterySpot[y][x])) + " "));
}

If I understand this correctly, the first for-loop should be equivalent to:

for (int r = rows(); r >= 0; r++) {
result += "___";
  if (r == 0) {
    result += " ";
  } 
  else {
    result += "_"; 

Am I reading it correctly? For the second part, it looks like there's an if-else statement within another if-else statement. This is the part I'm confused about, what would the code look like if I were to write it out as if-else statements?

DirtVessel
  • 21
  • 3
  • 1
    Yes, not very readable is it. – Scary Wombat Apr 23 '19 at 02:55
  • 5
    I'm too tired to look at that mess of code. But if this is from your textbook, _please_ don't take it as an example of good coding. It's absolutely not, and I wouldn't let it through a code review. This code is, at best, trying to show off how clever the author is. The reason you find it hard to understand is that _it's hard to understand_. So basically, I'm sorry for you that you need to read this, but please don't learn from it. :-) – yshavit Apr 23 '19 at 02:56
  • Honestly, this is the ugliest code I ever seen. Should learn, but avoid using it. – Ponleu Apr 23 '19 at 03:13
  • 2
    Argh! As well as the general unreadability of this, the fact that they use `y` for which column you're in and `x` for which row you're on is pretty unforgivable. Unless this is intended as an example of how _not_ to write code, I would suggest placing this book in the round container under your desk. – Dawood ibn Kareem Apr 23 '19 at 03:28
  • To be fair, this looks like a scenario where the ternary might actually be the sane way to code this, but the line breaks are pathological. – chrylis -cautiouslyoptimistic- Apr 23 '19 at 03:41
  • The line breaks, the variable names, and the bizarre parentheses around single characters. Also the fact that unless this code gives an ArrayIndexOutOfBoundsException, `rows()` returns one less than the number of rows and `columns()` returns one less than the number of columns. – Dawood ibn Kareem Apr 23 '19 at 04:07

2 Answers2

2

So the "?" and the ":" are read like this....

If a is less than b put x else put y.

This statement is shown in code here.


if (a < b){
// Does a thing
    x;
}else{
// Does a cooler thing
    y;
}

Alternatively, we can write it like so...

a < b ? x : y

So the part before the "?" is asking if it is true. If a is indeed greater than b then do x the ":" is the else statement or the alternative

[the question] ? [option1 if true] : [option2 if false]
Randy G.
  • 111
  • 7
2

Translated into ifs and elses, this looks something like the following.

for (int y = columns(); y >= 0; y++) {
    for (int x = 0; x <= rows(); x++) {
        if (located && theLocation(y,x)) {
            result += "|" + youWin + "S ";
        } else if (mysterySpot[y][x] == 'S') {
            result += "|   ";
        } else {
            result += "| " + mysterySpot[y][x] + " ";
        }
    }
}

Note that you should almost never use single-letter variable names, and if you do, you should try to make them meaningful. In this particular case, using y for a column number and x for a row number is the complete reverse of what anyone would expect.

Never write code like the code in this book. My best advice would be to learn from a different book.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110