Smelly Coding

Star-Dust

Expert
Licensed User
Longtime User
a lib less one liner based on @Star-Dust 's excellent (fixed) code...

I hope it fits the width of this forum ;)

B4X:
Public Sub dayOfYear (month As Int, dayOfMonth As Int,year As Int) As Int
    Return dayOfMonth+Bit.ParseInt(Regex.Split(",","0,0,31,59,90,120,151,181,212,243,273,304,335")(month),10)+2*(0.5-Min((year Mod 4),0.5)+Min((year Mod 100),0.5)-Min((year Mod 400),0.5))*Floor(Min(month,2)/2)
End Sub
This version is extraordinary, I like it a lot
 

sorex

Expert
Licensed User
Longtime User
a few brackets too much so it can be 6 characters shorter or so but it's a habbit to use enough of them and rely on the bracket matcher.
 

DonManfred

Expert
Licensed User
Longtime User
I hope it fits the width of this forum ;)
even on a smaller window the CODE is scrollable horizontal (still one line) ;-)
 

OliverA

Expert
Licensed User
Longtime User

sorex

Expert
Licensed User
Longtime User
No problem, Oliver.

I didn't read the article till now but that "rule" spoils this challenge if you have to declare everything as explaining variables first.
 

Star-Dust

Expert
Licensed User
Longtime User
See "Avoid Magic Numbers" in the link provided by Ilan. I'm not knocking the code, I love it, but it does violate the spirit of that section of the linked article.
There is no magic number that appears without explanation.

It is simply a formula, like Zeller's congruence formula, which is used to calculate the day of the week of each date. They are not magic numbers, but mathematical formulas.

I do not give explanations just because I do not write well English and I get really complicated, not because it is a 'magic number' which is not explained. (Actually I do not express well even in my mother tongue, except for the Qbasic boasted an excellent pronunciation)
 

sorex

Expert
Licensed User
Longtime User
if you know the rules your code makes sense so yes it's just taking advantage of math trickery.

you can do a libless oneliner without the need of the extra date functions lib aswell but then you rely on the core date functions and I doubt it was allowed aswell
(a modded version of what Luka posted where you count days between Jan 1st and the given date of that year)
 
Top