Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.16.MISMATCH!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: alias for Integer Date: Fri, 18 Nov 2011 07:34:09 -0800 (PST) Organization: http://groups.google.com Lines: 93 Message-ID: <12712433.869.1321630449979.JavaMail.geo-discussion-forums@prmf13> References: <21374513.220.1321627842805.JavaMail.geo-discussion-forums@yqmj32> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 173.164.137.214 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1321630528 2921 127.0.0.1 (18 Nov 2011 15:35:28 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 18 Nov 2011 15:35:28 +0000 (UTC) In-Reply-To: <21374513.220.1321627842805.JavaMail.geo-discussion-forums@yqmj32> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10045 jrobinss wrote: > I am processing structures that contain integers, structures such as matr= ixes used in statistical analysis. As I implement these as Maps of indexes,= I use the class Integer, as in >=20 > // Map <- I'd like to remove this comment! > public static Map myMap =3D ...; >=20 > Now what I'd like is to write > public static Map myMap =3D ...; >=20 > The advantage is that the code is auto-documented, but even better that t= he compiler will check that I never get mixed up in different indexes, by r= elying on strong typing. >=20 > Usually, I would do this by extending the relevant class. But here it's I= nteger, which is final. That is kind of an antipattern. Joshua Bloch suggest to "Prefer compositio= n to inheritance" in /Effective Java/ (2nd ed.). You could write a wrapper= for 'Integer', but then 'Integer' already *is* a wrapper type. > Questions:=20 > 1. is this a bad good idea, and I should proceed with Integer? It's not a terrible idea, necessarily, but it seems unnecessary.=20 > 2. if not, how would you implement this? I would pick a type that actually helps. I don't see what you object to in= 'Integer', quite frankly. > 3. is there any performance issue in defining my own classes instead of I= nteger? How could anyone possibly know the answer to this question? > My current solution is to define my own classes for replacing Integer, su= ch as: >=20 > public final class MatrixIndex { > public final int value; > public MyIndex(int i) {value =3D i;} > } Congratulations, you just re-invented 'Integer', but without any of its fea= tures. Now you have to go through all sorts of gyrations to translate your custom = class to and from 'int'. Your code complexity goes up, and you have to mai= ntain your own custom substitute for a fundamental API type. Thus your ris= k of bugs and code-maintenance costs skyrocket. How again does this help you? > I won't benefit from autoboxing, then... :-( No one does. > (I'm just hoping it doesn't break too much of the code, because even thou= gh it's mine to break, I don't have infinite time) Why waste *any* time on this? Just use 'Integer'. > Note that (exceptionnally for me) performance *is* an issue here. I haven= 't yet narrowed it down, but the code executes very slowly and eats up much= too much memory at the moment. I'm starting to optimize it, that's why I'm= starting with strongly typing it to prevent errors. "Optimize" and "prevent errors" are, at best, orthogonal, and at worst (and= typically in this kind of premature action) the former interferes with the= latter. Yet you say them in the same breath as though they were the same thing. They aren't. Your "strong typing" isn't, really. 'Integer' already is a strong type. As for what you choose to "optimize", what evidence (that is, factual data = derived from actual tests whose protocols are publicized) do you have that = you are attacking the slow parts? IOW, what /actual/ tests have you performed, and how did you control the va= riables like system load, HotSpot warmup and application load? You have a performance problem. So you decide to obfuscate random parts of= your code base with zero foundation for your actions. Now you have two pr= oblems. --=20 Lew