Hello,
I read the book (FreeBSD device driver), but I'm not sure if the writer intend to make examples easier to read, for example :-
He used MIN so many times, despite doing this will increase calculation time and make code plotted , and it can be easily written as :-
Which is more readable for me? Does anybody share the same point with me? Or does he make it that way for some reason I don't understand?
There are other examples but I don't feel we have mush space here to list them out?
I read the book (FreeBSD device driver), but I'm not sure if the writer intend to make examples easier to read, for example :-
Code:
static int
echo_write(struct cdev *dev, struct uio *uio, int ioflag)
{
int error = 0;
error = copyin(uio->uio_iov->iov_base,
echo_message->buffer,
MIN(uio->uio_iov->iov_len, BUFFER_SIZE - 1));
if (error != 0) {
uprintf("Write failed.\n");
return (error);
}
*(echo_message->buffer +
MIN(uio->uio_iov->iov_len, BUFFER_SIZE - 1)) = 0;
echo_message->length = MIN(uio->uio_iov->iov_len, BUFFER_SIZE - 1);
return (error);
}
He used MIN so many times, despite doing this will increase calculation time and make code plotted , and it can be easily written as :-
Code:
static int
echo_write(struct cdev *dev, struct uio *uio, int ioflag)
{
int error = 0;
int length = MIN(uio->uio_iov->iov_len, BUFFER_SIZE - 1);
error = copyin(uio->uio_iov->iov_base, echo_message->buffer, length);
if (error != 0) {
uprintf("Write failed.\n");
return (error);
}
echo_message->buffer[length] = '\0';
echo_message->length = length;
return (error);
}
Which is more readable for me? Does anybody share the same point with me? Or does he make it that way for some reason I don't understand?
There are other examples but I don't feel we have mush space here to list them out?