From nobody Thu May 2 07:42:05 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=1658762506; cv=none; d=zohomail.com; s=zohoarc; b=UbEj7kjYSkhMd7E0JpDaqI0LMsLBS+vRZQ1VUvT9klns0yTbdNxNXMcfOYeWxYem6p3VheAF5jZ2kJ3SfmB7T36bHiQ8s3HUlyQM3jMVfpOiij3kPAAzi2yHF2M3MgO3Tf1POIFwPFKoIlQxz+SOD3BQXCoSji/qSU3P6r/4C8Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658762506; 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=ssTU7Yx1Vy/8B761mLH5wYHGUk6pQtpuMigg5tZMxQo=; b=RXZV9tfqCztrMsOHmwuKsA2Jo5IZ7bJcM/zaNPHm8eufTzEBnY+AfDPBW6iYDIkA6GW2KTZOAzfI+mT0ffe6rwHQ6dx24LuiUBgGBMH+HtNL26wJG9KbJmFhWMPFdGNfhHfNMaXqZbD86l3qLE0wfWQWXjm/OWFNE2iUizkM0G8= 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 1658762506087603.6250003577411; Mon, 25 Jul 2022 08:21:46 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-638-xrYyV8pBN3u06aftbyXEwA-1; Mon, 25 Jul 2022 11:21:42 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C5F002999B3B; Mon, 25 Jul 2022 15:21:40 +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 58A3D18EB5; Mon, 25 Jul 2022 15:21:40 +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 F35C01945D86; Mon, 25 Jul 2022 15:21:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 6F47A1945D83 for ; Mon, 25 Jul 2022 15:21:39 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 3FEF4400E577; Mon, 25 Jul 2022 15:21:39 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id DFDE040CFD05 for ; Mon, 25 Jul 2022 15:21:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1658762504; 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=ssTU7Yx1Vy/8B761mLH5wYHGUk6pQtpuMigg5tZMxQo=; b=gP3azLQIUGDfPapKhDl9EhRgyijigOFtN9bdVtX2wuUyVwx2nAzrNQpRSYSxZD0ZStWqu7 /tUVMfD73VKoM95wClwFeXauAM/oWDGFz1cJ2CDz6/3psDusCjMohjNfO3YUPgD+dEsYhI 5IPbPjBG2SUHTJmfE3usL0xvSTj+XKA= X-MC-Unique: xrYyV8pBN3u06aftbyXEwA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2] coding-style: Allow some use of ternary operators Date: Mon, 25 Jul 2022 17:21:37 +0200 Message-Id: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 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.79 on 10.11.54.5 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: 1658762507599100001 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 it from our "no ternary operators" rule. Signed-off-by: Michal Privoznik Reviewed-by: Claudio Fontana Reviewed-by: Daniel P. Berrang=C3=A9 --- v2 of: https://listman.redhat.com/archives/libvir-list/2022-July/233150.html diff to v1: - Changed wording, as suggested by Daniel. 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..81bd4474f1 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: +Its usage in basic cases is warranted (e.g. when deciding between +two constant strings), however, it must never span more than one +line or nest. =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