Velocity Reviews > Java > Freeing Algorithms

# Freeing Algorithms

Stefan Ram
Guest
Posts: n/a

 03-18-2006
Here is an algorithm for the determination of the leap-year
property of a year:

public static boolean
isLeapYear( final int yearNumber )
{ final boolean isQuad = yearNumber % 4 == 0;
final boolean isCentury = yearNumber % 100 == 0;
final boolean isQuadcentury = yearNumber % 400 == 0;
final boolean isCanceled = isCentury & !isQuadcentury;
final boolean isLeapYear = isQuad & !isCanceled;
return isLeapYear; }

But this will only handle years up to about 2^31-1 or so!

What will happen, when wants to use BigInteger for years?

The algorithm actually only uses the property of years to
be congruent to a number modulo another number:

public interface IsCongruentModuloIntInt
{ public boolean isCongruentModulo( int comparand, int modulus ); }

So there is no need to bind it to the special type »int« that
has many more features which are not needed here. The
following untested draft shows how the algorithm can be freed
from this binding.

public static boolean
( final IsCongruentModuloIntInt yearNumber )
{ final boolean isQuad = yearNumber.isCongruentModulo( 0, 4 );
final boolean isCentury = yearNumber.isCongruentModulo( 0, 100 );
final boolean isQuadcentury = yearNumber.isCongruentModulo( 0, 400 );
final boolean isCanceled = isCentury & !isQuadcentury;
final boolean isLeapYear = isQuad & !isCanceled;
result isLeapYear; }

If an algorithm needs more features of a year than just a
single feature, it can be written as follows stating exactly
which operations it needs (untested draft):

<T extends AddInt & IsPositive & DividedByInt >
public static int
difference( final T yearParameter )
{ if( yearParameter.isPositive() )yearParameter.addInt( - 1 )
quads = yearParameter.DividedByInt( 4 ); /* ... */ }

So each algorithm is generic to the maximum extend possible,
if it binds itself to the smallest interface it needs.

The interfaces »IsCongruentModuloIntInt« still is somewhat
bound to »int«, but the generalization achieved here
usually will be sufficient. A more general version could be:

public interface IsCongruentModulo<T,U,V>
{ public boolean isCongruentModulo( T comparand, U comparand1, V modulus ); }

An important part of this programming style was removed
state what the meaning of this operation is.

Rhino
Guest
Posts: n/a

 03-18-2006

"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> Here is an algorithm for the determination of the leap-year
> property of a year:
>
> public static boolean
> isLeapYear( final int yearNumber )
> { final boolean isQuad = yearNumber % 4 == 0;
> final boolean isCentury = yearNumber % 100 == 0;
> final boolean isQuadcentury = yearNumber % 400 == 0;
> final boolean isCanceled = isCentury & !isQuadcentury;
> final boolean isLeapYear = isQuad & !isCanceled;
> return isLeapYear; }
>
> But this will only handle years up to about 2^31-1 or so!
>
> What will happen, when wants to use BigInteger for years?
>
> The algorithm actually only uses the property of years to
> be congruent to a number modulo another number:
>
> public interface IsCongruentModuloIntInt
> { public boolean isCongruentModulo( int comparand, int modulus ); }
>
> So there is no need to bind it to the special type »int« that
> has many more features which are not needed here. The
> following untested draft shows how the algorithm can be freed
> from this binding.
>
> public static boolean
> ( final IsCongruentModuloIntInt yearNumber )
> { final boolean isQuad = yearNumber.isCongruentModulo( 0, 4 );
> final boolean isCentury = yearNumber.isCongruentModulo( 0, 100 );
> final boolean isQuadcentury = yearNumber.isCongruentModulo( 0, 400 );
> final boolean isCanceled = isCentury & !isQuadcentury;
> final boolean isLeapYear = isQuad & !isCanceled;
> result isLeapYear; }
>
> If an algorithm needs more features of a year than just a
> single feature, it can be written as follows stating exactly
> which operations it needs (untested draft):
>
> <T extends AddInt & IsPositive & DividedByInt >
> public static int
> difference( final T yearParameter )
> { if( yearParameter.isPositive() )yearParameter.addInt( - 1 )
> quads = yearParameter.DividedByInt( 4 ); /* ... */ }
>
> So each algorithm is generic to the maximum extend possible,
> if it binds itself to the smallest interface it needs.
>
> The interfaces »IsCongruentModuloIntInt« still is somewhat
> bound to »int«, but the generalization achieved here
> usually will be sufficient. A more general version could be:
>
> public interface IsCongruentModulo<T,U,V>
> { public boolean isCongruentModulo( T comparand, U comparand1, V
> modulus ); }
>
> An important part of this programming style was removed
> state what the meaning of this operation is.
>

Do you really think the average Java programmer will ever care whether the
year 23456543215678908765432124354667 is a leap year? Surely no one alive
today is likely to care whether any given year much beyond their lifetime is
a leap year; barring truly miraculous achievements in medical science, no
one on this newsgroup is likely to live much past 2100, let alone to a time
where the year exceeds 2^31.

Now, if you simply started this thread to make a purely intellectual
programming point and then chose a questionable example, forgive my remarks.

--
Rhino

Luc The Perverse
Guest
Posts: n/a

 03-18-2006
"Rhino" <(E-Mail Removed)> wrote in message
news:6kYSf.7740\$(E-Mail Removed).. .
>
> "Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
> news:(E-Mail Removed)-berlin.de...
>> Here is an algorithm for the determination of the leap-year
>> property of a year:
>>
>> public static boolean
>> isLeapYear( final int yearNumber )
>> { final boolean isQuad = yearNumber % 4 == 0;
>> final boolean isCentury = yearNumber % 100 == 0;
>> final boolean isQuadcentury = yearNumber % 400 == 0;
>> final boolean isCanceled = isCentury & !isQuadcentury;
>> final boolean isLeapYear = isQuad & !isCanceled;
>> return isLeapYear; }
>>
>> But this will only handle years up to about 2^31-1 or so!
>>
>> What will happen, when wants to use BigInteger for years?
>>
>> The algorithm actually only uses the property of years to
>> be congruent to a number modulo another number:
>>
>> public interface IsCongruentModuloIntInt
>> { public boolean isCongruentModulo( int comparand, int modulus ); }
>>
>> So there is no need to bind it to the special type »int« that
>> has many more features which are not needed here. The
>> following untested draft shows how the algorithm can be freed
>> from this binding.
>>
>> public static boolean
>> ( final IsCongruentModuloIntInt yearNumber )
>> { final boolean isQuad = yearNumber.isCongruentModulo( 0, 4 );
>> final boolean isCentury = yearNumber.isCongruentModulo( 0, 100 );
>> final boolean isQuadcentury = yearNumber.isCongruentModulo( 0, 400 );
>> final boolean isCanceled = isCentury & !isQuadcentury;
>> final boolean isLeapYear = isQuad & !isCanceled;
>> result isLeapYear; }
>>
>> If an algorithm needs more features of a year than just a
>> single feature, it can be written as follows stating exactly
>> which operations it needs (untested draft):
>>
>> <T extends AddInt & IsPositive & DividedByInt >
>> public static int
>> difference( final T yearParameter )
>> { if( yearParameter.isPositive() )yearParameter.addInt( - 1 )
>> quads = yearParameter.DividedByInt( 4 ); /* ... */ }
>>
>> So each algorithm is generic to the maximum extend possible,
>> if it binds itself to the smallest interface it needs.
>>
>> The interfaces »IsCongruentModuloIntInt« still is somewhat
>> bound to »int«, but the generalization achieved here
>> usually will be sufficient. A more general version could be:
>>
>> public interface IsCongruentModulo<T,U,V>
>> { public boolean isCongruentModulo( T comparand, U comparand1, V
>> modulus ); }
>>
>> An important part of this programming style was removed
>> state what the meaning of this operation is.
>>

> Do you really think the average Java programmer will ever care whether the
> year 23456543215678908765432124354667 is a leap year? Surely no one alive
> today is likely to care whether any given year much beyond their lifetime
> is a leap year; barring truly miraculous achievements in medical science,
> no one on this newsgroup is likely to live much past 2100, let alone to a
> time where the year exceeds 2^31.
>
> Now, if you simply started this thread to make a purely intellectual
> programming point and then chose a questionable example, forgive my
> remarks.

We don't need another Y2K problem!

I don't know if you had airplanes falling out of the sky into your backyard,
but that was scary for me.

--
LTP

Stefan Ram
Guest
Posts: n/a

 03-18-2006
"Rhino" <(E-Mail Removed)> writes:
>Now, if you simply started this thread to make a purely
>intellectual programming point and then chose a questionable
>example, forgive my remarks.

To make this point, it was not necessary to quote the full
text of my article. I suggest that you consider to quote only
a relevant part of an article you respond to.

The example was chosen to talk about the usage of algorithms
bound to interfaces only. I agree that there usually is no
need to find the leap year property for very large year numbers.

Luc The Perverse
Guest
Posts: n/a

 03-18-2006
"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> "Rhino" <(E-Mail Removed)> writes:
>>Now, if you simply started this thread to make a purely
>>intellectual programming point and then chose a questionable
>>example, forgive my remarks.

>
> To make this point, it was not necessary to quote the full
> text of my article. I suggest that you consider to quote only
> a relevant part of an article you respond to.

You should pick your battles - there are people who are top posting and
people not quoting at all that are the real problem.

--
LTP

"I'm not part of the problem; I'm Republican." - Dubyai Bush

Alan Krueger
Guest
Posts: n/a

 03-18-2006
Stefan Ram wrote:
> But this will only handle years up to about 2^31-1 or so!

That only gives us until 2,147,483,647 AD. That's not enough to last
until our local star goes through its red giant stage.

Stefan Ram
Guest
Posts: n/a

 03-18-2006
"Luc The Perverse" <(E-Mail Removed)> writes:
>>To make this point, it was not necessary to quote the full
>>text of my article. I suggest that you consider to quote only
>>a relevant part of an article you respond to.

So helping someone to improve his articles is regarded
as a »battle« by you.

Luc The Perverse
Guest
Posts: n/a

 03-18-2006
"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> "Luc The Perverse" <(E-Mail Removed)> writes:
>>>To make this point, it was not necessary to quote the full
>>>text of my article. I suggest that you consider to quote only
>>>a relevant part of an article you respond to.

>
> So helping someone to improve his articles is regarded
> as a »battle« by you.

You mean you have never fought trolls - who insist that their 3 posts of
usenet experience is vastly superior to the cumulative tens of thousands of
posts by the group regulars?

Yes - telling someone they are posting wrong is an attack. And you
shouldn't attack people for doing everything right except a little trimming.

--
LTP

Rhino
Guest
Posts: n/a

 03-18-2006

"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> "Rhino" <(E-Mail Removed)> writes:
>>Now, if you simply started this thread to make a purely
>>intellectual programming point and then chose a questionable
>>example, forgive my remarks.

>
> To make this point, it was not necessary to quote the full
> text of my article. I suggest that you consider to quote only
> a relevant part of an article you respond to.
>

Some people yell at you when you snip posts and some people yell at you when
you don't. It's hard to know what will please people sometimes. Sorry for
guessing incorrectly.

> The example was chosen to talk about the usage of algorithms
> bound to interfaces only. I agree that there usually is no
> need to find the leap year property for very large year numbers.
>

Fair enough

--
Rhino

Rhino
Guest
Posts: n/a

 03-18-2006

"Luc The Perverse" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
> news:(E-Mail Removed)-berlin.de...
>> "Luc The Perverse" <(E-Mail Removed)> writes:
>>>>To make this point, it was not necessary to quote the full
>>>>text of my article. I suggest that you consider to quote only
>>>>a relevant part of an article you respond to.

>>
>> So helping someone to improve his articles is regarded
>> as a »battle« by you.

>
> You mean you have never fought trolls - who insist that their 3 posts of
> usenet experience is vastly superior to the cumulative tens of thousands
> of posts by the group regulars?
>
> Yes - telling someone they are posting wrong is an attack. And you
> shouldn't attack people for doing everything right except a little
> trimming.
>

Thanks Luc but I don't consider myself attacked; Stefan was making a
reasonable point.

I'm afraid I have been getting lax about snipping lately. I've had people
"yelling" at me for snipping; one guy even _killfiled_ me because I had
snipped a previous poster's _sig_, not the actual content of his post. I
suppose I've been erring on the side of caution as a result.

Netiquette is a bit of a balancing act at the best of times; there is not
always a universal agreement on what is right or wrong. As long as we are
reasonable human beings, we can usually find a middle ground that most
people can live with. I'll try to snip more in future

--
Rhino