DEV Community

Discussion on: Do you think functional code readable ?

Collapse
 
voanhcuoc profile image
Khoa Che

using lambdas over regular function definitions gets you much here. If your lambda if split over four lines, the extra return is not a big deal. Using function alone will make the code look much more familiar.

Also, you really should document your functions' purposes.

Agree. I wrote this code yesterday, haven't merged yet. At that time I just want to prototype as fast as possible, so less keystroke is a thing :)

you don't need to use lodash

Yes... I just get used to it than native Web API. Also AFAIK, Web API doesn't have range() and few other things.

you should probably not use [0] and [1]; use .x and .y since that's what you mean!

My bad!

Where you are mutating objects, you should probably stick to an imperative style.

How can I write it without this shit var i=0; i<50; i++ ?

Many thanks anyway :)

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!