X-Received: by 2002:ac8:1771:: with SMTP id u46mr65138304qtk.186.1555508562304; Wed, 17 Apr 2019 06:42:42 -0700 (PDT) X-Received: by 2002:a81:234c:: with SMTP id j73mr69688215ywj.327.1555508562069; Wed, 17 Apr 2019 06:42:42 -0700 (PDT) Path: csiph.com!xmission!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!t9no1917759qtn.0!news-out.google.com!i54ni893qte.0!nntp.google.com!t9no1917755qtn.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Wed, 17 Apr 2019 06:42:41 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.78.95.177; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H NNTP-Posting-Host: 50.78.95.177 References: <109a7224-f392-46c3-bf56-7d8bf2bfb4bf@googlegroups.com> <13t97bxk9km7n$.ooqi8n8ocm7f$.dlg@40tude.net> <13ak6boysjgrh$.1h4x7tkjgs00q$.dlg@40tude.net> <7ivllvz2ukra$.qlcrlm81adao.dlg@40tude.net> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <28f31a7a-5bdf-4eaa-ad70-df6c0751b5ff@googlegroups.com> Subject: Re: Java variable access From: Eric Douglas Injection-Date: Wed, 17 Apr 2019 13:42:42 +0000 Content-Type: text/plain; charset="UTF-8" Lines: 24 Xref: csiph.com comp.lang.java.programmer:38923 On Tuesday, April 16, 2019 at 4:46:33 PM UTC-4, Eric Sosman wrote: > What is wrong (tm) with > > public void setUnitPrice(double unitPrice) { > if (unitPrice <= 0 || !Double.isFinite(unitPrice)) { > throw new IllegalArgumentException( > "bad price: " + unitPrice); > } > if (unitPrice != this.unitPrice) { > this.unitPrice = unitPrice; > fireChangeEvent(this, PRICE_CHANGED); > } > } > If you leave off the validation and just declare class variable public double unitPrice, you either validate it where it gets used or never validate assuming it's ok to just throw an error where it would be used if it somehow gets an invalid value. If you would have a number of variables that you want the same exact validation you could build in a private method: public void setUnitPrice(double unitPrice) { setPositiveValue(values.UNITPRICE,unitPrice); } (where UNITPRICE is an enum) ? or you could just use generated code, with a UI (IDE) or Eclipse plugin type thing. We do use several languages at my company and one of them we did write our own custom IDE (in C#) which inserts certain common code blocks which have to be in every program but would be tedious to write.