From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Konstantin Kharlamov Newsgroups: gmane.emacs.bugs Subject: bug#35062: [PATCH v3 2/3] constify a bit of xterm.c Date: Sat, 13 Apr 2019 14:30:46 +0300 Message-ID: <1555155046.2588.0@yandex.ru> References: <20190407021331.948-1-Hi-Angel@yandex.ru> <20190407021331.948-2-Hi-Angel@yandex.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=koi8-r; format=flowed Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="85799"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Paul Eggert , 35062@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Apr 13 13:32:10 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hFGtG-000M9A-Bu for geb-bug-gnu-emacs@m.gmane.org; Sat, 13 Apr 2019 13:32:10 +0200 Original-Received: from localhost ([127.0.0.1]:50055 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hFGtE-0001Or-U5 for geb-bug-gnu-emacs@m.gmane.org; Sat, 13 Apr 2019 07:32:08 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:39584) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hFGt9-0001Oj-7V for bug-gnu-emacs@gnu.org; Sat, 13 Apr 2019 07:32:04 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hFGt8-0004UW-2H for bug-gnu-emacs@gnu.org; Sat, 13 Apr 2019 07:32:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:44724) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hFGt7-0004UI-T9 for bug-gnu-emacs@gnu.org; Sat, 13 Apr 2019 07:32:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hFGt7-0004Yv-Nl for bug-gnu-emacs@gnu.org; Sat, 13 Apr 2019 07:32:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Konstantin Kharlamov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 13 Apr 2019 11:32:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 35062 X-GNU-PR-Package: emacs Original-Received: via spool by 35062-submit@debbugs.gnu.org id=B35062.155515506414510 (code B ref 35062); Sat, 13 Apr 2019 11:32:01 +0000 Original-Received: (at 35062) by debbugs.gnu.org; 13 Apr 2019 11:31:04 +0000 Original-Received: from localhost ([127.0.0.1]:58265 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hFGsA-0003k2-8u for submit@debbugs.gnu.org; Sat, 13 Apr 2019 07:31:04 -0400 Original-Received: from forward103j.mail.yandex.net ([5.45.198.246]:46411) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hFGs6-0003Zc-8i for 35062@debbugs.gnu.org; Sat, 13 Apr 2019 07:30:59 -0400 Original-Received: from mxback3g.mail.yandex.net (mxback3g.mail.yandex.net [IPv6:2a02:6b8:0:1472:2741:0:8b7:164]) by forward103j.mail.yandex.net (Yandex) with ESMTP id CB9C16740546; Sat, 13 Apr 2019 14:30:49 +0300 (MSK) Original-Received: from smtp4j.mail.yandex.net (smtp4j.mail.yandex.net [2a02:6b8:0:1619::15:6]) by mxback3g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id fvzh3bADOP-Un1qGKvW; Sat, 13 Apr 2019 14:30:49 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1555155049; bh=vATkhKuvVOMP9k/QNWuPYouicgT1FGi9qZjBbG3d0zQ=; h=In-Reply-To:Cc:To:Subject:From:References:Date:Message-Id; b=knj69Nne++uSFaDA/czEAFPp1y2KwkVtp1swLovXrIcqRnj+KnevsKu3nHqdlC6z4 SisNcOsiMwDrgNATGqv2eQQviDQ99GaKiY+SMRjIVP1IvohPLLrwFrdYCoIzom05rk C/X2cS4oaMEF3CrIxVc/QdzSeGBwxNpjozNx/NGs= Authentication-Results: mxback3g.mail.yandex.net; dkim=pass header.i=@yandex.ru Original-Received: by smtp4j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id W84on9osMO-UmZ4YwqB; Sat, 13 Apr 2019 14:30:48 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client certificate not present) In-Reply-To: <8336mml3s7.fsf@gnu.org> X-Mailer: geary/master~g59ef85ca X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:157580 Archived-At: =F7 =F3=C2, =C1=D0=D2 13, 2019 at 11:15, Eli Zaretskii =20 =CE=C1=D0=C9=D3=C1=CC: >> From: Konstantin Kharlamov >> Date: Sun, 7 Apr 2019 05:13:30 +0300 >>=20 >> * 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):=20 >> 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 =20 >> of >> changes is clear either way. >=20 > 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=20 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. >=20 > 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=20 > 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=20 immutable code (e.g. in languages such as Haskell) keeps algorithm as=20 clean as possible, whereas mutability adds points of mental burden=20 (i.e. because you don't know without going through whole function=20 whether variable was only assigned once, or was it changed somewhere). Modern systems language Rust by default is immutable, and mutable=20 variables has to be explicitly marked as such (unlike it older=20 languages, where everything is mutable, and you have to `const`ify=20 things). ----- Either way, if you folks really in doubt, I'm okay with dropping this=20 patch. It took me maybe 10 minutes to assemble it, so it's not like=20 there's much work went into it. I simply found a place for improvement=20 in code, and did that. I'd like to take this as an opportunity to ask a question: I see Emacs=20 C code is using the old style where variables (mostly) are declared at=20 the beginning of a function. Is it just a legacy from C89 days (I hope=20 so), or is it a mandatory style? I'm asking because if I gonna work on the code, I'd for sure like to=20 encapsulate variables as much as possible, which means I'd rather=20 declare them on the first use (as a bonus, this often may allow to=20 constify the variable too). =