Lately I've been concerned about code legibility.
I am striving to write the most readable code I can, yet somehow end up with unaesthetic blocks of words that at first glance, are headache inducing.
I'm not claiming that visually pleasing code is easier to understand. I've written "pretty" code that is far from clear. case and point:
Code:
static inline int is_square_valid(char character, unsigned x, unsigned y)
{
static unsigned int newlines = 0;
static unsigned int blocks = 0;
int ret_val;
blocks = (blocks + (character == '#')) & (~(x || y) + 1);
newlines = (newlines + 1) & (~(character == '\n') + 1);
x += !newlines;
y += ! !newlines;
ret_val = (((newlines == BREAKS && y == COLUMNS) ||
((x % (ROWS + 1) && (!newlines || x == ROWS)) &&
(y < COLUMNS) && (blocks <= TETRAMINO_BLOCKS))) &&
(character == '.' || character == '#' || character == '\n'));
return (ret_val);
}
But lately more and more of what I'm writing looks like an ill formatted English essay than a technical sequence of instructions.
Today I was reviewing a utility function of my printf clone; the thing is as aesthetically ugly as I managed to make it. I was looking at what I though were descriptive identifiers and realized they are not as descriptive as I thought. Plus they are very long, which is an issue because my lines are to be shorter than 80 characters -school rule- and I have to constantly split them which adds to the bulky text appearance and increases my function line count -another limit on that as well.
When I found myself creating two macros to clarify the meaning of a short, third one, that I had to use in order not to make a line go over the char limit, I thought to myself "what the heck I'm doing".
I'm I thinking too much over this? Are meaningful identifiers still meaningful when they're longer than an 18 wheeler? As experienced programmers, do you think this is "readable":
Code:
#define DOT 1
#define FIRST_DIGIT 1
#define EXTRAS DOT + FIRST_DIGIT
extern void adjust_decimal_point(char *float_str, unsigned int precision)
{
char *str_cpy;
char *strcpy_start;
char *floatstr_start;
str_cpy = ft_strnew(ft_strlen(float_str));
strcpy_start = str_cpy;
floatstr_start = float_str;
if (*float_str == '-')
*str_cpy++ = *float_str++;
while (*float_str == '0' || *float_str == '.')
++float_str;
*str_cpy++ = *float_str++;
*str_cpy++ = '.';
while (*float_str && precision--)
{
float_str += (*float_str == '.');
*str_cpy++ = *float_str++;
}
str_cpy = strcpy_start;
round_up(str_cpy, ft_strlen(str_cpy) - (EXTRAS + (*str_cpy == '-')));
while (*str_cpy)
*floatstr_start++ = *str_cpy++;
*floatstr_start = 0;
free(strcpy_start);
}