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, 20 Apr 2019 04:09:19 +0300



В Пт, апр 19, 2019 at 17:31, Paul Eggert <eggert@cs.ucla.edu> написал:
I'd rather not use 'const' on locals. Let me tell you a story as to why.

I once worked with someone who insisted on using the 'register' keyword on every C local variable that was never taken the address of, on the grounds that this meant the reader could easily see that the variable could never be modified indirectly via a pointer and that this made the code easier to read because you didn't need to worry about aliasing.

I disagreed then, and still disagree. Saying 'register' nearly all the time clutters up the code, and the cost is not worth the benefit in C. It's pretty easy for a human reader to determine whether a local variable is taken the address of somewher in its function. (If it's hard, then write an Elisp function that will tell you. :-) In hindsight, perhaps C should have been designed so that 'register' was the default for local variables, and that one needed a special word to say "watch out! this variable might have its address taken!"; but the ship has sailed.

'const' is like 'register' in this respect. Putting 'const' nearly everywhere clutters C code. It's pretty easy for a human reader to determine whether a local variable is modified.

"const" is only similar to "register" in a way of having an additional word. Sure it's easy for a human to find whether variable was modified, but this requires to go through the whole function highlighting variable usages. Now imagine if visibility scope has lots of variables? This is especially relevant for the old C89 style where every variable was declared at the beginning of a function.

And btw, current workflow with searching through the body doesn't work well in vanilla Emacs. While other editors allow you to highlight matches by putting a caret over a symbol, Emacs requires either manually making it highlight/unhighlight, or installing a separate package highlight-symbol.el which is a bit buggy and wasn't updated since 2016. That is to say, having non-modified things declared with "const" may help an average Emacs developer.

(if it's hard, write an Elisp function :-)

But situation in there is similar: declaring a local variable requires to use (let…), which means that even if you use variable once in the end of a function, the variable will be visible to everything else. And it's not like you can can make it immutable either.

In hindsight, perhaps C should have been designed so that 'const' was the default for local variables; but the ship has sailed there too.

Yeah… FWIW, there's an "REmacs" effort on rewriting Emacs from C to Rust, Rust has everything "const" by default. I'm sure emacs-devel would largely disagree on integrating it because Rust isn't stable yet (despite being widely deployed) and there's no GCC backend (LLVM only), which are reasonable points. Still, I'm hoping at some point it will change :)







reply via email to

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