bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#35062: [PATCH v3 2/3] constify a bit of xterm.c


From: Konstantin Kharlamov
Subject: bug#35062: [PATCH v3 2/3] constify a bit of xterm.c
Date: Sat, 13 Apr 2019 14:30:46 +0300



В Сб, апр 13, 2019 at 11:15, Eli Zaretskii <eliz@gnu.org> написал:
 From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
 Date: Sun,  7 Apr 2019 05:13:30 +0300

 * src/xterm.c (x_cr_draw_image, x_update_begin,
 x_clear_under_internal_border, x_draw_fringe_bitmap,
 x_set_glyph_string_clipping, x_draw_glyph_string_background,
x_draw_composite_glyph_string_foreground, x_send_scroll_bar_event): make
 code easier to follow by making explicit that some variables are
 immutable. (Bug#35062)
 ---
 v3: mention functions changed in commit messages, mention the bug
number, and don't mention that it fixes a warning since intention of
 changes is clear either way.

I'm really struggling with these changes.  My main problem is that I
don't see how using 'const' here improves the readability and clarity
of the code.  IMO, if the variable's name doesn't state its purpose,
adding 'const' won't help much.  And I think compilers nowadays are
smart enough to deduce this by themselves.

I've had an example somewhere at the beginning of this thread, so let me quote myself

For illustration you can take a look at patch 3/4 here. There, I
constify min_rows and min_cols; and afterwards I remove a paragraph
that compares them to a number that wasn't assigned there. This allows
to not look through the code before the comparison because it's
immediately obvious: these variables are never changed.

This is true for reading the code as well, especially when you're
debugging a problem: you may often wonder "okay, when was the last time
that variable changed, could it be invalid here?". Then, if it's
"const" you immediately know the answer, whereas otherwise you have to
search through all usages of that variable to see when was the last
time it changed.

Basically, less mutability means easier to read code. Usually, fully immutable code (e.g. in languages such as Haskell) keeps algorithm as clean as possible, whereas mutability adds points of mental burden (i.e. because you don't know without going through whole function whether variable was only assigned once, or was it changed somewhere).

Modern systems language Rust by default is immutable, and mutable variables has to be explicitly marked as such (unlike it older languages, where everything is mutable, and you have to `const`ify things).

-----

Either way, if you folks really in doubt, I'm okay with dropping this patch. It took me maybe 10 minutes to assemble it, so it's not like there's much work went into it. I simply found a place for improvement in code, and did that.

I'd like to take this as an opportunity to ask a question: I see Emacs C code is using the old style where variables (mostly) are declared at the beginning of a function. Is it just a legacy from C89 days (I hope so), or is it a mandatory style?

I'm asking because if I gonna work on the code, I'd for sure like to encapsulate variables as much as possible, which means I'd rather declare them on the first use (as a bonus, this often may allow to constify the variable too).







reply via email to

[Prev in Thread] Current Thread [Next in Thread]