Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Style police (Was: Re: How to handle same sanity checks) Date: Sun, 25 Jun 2017 19:14:41 -0400 Organization: Aioe.org NNTP Server Lines: 62 Message-ID: References: <87o9tgo0a9.fsf@munus.decebal.nl> NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 X-Notice: Filtered by postfilter v. 0.8.2 Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:36723 On 6/25/2017 5:28 PM, Eric Sosman wrote: > On 6/25/2017 2:23 PM, Arne Vajhøj wrote: >> On 6/25/2017 6:31 AM, Robert Klemme wrote: >>> [...] >>> A few minor remarks: brackets around comparisons are not needed. >> >> Not necessary. >> >> But actually recommended in the traditional Java coding convention. >> >> >> 10.5.1 Parentheses >> >> It is generally a good idea to use parentheses liberally in >> expressions involving mixed operators to avoid operator precedence >> problems. Even if the operator precedence seems clear to you, it might >> not be to others-you shouldn't assume that other programmers know >> precedence as well as you do. >> >> if (a == b && c == d) // AVOID! >> if ((a == b) && (c == d)) // RIGHT >> >> > > Is anybody aware of any actual research into the value of > this "generally good" practice, or does "generally good" merely > mean "it's my prejudice, so as author I'll claim it's good?" Many of these best practices are very hard to empirically prove. So it end up with whether you trust the people preaching the best practice. This one is also in McConnell's Code Complete. Section 31.2: Use more parentheses than you think you need. Use parentheses to clarify expressions that involve more than two terms. They may not be needed, but they add clarity and they don't cost you anything. I have noticed that it is a very common problem among beginner programmers to get unexpected results due to lack of parentheses. For whatever reason SQL seems particular prone to this error. Whether you would let your coding style influence by: * The gentleman Peter King, Patrick Naughton, Mike DeMoney, Jonni Kanerva, Kathy Walrath, and Scott Hommel from SUN (that apperently contributed to Java coding convention) * McConnel * a random dude on the internet: me is up to you. Arne