Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2FB10D7C4 for ; Wed, 5 Sep 2012 20:03:17 +0000 (UTC) Received: (qmail 78763 invoked by uid 500); 5 Sep 2012 20:03:16 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 78575 invoked by uid 500); 5 Sep 2012 20:03:16 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 78566 invoked by uid 99); 5 Sep 2012 20:03:16 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Sep 2012 20:03:16 +0000 X-ASF-Spam-Status: No, hits=0.3 required=5.0 tests=FREEMAIL_REPLY,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of sebbaz@gmail.com designates 209.85.212.43 as permitted sender) Received: from [209.85.212.43] (HELO mail-vb0-f43.google.com) (209.85.212.43) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Sep 2012 20:03:12 +0000 Received: by vbbfq11 with SMTP id fq11so673937vbb.30 for ; Wed, 05 Sep 2012 13:02:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=fz+UGsMl94m6Qdu8OdfOh2YJWw9uGXGSngPTLrSe5GA=; b=ron62PxI5lAYBmqt/FZLTfQk8hhGoQBYocqxdath77JDuP611c9dv7lUHsaRRyvXZ/ HINiqAo2TSCDxYPIoAQcGWsFHtul0R7rvJZEdwDdm1t+/3T48zPH6X/eugwZAVTgVf2w tiDrt+2n8PLbUerGDbAo7FvHyLQnvMT3PJ/bnCS0PSisdKlX/met7MFQ0HXi1Aj8WkDq HC9HA7gHuev0k6UZgxVz9cG3yvh3uthsI8U16Cl6941qEhkYeT9T1ZiLeIctHTkXkNnN zGjTr9nTJfJfum/3vs9IjAMyXIDp2TsOAAnZGW9tntkI4J+89ghbeLxTGNrIeG5vmAva 7SVQ== MIME-Version: 1.0 Received: by 10.58.209.73 with SMTP id mk9mr22310463vec.25.1346875371217; Wed, 05 Sep 2012 13:02:51 -0700 (PDT) Received: by 10.58.58.197 with HTTP; Wed, 5 Sep 2012 13:02:51 -0700 (PDT) In-Reply-To: References: <20120904230200.GA24856@dusk.harfang.homelinux.org> Date: Wed, 5 Sep 2012 21:02:51 +0100 Message-ID: Subject: Re: [all] FindBugs' Switch statement found in class.method where default case is missing From: sebb To: Commons Developers List Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Checked: Checked by ClamAV on apache.org Seems to me there are several reasons why the default case may have been omitted: 1) It was accidentally omitted. In this case, add the required clause. 2) It was omitted because nothing needs to be done. In this case, this needs to be documented; the easiest way is to add: default: break; // nothing needs to be done here 3) It was omitted because "it cannot happen" In this case, add an assertion or throw an error. This is so that a future change which invalidates the assumption is not overlooked, but is caught. It's important that the person reading the code knows that the switch block is complete. So fixing the problem by changing CheckStyle or Findbugs configuration files is not sufficient; there must be at least a comment in the code. On 5 September 2012 01:04, Liviu Tudor wrote: > Hi guys, > > I am looking at this from a different perspective: the same check can be > performed using checkstyle > (http://checkstyle.sourceforge.net/config_coding.html#MissingSwitchDefault) > as well as FindBugs. So if this is a valid case where there is no need > for a default branch, we could perhaps simply use a configured checkstyle > comment to instruct checkstyle that this check doesn't need to be > performed in this case? > > If this definitely needs FindBugs, I know that FindBugs supports > annotations, so it might be worth looking into that, though I don't know > off the top of my head if there is one available for default missing in a > switch. > > > Liv > > Liviu Tudor > > E: liviu.tudor@gmail.com > C: +1 650-2833815 (USA) > M: +44 (0)7591540313 (UK, Europe) > W: http://about.me/liviutudor > Skype: liviutudor > > I'm nobody, nobody's perfect -- therefore I'm perfect! > > > > > > > > On 04/09/2012 16:02, "Gilles Sadowski" > wrote: > >>> > > >>> > > FindBugs can give warnings like: >>> > > >>> > > Switch statement found in >>> > > org.apache.commons.codec.binary.Base32.decode(byte[], int, int, >>> > > BaseNCodec$Context) where default case is missing >>> > > >>> > > In this case for [codec], it looks like the code was carefully >>> > constructed >>> > > and that no default clause is needed. >>> > > >>> > > In these cases for any component, this FindBugs issue feels like a >>>style >>> > > issue, is it worth changing the code to add a default clause like: >>> > > >>> > > default: >>> > > // ok to fall through for other values. >>> > > break; >>> > > >>> > > Or does this feel like noise to you all? >>> > >>> > In Math, there is this kind of code: >>> > ---CUT--- >>> > switch (method) { >>> > case ILLINOIS: >>> > f0 *= 0.5; >>> > break; >>> > case PEGASUS: >>> > f0 *= f1 / (f1 + fx); >>> > break; >>> > case REGULA_FALSI: >>> > // Detect early that algorithm is stuck, instead >>>of >>> > // waiting >>> > // for the maximum number of iterations to be >>>exceeded. >>> > if (x == x1) { >>> > throw new ConvergenceException(); >>> > } >>> > break; >>> > default: >>> > // Should never happen. >>> > throw new MathInternalError(); >>> > } >>> > ---CUT--- >>> > >>> > >>> What about the opposite case: >>> >>> We do not care about the other values than the ones in each switch case. >>> >> >>Wouldn't adding an empty "default" be enough to mean that (and silence >>FindBugs)? >> >> >>Regards, >>Gilles >> >>--------------------------------------------------------------------- >>To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org >>For additional commands, e-mail: dev-help@commons.apache.org >> > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > For additional commands, e-mail: dev-help@commons.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org