0

I'm writing a small C# console app, and I was wondering if there are ways to improve this part of code (particullary how to make it comply with the principle "Don't repeat yourself"). My app works fine with this construction too, but I don't like how it looks like.

if (rows % 2 == 0)
{
    evenCell = '#';
    oddCell = '.';
}
else
{
    evenCell = '.';
    oddCell = '#';
}
pppery
  • 3,731
  • 22
  • 33
  • 46
  • 1
    Ternary expression: `(evenCell, oddCell) = rows % 2 == 0 ? ('#', '.') : ('.', '#');` – Lasse V. Karlsen Jun 12 '20 at 15:06
  • @LasseV.Karlsen hopefully this works! Many thanks! – Alexey Bogdan Jun 12 '20 at 15:10
  • It works in newer versions of C#. – Lasse V. Karlsen Jun 12 '20 at 15:30
  • In newer versions of C# you can use tuples and tuple assignments to assign multiple variables "at the same time" (it's a bit of trickery but the syntax *means* that). Thus you either do a tuple assignment using one tuple with values, or with another tuple with different values. Tuple assignments (I'm sure it has a proper term) also allows you to swap variables: `(a, b) = (b, a);` will swap the contents of `a` and `b`. – Lasse V. Karlsen Jun 12 '20 at 15:31
  • 4
    Having said all of that, remember that you need to *maintain* this code. If every time you get back to this code you scratch your head (or beard, or leg, or parrot, or ... ) and wonder "what the he** does this do?", then perhaps it should've stayed as an if-statement. :) – Lasse V. Karlsen Jun 12 '20 at 15:32
  • @LasseV.Karlsen haha, this is a golden qoute. Indeed, "readability over brevity" seems to be a healthy practice. However, learning how to use new tools in my opinion is important as well, so I will really try to dig into ternary expressions. Thank god we can leave comments in the code :D – Alexey Bogdan Jun 12 '20 at 19:03
  • Personally I feel that comments are a blessing in disguise. It allows us to keep the bad code, just write what it actually does, instead of refactoring the code so that it makes sense on its own. But, that's just **my** opinion, the important part is that you find **your** opinion. Personally I would use my code from my comment, but others might not. – Lasse V. Karlsen Jun 12 '20 at 19:12

2 Answers2

2

You can use conditional operator ?: for that

var evenCell = rows % 2 == 0 ? '#' : '.';
var oddCell = rows % 2 == 0 ? '.' : '#';

It's shorter, but in this case you evaluate rows % 2 expression twice

You can also use C# 8 switch expression with tuple pattern

var (evenCell, oddCell) = (rows % 2) switch
{
    0 => ('#', '.'),
    _ => ('.', '#')
};

Or just conditional operator with value tuple (it works starting from C# 7)

var (evenCell, oddCell) = rows % 2 == 0 ? ('#', '.') : ('.', '#');
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
1

Maybe the short version is better like this :

(rows % 2 == 0) ? (evenCell = '#'; oddCell = '.';) : ( evenCell = '.';  oddCell = '#');
Ali Kianoor
  • 1,167
  • 9
  • 18
  • 1
    Looks kind of long, but here the check is done once and I don't see any repetition. I will mark this as an answer, thank you! – Alexey Bogdan Jun 12 '20 at 15:01