Difference: JavaNotes (9 vs. 10)

Revision 1020 Feb 2003 - TobyCabot

Line: 1 to 1
 
META TOPICPARENT name="ProgrammingTips"
(back to ProgrammingTips)
Line: 40 to 40
  When something is an identifier, and you feel that you must append "Id" in some form to the name, use ID not Id. It's easier to type and reflects that most people call it an "eye-dee" not an "id".
Changed:
<
<
When a class has a member variable that holds multiple items, don't make a setFoo(Collection foos) method, make an addFoo(Foo foo) method instead. Maintain the collection (or set or whatever) internally. The difference is that there's some type checking in the second approach whereas the first approach lets the client pass a Collection full of anything in. It's also often more convenient for the client to call add() a few times rather than create their collection, fill it, and then set() it. If they want to look at it you can return either a collection/set/list (some interface) or you can return an iterator of some sort.
>
>
When a class has a member variable that holds multiple items, don't make a setFoo(Collection foos) method, make an addFoo(Foo foo) method instead. Maintain the collection (or set or whatever) internally. The difference is that there's some type checking in the second approach whereas the first approach lets the client pass a Collection full of anything in. It's also often more convenient for the client to call add() a few times rather than create their collection, fill it, and then set() it. If they want to look at it you can return either a collection/set/list (some interface) or you can return an array or iterator of some sort.
 

Misc

Line: 61 to 61
  Java is very resource-intensive: cpu, memory, and processes. So you might bump into the limits that Unix uses to limit individual user resource consumption. An important one is max user processes which you can see if you run ulimit -a. You probably want to bump this up to 1020 or so: ulimit -u 1020. Other limits that you might bump into are SHMMAX and SHMANY which you can set using files in /proc/sys/kernel/ or by setting values in /etc/sysctl.conf.
Added:
>
>

Random Musings

Why don't people use arrays in Java? It seems to me that people get so excited about Collection, Set, List etc that they forget about arrays. Arrays have some definite issues, but they also have some advantages. Arrays are fixed-length so they're not good if you're not sure how many things that you're going to put into them in advance. Their big advantage (which limits their use) is that they're strongly typed. This can be a big advantage in many cases, i.e. where you've got a bunch of the same thing. In this case an array is nice because it enforces type safety in a way that a Collection doesn't.

In summary, an array is the correct data structure to use where you have a known number of objects all of the same type.

 -- TobyCabot - 28 Dec 2001-31 May 2002
View topic | History: r36 < r35 < r34 < r33 | More topic actions...
Copyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding The Caboteria? Send feedback