Re:How I See Numbers
by teaearlgraycold on Thursday March 03, 2022 @02:47PM
(#30545827)
One thing that helps at lot with programming is my tendency to visualize branches and dependencies as graphs/trees as I read/write code. This makes aberrations and code smells extremely obvious. A dirty hack makes you go from something that looks like a beautiful fine-toothed comb to a comb with a cancerous tumor on it.
[ Parent
| on HN ]
Sounds like something that would make an interesting blog post!
Re:How I See Numbers
by teaearlgraycold on Thursday March 03, 2022 @03:46PM
(#30546733)
That's a good idea.
Sometimes the domain is already graphical - and I take every opportunity to make the code match the visual layout, ex:
https://github.com/danthedaniel/gameoflife-rs/blob/master/sr...
/// Count living cells adjacent to a cell in the matrix.
#[inline]
#[rustfmt::skip]
fn alive_neighbors(&self, x: i32, y: i32) -> u8 {
[
self[(x - 1, y - 1)], self[(x + 0, y - 1)], self[(x + 1, y - 1)],
self[(x - 1, y + 0)], /* selected cell */ self[(x + 1, y + 0)],
self[(x - 1, y + 1)], self[(x + 0, y + 1)], self[(x + 1, y + 1)],
]
.iter()
.fold(0, |total, &neighbor| total + (neighbor != 0) as u8)
}