public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Leif Lindholm <leif.lindholm@linaro.org>
To: Evan Lloyd <Evan.Lloyd@arm.com>
Cc: "ryan.harkin@linaro.org" <ryan.harkin@linaro.org>,
	"ard.biesheuvel@linaro.org" <ard.biesheuvel@linaro.org>,
	"edk2-devel@ml01.01.org" <edk2-devel@ml01.01.org>
Subject: Re: [PATCH 4/4] ArmPkg:Provide GetTriggerType/SetTriggerType functions
Date: Fri, 24 Feb 2017 14:06:16 +0000	[thread overview]
Message-ID: <20170224140616.GC16034@bivouac.eciton.net> (raw)
In-Reply-To: <AM5PR0801MB17621F84D63C3A1FFD9D78FA8B5D0@AM5PR0801MB1762.eurprd08.prod.outlook.com>

(Intentionally sending my responses out of order)

Hi Evan,

On Fri, Feb 17, 2017 at 12:06:30PM +0000, Evan Lloyd wrote:
> Leif points out that “Ternaries are excellent when they increase
> code readability.”
> 
> The debate would thus seem to be a subjective assessment of
> “readability”.

Indeed. With the asymmetric weight distribution towards readability by
the maintainer.

> What criteria should we use to identify when a ternary is more
> readable, and when not?

My view of readability of ternaries is that it comes down to the
amount of information decoding required.

The Tianocore coding style actually works against ternaries a bit, in
that the resulting long variable/function/macro names frequently cause
them to spill onto multiple lines, which usually (but not always)
decrease readability.

The sequence in question
---
  *TriggerType = (MmioBitFieldRead32 (RegAddress, BitNumber, BitNumber) == 0)
                 ?  EFI_HARDWARE_INTERRUPT2_TRIGGER_LEVEL_HIGH
                 :  EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING;
---
looks like it's extracting a bitfield and assigning it to
*TriggerType, until you get to the == 0, where it looks like it's
assigning whether or not the value of an extracted bitfield is equal
to 0, and you have to get to the next line before you even spot that
it's a ternary. That's twice I have to re-evaluate _what_ it is I am
reviewing before I get to actually reviewing it.

At which point it becomes clear that the "bitfield extraction" is
actually being used as a roundabout binary AND operation. Another
re-evaluation of the sequence.

Had it been written as
---
  *TriggerType = (MmioRead32 (RegAddress) & (1U << BitNumber) == 0)
---
it wouldn't have slowed me down as much, and I might not even have
objected, only grumbled a bit under my breath.

The if-form, while 2 lines longer, contains the two lines
  } else {
and
  }
, which are completely trivial. So while two lines are added, they do
not add two lines to review. And it makes it completely clear from the
get-go that what I am reviewing is a test assigning one value or
another depending on the result of the test.

It also means (although it too would be improved by replacing the
BitField form with the binary AND) it at least isolates the test
statement.

Examples of ternary use that I approve of:
>From Linux kernel:
  arch/arm64/mm/dump.c
  .name   = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",

>From EDK2:
  MdeModulePkg/Bus/Isa/Ps2MouseDxe/CommPs2.c
  MouseDev->State.RightButton = (UINT8) (RButton ? TRUE : FALSE);

  MdeModulePkg/Core/Dxe/FwVol/FwVolRead.c
  WholeFileSize = IS_FFS_FILE2 (FfsHeader) ? FFS_FILE2_SIZE (FfsHeader): FFS_FILE_SIZE (FfsHeader);
  
I guess we can sum it up in an oversimplified way as "where the
distance between the '=' and the '?' is short enough to make it
immediately clear that we're dealing with a ternary, and the test is
immediately obvious even to someone not familiar with the component in
question.

Nested ternaries are _always_ an abomination, even if trivial.

> And how do we ensure consistency across all EDK2 maintainers?

This is on my list for things to bring into the discussion in
Budapest.

Regards,

Leif


  parent reply	other threads:[~2017-02-24 14:06 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-09 19:26 [PATCH 0/4] HardwareInterrupt2 protocol evan.lloyd
2017-02-09 19:26 ` [PATCH 1/4] EmbeddedPkg: introduce " evan.lloyd
2017-02-13 12:26   ` Leif Lindholm
2017-02-09 19:26 ` [PATCH 2/4] ArmPkg/ArmGicDxe: expose " evan.lloyd
2017-02-13 12:21   ` Leif Lindholm
2017-02-13 12:26     ` Ard Biesheuvel
2017-02-09 19:26 ` [PATCH 3/4] ArmPkg/GenericWatchdogDxe: Set Watchdog interrupt type evan.lloyd
2017-02-13 12:30   ` Leif Lindholm
2017-02-09 19:26 ` [PATCH 4/4] ArmPkg:Provide GetTriggerType/SetTriggerType functions evan.lloyd
2017-02-13 12:15   ` Leif Lindholm
2017-02-16 20:27     ` Evan Lloyd
2017-02-16 20:42       ` Ryan Harkin
2017-02-17 12:06         ` Evan Lloyd
2017-02-17 12:30           ` Ryan Harkin
2017-02-17 15:08             ` Alexei Fedorov
2017-02-17 18:18               ` Ard Biesheuvel
2017-02-24 14:06           ` Leif Lindholm [this message]
2017-02-13 13:05   ` Ard Biesheuvel
2017-02-16 20:16     ` Evan Lloyd
2017-02-16 20:46       ` Ard Biesheuvel
2017-02-17 11:53         ` Evan Lloyd
2017-02-24 11:26           ` Leif Lindholm
2017-02-13 15:51 ` [PATCH 0/4] HardwareInterrupt2 protocol Evan Lloyd
2017-02-13 17:15   ` Leif Lindholm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170224140616.GC16034@bivouac.eciton.net \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox