Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10046

Re: alias for Integer

From jrobinss <julien.robinson2@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: alias for Integer
Date 2011-11-18 08:26 -0800
Organization http://groups.google.com
Message-ID <19404286.1644.1321633587044.JavaMail.geo-discussion-forums@yqhd1> (permalink)
References <21374513.220.1321627842805.JavaMail.geo-discussion-forums@yqmj32> <12712433.869.1321630449979.JavaMail.geo-discussion-forums@prmf13>

Show all headers | View raw


Aaah, it had been some time, but I'm not disapointed. :-)

I'm sorry Lew, but I feel that I haven't been quite clear.

This is a large piece of code, not extraordinary but still a reasonable size, and I didn't write it. I end up manipulating indexes all over the place, so that I have for example a table (or map or whatever) that associates database indexes to matrix indexes, or identifiers to indexes, etc. These are all ints or Integers, and take part in ringamaroles of loops, indexes of indexes of arrays of arrays and such joyous constructs. In short, it's a lot of ints and hard to understand.

That these indexes and identifiers are all integers is in fact nearly a coincidence (it *is* very handy to reuse matrix libraries), conceptually they are not the same thing; the ID of an object could be a string, it just happens to be an int. I am certainly not obfuscating my code by replacing a type such as Integer with DbIndex or MyObjId, on the contrary I am stating to everyone, including the compiler, that this should be congruent with indexes, not to matrix sizes or number of chars in string of what-have-we-nots. The error I am trying to avoid is to use a DB index in place of a matrix index in the middle of a large set of loops.
(of course a nice side effect is that I could replace some IDs with strings or objects or anything, benefiting from encapsulation, but here it's not the objective)

See this as the same as when you call a method that takes ten ints as entry params: the main risk of error is to get confused in the order of parameters, and nothing will warn you except some strange bug a year later. A way to avoid it is to type strongly parameters, so that the caller may call
  new Rect(new Rect.Length(x), new Rect.Width(y))
instead of
  new Rect(x, y)
which is kind of verbose overkill in this particular example of course, but it's just an illustration.

Ok, so that's the reason why the typing.

As for the optimisation, I am perfectly aware that one of the most-cited mottos here is "premature optimization is the root of all evil" (with which I wholeheartedly agree, BTW). I am not prematurely optimizing: I am about to optimize some code that I didn't write, and it's certainly not premature because the code *is* slow. For me, this always starts with a bit of local rewriting in order to better understand the code and to guarantee that my changes won't break anything, in particular by letting the compiler help me out. The o-word, which acts as a kind of magnet for your reactions, was not the core subject of my post, which is probably why you feel you don't have any elements about it: I didn't provide any, because that was not my question. I merely stated that performances were potentially an issue, because that is generally one of the parameters to take into account when choosing a particular implementation.

I hope this clarifies.

Many thanks to you, Lew, and also to Roedy and Mark for answering. I'll keep your answers in mind while I progress.

For those who wonder about structures and matrixes etc, I'm starting by replacing types, but I may do another code writing iteration where I replace structures. So that it may go like this...
  original code: Blob<Integer>
  better: Blob<MyIndex>
  even better: MyBlob extends Blob<Integer>, or extends Blob<MyIndex>
  gettin' better: MyBlob with methods using ints, bye bye boxing :-)

JRobinss

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

alias for Integer jrobinss <julien.robinson2@gmail.com> - 2011-11-18 06:50 -0800
  Re: alias for Integer markspace <-@.> - 2011-11-18 07:11 -0800
  Re: alias for Integer Roedy Green <see_website@mindprod.com.invalid> - 2011-11-18 07:32 -0800
  Re: alias for Integer Lew <lewbloch@gmail.com> - 2011-11-18 07:34 -0800
    Re: alias for Integer jrobinss <julien.robinson2@gmail.com> - 2011-11-18 08:26 -0800
      Re: alias for Integer Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-11-18 18:00 +0000
      Re: alias for Integer markspace <-@.> - 2011-11-18 10:17 -0800
      Re: alias for Integer Roedy Green <see_website@mindprod.com.invalid> - 2011-11-18 20:30 -0800
    Re: alias for Integer Roedy Green <see_website@mindprod.com.invalid> - 2011-11-18 20:23 -0800
  Re: alias for Integer Jim Janney <jjanney@shell.xmission.com> - 2011-11-18 13:14 -0700
  Re: alias for Integer Patricia Shanahan <pats@acm.org> - 2011-11-18 12:43 -0800
  Re: alias for Integer jrobinss <julien.robinson2@gmail.com> - 2011-11-21 01:50 -0800
  Re: alias for Integer Robert Klemme <shortcutter@googlemail.com> - 2011-11-21 20:58 +0100
  Re: alias for Integer David Lamb <dalamb@cs.queensu.ca> - 2011-11-21 15:38 -0500

csiph-web