From nobody Sun Apr 28 08:39:43 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1658754848; cv=none; d=zohomail.com; s=zohoarc; b=MS+OP5wPg4HiMK+FOmWGexOMY8gJDSLRmcKhaSBWn+N6bU9jTKQKtjE8qlvEoLoDIf8x/xxH2VD9c27+AcaPjPRwQWSxHm7plNw9qMw0//e/+siDUYoPKxWA2O5UpW3poEFqrenD4wSlXs2iojhyc20iZiANHm9g/PaErV/GBds= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658754848; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=J1ZFa8ajFGoGd/7unf7KyvivoYOmt9gWSKKYvtodBKM=; b=CoyacRbL0dousb8wVQVzvA9l71NVgWh+XkQ+9N4DEEBI0OsIzDyfKP7UIVtBzL7X+VnENnP0usff7J5L4Zt4rkN0hMfH9nEOzrYKCmP1X7Ua/EkEgUY834Zvzc+AnjsSykCb+l+h86LAiPUUiptXDXcIwDsKabOEwCLwNnVA/TI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1658754848478860.1807224432134; Mon, 25 Jul 2022 06:14:08 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-184-slu-NDBiPFC6aEUrmoTSAw-1; Mon, 25 Jul 2022 09:14:05 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BDEA0185A794; Mon, 25 Jul 2022 13:14:02 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id AAC07492CA2; Mon, 25 Jul 2022 13:14:02 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 41A721945D83; Mon, 25 Jul 2022 13:14:02 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 9C70B1945D81 for ; Mon, 25 Jul 2022 13:14:01 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 7BBCD2166B2B; Mon, 25 Jul 2022 13:14:01 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id 271B82166B26 for ; Mon, 25 Jul 2022 13:14:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658754847; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=J1ZFa8ajFGoGd/7unf7KyvivoYOmt9gWSKKYvtodBKM=; b=W8KhsLdkLD0wtBeRLyASqSJjciyPZ8yGdGUYtzbqsJ/mkLoYfhBNh5VjM4kRDErLpiiWk+ TQdWNyq3SUMay705JlzniMyFSArfO6XCyDQUTEVc/mvDe+L/gicb2v3cEwApsCAlKG/Eir EzuCU0rUEZOafd7Pq0M+pC+XOSiKE/Q= X-MC-Unique: slu-NDBiPFC6aEUrmoTSAw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH] coding-style: Allow some use of ternary operators Date: Mon, 25 Jul 2022 15:13:58 +0200 Message-Id: <74d640cf2d9b76203b20e42acbbab9ba6fd87477.1658754838.git.mprivozn@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1658754848967100001 Content-Type: text/plain; charset="utf-8"; x-default="true" While we all understand that excessive use of ternary operator may worsen code readability (e.g. nested, multi-line expression), there are few cases where using it actually improves code readability. For instance, when a function takes a long list of arguments out of which one depends on a boolean expression, or when formatting "yes"/"no" or "on"/"off" values based on a boolean variable (although one can argue that the latter is a subset of the former). Just consider alternatives to: virBufferAsprintf(buf, "%s\n", boolVar ? "yes" : "no"); In fact, this pattern occurs plenty in our code. Exempt if from our "no ternary operators" rule. Signed-off-by: Michal Privoznik Reviewed-by: Claudio Fontana --- docs/coding-style.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/coding-style.rst b/docs/coding-style.rst index bf0a80fbc5..038f18bda2 100644 --- a/docs/coding-style.rst +++ b/docs/coding-style.rst @@ -470,7 +470,9 @@ Pointer comparisons may be shortened. All long forms ar= e okay. if (!foo) # or: if (foo =3D=3D NULL) =20 New code should avoid the ternary operator as much as possible. -Specifically it must never span more than one line or nest: +Specifically it must never span more than one line or nest. However, +its usage in very basic cases is warranted (e.g. when deciding +between two constant strings): =20 :: =20 @@ -481,6 +483,9 @@ Specifically it must never span more than one line or n= est: =20 char *foo =3D bar ? bar->baz ? bar->baz->foo : "nobaz" : "nobar"; =20 + GOOD: + virBufferAsprintf(buf, "%s\n", boolVar ? "yes" : "n= o"); + Preprocessor ------------ =20 --=20 2.35.1