From: "Håkon Løvdal" <hlovdal@gmail.com>
To: David Miller <davem@davemloft.net>
Cc: mfmooney@gmail.com, joe@perches.com, aquini@linux.com,
kernel-janitors@vger.kernel.org, fubar@us.ibm.com,
andy@greyhouse.net, shemminger@vyatta.com,
netdev@vger.kernel.org, nikai@nikai.net
Subject: Re: [PATCH] net/bonding: adjust codingstyle for bond_3ad files
Date: Mon, 9 May 2011 03:30:10 +0200 [thread overview]
Message-ID: <BANLkTin77k5oHGvn-E-6asQForB62it1Yw@mail.gmail.com> (raw)
In-Reply-To: <20110508.171204.27789908.davem@davemloft.net>
On 9 May 2011 02:12, David Miller <davem@davemloft.net> wrote:
> From: Håkon Løvdal <hlovdal@gmail.com>
> Date: Mon, 9 May 2011 02:08:41 +0200
>
>> void bond_3ad_state_machine_handler(struct work_struct *work)
>> {
>> struct bonding *bond = container_of(work, struct bonding,
>> ad_work.work);
>> struct port *port;
>> struct aggregator *aggregator;
>>
>> read_lock(&bond->lock);
>>
>> if (! bond->kill_timers) {
>>
>> //check if there are any slaves
>> if (bond->slave_cnt != 0) {
>> ...
>> }
>> queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
>> }
>> read_unlock(&bond->lock);
>> }
>>
>>
>> And this was what I trying to reccommend against (and which the
>> stackoverflow question is about).
>
> I really don't see anything wrong with either approach.
The example above looks innocent since it is so small. But written
this way, the all of core of the function ("...") will have two extra,
unnecessary indentation levels. My rule of thumb is to always test for
exceptions, not the normal case.
Expanding the example to cover the full function, it could be like the
following:
void bond_3ad_state_machine_handler(struct work_struct *work)
{
struct bonding *bond = container_of(work, struct bonding,
ad_work.work);
struct port *port;
struct aggregator *aggregator;
read_lock(&bond->lock);
if (! bond->kill_timers)
{
//check if there are any slaves
if (bond->slave_cnt != 0)
{
int uninitialized = 0;
// check if agg_select_timer timer after
initialize is timed out
if (BOND_AD_INFO(bond).agg_select_timer &&
!(--BOND_AD_INFO(bond).agg_select_timer)) {
// select the active aggregator for the bond
if ((port = __get_first_port(bond))) {
if (port->slave) {
aggregator =
__get_first_agg(port);
ad_agg_selection_logic(aggregator);
} else {
pr_warning("%s:
Warning: bond's first port is uninitialized\n",
bond->dev->name);
uninitialized = 1;
}
}
if (! uninitialized)|
bond_3ad_set_carrier(bond);
}
if (! uninitialized) {
// for each port run the state machines
for (port = __get_first_port(bond);
port; port = __get_next_port(port)) {
if (port->slave) {
/* Lock around state
machines to protect data accessed
* by all (e.g.,
port->sm_vars). ad_rx_machine may run
* concurrently due to
incoming LACPDU.
*/
__get_state_machine_lock(port);
ad_rx_machine(NULL, port);
ad_periodic_machine(port);
ad_port_selection_logic(port);
ad_mux_machine(port);
ad_tx_machine(port);
// turn off the BEGIN
bit, since we already handled it
if (port->sm_vars &
AD_PORT_BEGIN)
port->sm_vars
&= ~AD_PORT_BEGIN;
__release_state_machine_lock(port);
} else {
pr_warning("%s:
Warning: Found an uninitialized port\n",
bond->dev->name);
}
}
}
}
queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
}
read_unlock(&bond->lock);
}
Notice for instance "aggregator = __get_first_agg(port);" now have 5
levels of indentation in addition to the function body level, compared
to just 2 in the original.
That extra indentation is one thing, but the most important in my opinion
is that layout of the code becomes harder to read. This is because the
error handling code will typically be much more intrusive in the "layout".
Good code written like
if (error_check) {
error
handling
code
}
normal
code
here
is possible to quickly scan/read, but if code is written like
if (some_check) {
error
handling
code
} else {
normal
code
here
}
or
if (some_check) {
normal
code
here
} else {
error
handling
code
}
you have to go into slow gear and read and mentally process (relatively)
much more of the code to get the same picture. Combine a few such if/else,
nested - now everything is tightly intervened and impossible to quickly
grasp and/or hold in your head.
And the "uninitialized" variable is a crutch.
BR Håkon Løvdal
next prev parent reply other threads:[~2011-05-09 1:30 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-07 1:27 [PATCH] net/bonding: adjust codingstyle for bond_3ad files Rafael Aquini
2011-05-07 1:51 ` Joe Perches
2011-05-07 17:31 ` Rafael Aquini
2011-05-07 18:02 ` Joe Perches
2011-05-07 19:35 ` matt mooney
2011-05-07 20:24 ` Joe Perches
2011-05-08 23:08 ` Håkon Løvdal
2011-05-08 23:10 ` David Miller
2011-05-09 0:08 ` Håkon Løvdal
2011-05-09 0:12 ` David Miller
2011-05-09 1:30 ` Håkon Løvdal [this message]
2011-05-07 21:25 ` Jay Vosburgh
2011-05-07 21:37 ` Rafael Aquini
-- strict thread matches above, loose matches on Subject: below --
2011-05-10 0:08 Rafael Aquini
2011-05-10 0:15 ` Joe Perches
2011-05-10 2:00 ` Jay Vosburgh
2011-05-10 2:11 ` Joe Perches
2011-05-10 12:22 ` Rafael Aquini
2011-05-06 11:50 Rafael Aquini
2011-05-06 13:01 ` Nicolas Kaiser
2011-05-06 14:56 ` Rafael Aquini
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-all 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=BANLkTin77k5oHGvn-E-6asQForB62it1Yw@mail.gmail.com \
--to=hlovdal@gmail.com \
--cc=andy@greyhouse.net \
--cc=aquini@linux.com \
--cc=davem@davemloft.net \
--cc=fubar@us.ibm.com \
--cc=joe@perches.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=mfmooney@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=nikai@nikai.net \
--cc=shemminger@vyatta.com \
/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;
as well as URLs for NNTP newsgroup(s).