Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36165
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: can someone chk is this code ok and if i can make it better please state how |
| Date | 2017-01-06 16:15 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <o4p1ae$4bv$1@dont-email.me> (permalink) |
| References | <e69219b6-6d90-4d99-9e0c-485e867fded4@googlegroups.com> |
On 1/6/2017 3:38 PM, SuperGiirl wrote:
For future reference: It's best not to rely on the "Subject" line
to convey your question. Rather, use "Subject" simply as a title and
ask your question in the body of the message.
That said, the code is "OK" but there are opportunities to improve
it in small ways. I'll point out a few ...
> public class Gadget
> {
> private int Weight;
> private double Price;
> private String Model;
> private String Size;
It is customary ("conventional") to use Initial Capital Letters for
the names of classes and interfaces, but to use initial lowercase for
variables and methods. The code is "OK" in the sense that it compiles
and executes without trouble, but would be more readable if you used the
naming conventions readers are familiar with. ifyoudontthinkclarityis
atleastinpartamatterofconventionthinkagain For more information, see
http://www.oracle.com/technetwork/java/codeconventions-135099.html
Also, if the values are never intended to change after being set in
the constructor (and you show no code that might change them), it would
be a good idea to declare them as `final' variables.
> public Gadget(int theWeight, double thePrice, String theModel, String theSize)
> {
> Weight = theWeight;
> Price = thePrice;
> Model = theModel;
> Size = theSize;
This would be a good place to perform sanity checks on the values:
A negative weight is probably nonsensical, as is an infinite price or
a null or absent model name and size. The code would be more "robust"
if the constructor checked such things and objected to craziness by
throwing an exception:
if (theWeight <= 0)
throw new IllegalArgumentException(
"bad weight: " + theWeight);
if (thePrice <= 0.0 || !Double.isFinite(thePrice))
throw new IllegalArgumentException(
"bad price: " + thePrice);
... and so on. The principle is that the sooner a mistake is found,
the easier it will be to fix. If a crazy price puts up a red flag as
soon as it appears, it'll be easier to figure out where it's coming
from than if it slips quietly into the mix along with thousands of
other Gadgets and your only clue is an absurd cash-flow total at the
end of the quarterly inventory reconciliation report.
> }
>
> public int getTheWeight()
> {
>
> return Weight;
> }
> public double getThePrice()
> {
> return Price;
> }
> public String getTheModel()
> {
> return Model;
> }
> public String getTheSize()
> {
> return Size;
> }
The "The" parts of these method names don't communicate much, and
seem to be little more than clutter. (Why not "getThatWeight" or
"getAPrice"?) It would be more usual to use names like "getModel"
and "getSize" instead.
Summary: OK, but improvable.
--
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
can someone chk is this code ok and if i can make it better please state how SuperGiirl <kainattshafiq90@gmail.com> - 2017-01-06 12:38 -0800
Re: can someone chk is this code ok and if i can make it better please state how SuperGiirl <kainattshafiq90@gmail.com> - 2017-01-06 12:52 -0800
Re: can someone chk is this code ok and if i can make it better please state how Andreas Leitgeb <avl@logic.at> - 2017-01-06 21:11 +0000
Re: can someone chk is this code ok and if i can make it better please state how SuperGiirl <kainattshafiq90@gmail.com> - 2017-01-06 14:02 -0800
Re: can someone chk is this code ok and if i can make it better please state how Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-01-06 16:15 -0500
Re: can someone chk is this code ok and if i can make it better please state how Robert Klemme <shortcutter@googlemail.com> - 2017-01-07 14:16 +0100
Re: can someone chk is this code ok and if i can make it better please state how lewbloch@gmail.com - 2017-01-06 17:23 -0800
Re: can someone chk is this code ok and if i can make it better please state how Robert Klemme <shortcutter@googlemail.com> - 2017-01-07 14:23 +0100
Re: can someone chk is this code ok and if i can make it better please state how galt_57@hotmail.com - 2017-01-07 11:39 -0800
csiph-web