> How do I make my count() method prettier?
Depends on what you find "pretty". You can certainly avoid testing each
variable twice, though.
> public int count() {
> if(a == null && b == null)
[quoted text clipped - 4 lines]
> return 1;
> }
public int count()
{
return (a == null? 0 : 1) + (b == null? 0 : 1);
}

Signature
Lew
Patricia Shanahan - 23 Feb 2008 18:26 GMT
>> How do I make my count() method prettier?
>
[quoted text clipped - 14 lines]
> return (a == null? 0 : 1) + (b == null? 0 : 1);
> }
public int count(){
int result = 0;
if(a != null){
result++;
}
if(b != null){
result++;
}
return result;
}
also avoids testing twice. I find it a bit more readable, and it is
obvious how to extend it to deal with more references, for example an array.
Patricia
fernhom@gmail.com - 23 Feb 2008 18:35 GMT
> > fern...@gmail.com wrote:
> >> How do I make my count() method prettier?
[quoted text clipped - 30 lines]
> also avoids testing twice. I find it a bit more readable, and it is
> obvious how to extend it to deal with more references, for example an array.
Simple as result + 1 :)
yep,could need to extend
thanks
> Patricia
fernhom@gmail.com - 23 Feb 2008 18:27 GMT
> fern...@gmail.com wrote:
> > How do I make my count() method prettier?
[quoted text clipped - 16 lines]
>
> }
Simple as 1+1 :)
thanks
> Lew
> How do I make my count() method prettier?
> TIA fh
[quoted text clipped - 34 lines]
> }
> }
public class ObjectCounter {
Map<String, Object> obj = new HashMap<String, Object>();
public int count() {
return obj.size();
}
// ...
}

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>