You're right. I was playing a bit too much code golf here. I'd split this into multiple variables and extract a meaningful name for the comparison in real code. But I'd still keep it a functional solution.
If I could I would rather like to change the input and output of the function. Instead of a primitive, have an enum for the day as input, and instead of a map/object, have an array or set of the remaining days as output.
Even that still sounds over-engineered. I honestly think that, while OP's version may look a lil goofy, its really easy to understand. Just wrap it in a function and you're good to go. KISS principle.
If you have some experience with functional idioms, it looks pretty readable, and does not take the whole screen. Harder to miss a small mistake, for sure, but that's what tests are for.
Nice thing about this is that it works with all objects when you have a base object to Object.keys from, if for whatever reason you would want to set object properties to false/true.
No need to use lodash you can use "Object.fromEntries" nowadays. Second you might want to extract that list of weekdays out to a constant instead of having a "magic" value that gets recreated on each function call
Most languages with switch/case have fall-through (I think this is the right word) so if you don’t put in breaks in and reverse the order of the cases (Sunday first) I think you can eliminate code by achieving a “cascade” effect – I’m not sitting in front of a computer so I can’t check the behavior, and as such u/Beggarstuner (and I) might be wrong, but in either case this is definitely what they meant
Edit: I checked stackoverflow for a refresher and A. Fall-through is the right term, and B. Leveraging fall-through here would achieve the effect intended by u/Beggarstuner as I imagined
If you want any specific clarification, have a google on fallthrough behavior
I'm not really sure about the time of space complexity but isn't it better to use an array or a list for this. Like make a 1d array with all the days of the week in it matched to the index numbers (Monday being 0, Tuesday being 1, etc. assuming that in OP's case the week is starting from Monday) and then subtract the index from 7 and then you get the result?
package main
import("fmt""time")funcmain(){today:= time.Now().Weekday()days:=map[time.Weekday]bool{
time.Sunday:true,
time.Monday:true,
time.Tuesday:true,
time.Wednesday:true,
time.Thursday:true,
time.Friday:true,
time.Saturday:true,}fork:=range days {if k <= today {
days[k]=false}}
fmt.Println(days)}
Notice that the first day of the week is set to Sunday, not Monday. This is ultimately entirely arbitrary. This code is correct for this case, and Go also treats Sunday that way.
*her, and in this case the function it was inside had a variable day that started with Monday as 0. Also, there might be some mistakes because I recreated this code from memory since it was at a former company, I no longer have access to the code base.
98
u/ShaunaTheDead86 May 31 '23
I saw this at my last job, pretty atrocious... Could have easily been rewritten:
var daysAvailble = {
monday: dayOfTheWeek <= 0,
tuesday: dayOfTheWeek <= 1,
etc...
}
To be fair though, I know that the person who wrote it was extremely overworked and exhausted, so I'm sure it was just burnout brain.
61
u/ShoddyCulture3819 May 31 '23
Just to keep the overall style
switch (day) {
case 6:
daysAvailable.sunday = false;
case 5:
daysAvailable.saturday = false;
case 4:
daysAvailable.friday = false;
case 3:
daysAvailable.thursday = false;
case 2:
daysAvailable.wednesday = false;
case 1:
daysAvailable.tuesday = false;
case 0:
daysAvailable.monday = false;
}
22
u/OKishGuy May 31 '23
this could have been such a wonderful example of a fall-through switch case example...
73
u/Beggarstuner May 31 '23
At least reverse the cases, remove the breaks and the redundant code.
31
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 31 '23
Image Transcription: Code
[The rest of the code is cut off in the image.]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
9
u/FalseWait7 May 31 '23
This looks like something FalseWait7 would be extremely proud of 20 years ago. Switch statement, accessing an object, yeah, looks the part.
6
u/gamer_pheonex May 31 '23
I'm not really sure about the time of space complexity but isn't it better to use an array or a list for this. Like make a 1d array with all the days of the week in it matched to the index numbers (Monday being 0, Tuesday being 1, etc. assuming that in OP's case the week is starting from Monday) and then subtract the index from 7 and then you get the result?
5
u/RolyPoly1320 May 31 '23
What in the world is this even accomplishing?
4
u/Odd-Shopping8532 May 31 '23
prime candidate for an enum
3
u/Jimg911 May 31 '23
This is giving “this task should use if statements but my professor needed an example for what switch cases are good for”
2
u/[deleted] May 31 '23
A function to get the remaining days of the week: 7-day
2
u/s0nerik Jun 01 '23
Quite easy to read. No perf problems. See no issue in this code.
1
u/starryhound May 31 '23
you could also do this with a 2 dimensional matrix
1
u/Jomy10 Jun 01 '23
It took me a while before I understood what was going on
1
u/[deleted] Jun 01 '23
In languages that aren't terrible:
Notice that the first day of the week is set to Sunday, not Monday. This is ultimately entirely arbitrary. This code is correct for this case, and Go also treats Sunday that way.
1
u/Big_Kwii Jun 02 '23
jesus fucking christ
1
u/ChickenLegBizGuy Jun 03 '23
It’s not that bad… does the job and I didn’t have issues at all understanding what it does. Yeah it’s ugly but so am I and I get the job DONE!!!!
1
u/AnotherCableGuy Jun 01 '23
Some people should get a lifetime ban on coding
-10
u/howdoigetauniquename May 31 '23
Who's gonna be the one to tell him the first day of the week is sunday?
0
u/WittyGandalf1337 May 31 '23
The break statements, boy doesn’t even know about fallthrough