DEV Community

Discussion on: Do you think functional code readable ?

Collapse
 
curtisfenner profile image
Curtis Fenner

I think

for (let i = 0; i < count; i++) {
    random_move(board);
}

Is probably the clearest way to write it. While for loops aren't very aesthetic, they are simple and very clear, and easily recognizable to JavaScript programmers.

If you really wanted to get rid of it, you could use something like

for (let i of _.range(count)) {

but since this isn't an established idiom, it takes more mental effort to read even if it is prettier.

I notice that random_move is actually doing too much. It shouldn't choose a move and execute it; it should just choose a move. The shuffle function body is the place where you should call process_move.

Thread Thread
 
voanhcuoc profile image
Khoa Che

:)

const loop = _.range;

loop(n).forEach(() => {
  random_move(board)
});

It shouldn't choose a move and execute it; it should just choose a move. The shuffle function body is the place where you should call process_move.

Oh yes. Thank you!