not sure why they have 24:00 as an option there...
This code takes way longer (like 4 orders of magnitude) than just just doing an array literal, but if you only need to do it once and you keep it....
idk, it's not the worst, it's just generally smart to have it be programmatic simply to avoid some fat finger mistake.
EDIT: since people in a fit, no the above probably would not be what I recommend for real use, but the idea of programming generative logic over hand coding. Quick rewrite with configurables:
The problem is that the customer now wanted a granularity of a single minute instead of half an hour. So I'd say that the issue is scalability maybe? It has 24:00 as a workaround to mean the whole day. So if you pick 00:00 to 24:00 it's the whole day. Idk I found this funny
Yeah that was the first thing I did to "fix" the issue, but of course the product didn't allow it since it was a drop down list and 1440 elements in a drop down is not that convenient. He had a laugh tho. I ended up changing lots of stuff and now it looks much nicer
good! yeah, if you are changing how this is represented in the UI to not be a list of options, I imagine you'd just delete this list entirely either way.
I dunno... The original makes sense, from a glance, exactly what its used for. This collapsed oneliner..... Is hard to read.
Reminds me of a few months ago, when some government had their source code release, and everyone lost their minds over 10 explicitly defined progress bars with various permutations of hollow and filled circles. Reddit was divided on trying to find the shortest possible way to generate it on the fly, vs appreciating how self describing it is. An array of progress meters? Must be for metering some progress. An array of HH:MM, in half hour chunks? Must be some kind of date or time selector. Easy to understand in less than a second
This, sometimes shorter or simplified is not better. If is for things like this i don't mind see an easy readable array instead of a short but less clear sentece.
The 24:00 option does make sense, but only in very select circumstances. For example, it makes to write 25:00, which might read less confusingly when writing opening hours such as 21:00-25:00.
This is more common in Japan than in Europe, but it makes it unambiguous "which" one o'clock is meant here.
it's just generally smart to have it be programmatic simply to avoid some fat finger mistake.
I think OP's code is much easier to understand. It declare's what it is rather than having to think through what this does.
There is no reason to worry about a fat fingered mistake in this case:
If the code you posted had a fat finger mistake, it's much harder to read than if OP's had a fat fingered mistake.
This isn't something that changes very often, if ever. That's lower risk for fat fingering than something that is getting updated frequently.
You don't have to type this out manually in the first place, just execute your command, and copy paste your result. done
If you fat finger your code, and you don't remember writing it, you at least have to re-logic your approach or replace it. no logic is needed to fix OP's fat finger
If the code you posted had a fat finger mistake, it's much harder to read than if OP's had a fat fingered mistake.
less likely to have a fat finger mistake, especially as requirements may change since it is much less likely to have a "incorrect but functional" output to begin with. If they suddenly want 10 minute intervals or 15, this takes minor change with a high certainty. The OP does not. It would take more effort with a higher chance of something being wrong but still functional.
You don't need to even read the code unless it has an error or needs to change. You read the variable it's assigned to.
I'm not saying this is always better. I think you're over overly reducing the benefit.
Any second year CS student with more than a few months of JS should be able to read this and adjust it to 15 or 10 minute intervals with no trouble. It's not difficult.
It’s not less likely to have a fat finger mistake.
you can generate the list one time with the same code you wrote, copy, paste and never touch again. Need to update it? Write a different function for the updated list, run it, copy, paste.
It’s not really a matter of whether it’s difficult, it’s worse in performance and readability.
Compilers will optimize OPs approach, and be able to optimize other constants that reference it as well.That’s lost when you use a function instead. There are more down sides to writing ‘programmatically’ than upsides in this case.
you’d an generate the list one time with the same code you wrote, copy, paste and never touch again. Need to update it? Write a different function for the updated list, run it, copy, paste.
but then WHY copy paste it? if the people looking at it are capable of quickly writing this algo, then they can also quickly understand it.
it’s worse in performance
This is quite marginal when it only needs to be run once. It's a constant. It's not needing to be run a million times a second. I'm sure you've heard "cpu time is cheap, developer time is expensive"
and readability
It's not hard to read (you don't need to read it anyway!!), and it's better than ending up with 100 lines of hard coded values. It's a fundamental difference in philosophy. Why hard code and risk human errors when you can generate it algorithmically? The only caveat would be that now we can use copilot to probably generate the hard coded list faster and easier than either human approach.
Compilers will optimize OPs approach
How?
And what about this approach isn't optimizable by a compiler?
lost when you use a function instead
Functions can also be optimized. JS is actually really good at doing repetitive loops over the same data.
but also, none of that really matters. You've talking about literal microseconds over years. Hard coding the OP will NEVER pay off in compute time over this (admittedly obtuse) implementation, and that's before the cost difference in computer over dev.
At the point you're concerned about, should make it JSON instead of a literal, since JSON parsing is faster than literal parsing.
but then WHY copy paste it? if the people looking at it are capable of quickly writing this algo, then they can also quickly understand it.
Because this is a case where i can declare the thing i want outright instead of describing how to create the thing I want. The former is clearer and more straightforward.
I'm sure you've heard "cpu time is cheap, developer time is expensive"
The programattic approach doesn't save much more developer time either. the only difference is a copy paste.
Functions can also be optimized. JS is actually really good at doing repetitive loops over the same data.
This is a good point, I was speaking more generically in terms of programming which would apply to other programming languages, and not necessarily scripting languages. In a compiled language OP's list is generated compile time and baked into the executable, and the programattic approach usually isn't. Not only does this mean that this variable would be more optomised, but objects composed using this variable would be compile time as well. If I had a compile-time varible and it needed this list, now I'd have to make it a runtime one because we decided to call this function instead.
At the point you're concerned about, should make it JSON instead of a literal, since JSON parsing is faster than literal parsing.
I honestly wasn't thinking about javascript specifically when i wrote this, so this is valid. this is true for scripting languages. but it most programming languages, JSON parsing is much slower than declaring this list as a variable.
I'm not saying your apprach is bad at all, however replacing OP's code with this solves no real problems. There's really nothing wrong with having a constant with 100's or even 1000's of lines. And fat fingering really isn't an issue either. replacing this with code that returns the same doesn't solve anything. You're fixing probelms that aren't there.
Because this is a case where i can declare the thing i want outright instead of describing how to create the thing I want. The former is clearer and more straightforward.
We both know that's not true.
The spec for this feature won't say "We need a dropdown that has 00:00, 00:30, 01:00...." It will say "we want a list of time codes in 30 minute increments".
The programatic approach doesn't save much more developer time either. the only difference is a copy paste.
Except updating is faster. Since you don't need to RECREATE the method every time.
however replacing OP's code with this solves no real problems.
It solves the real problem of adapting to changes in spec simply.
And fat fingering really isn't an issue either. replacing this with code that returns the same doesn't solve anything.
Wrong. Making changes is simpler and more consistent. No risk of silent typos.
I don't understand what you mean, OP's code literraly just declares what it is outright, your code describes steps for how to generate the thing you want. curious what your argument is here.
The spec for this feature won't say "We need a dropdown that has 00:00, 00:30, 01:00...." It will say "we want a list of time codes in 30 minute increments".
That's fine, whether this code uses a list of strings, or something else as a way to represent the data isn't really a factor deciding which apprach to take. Doing it programmatically to generate the same list doesn't change this.
Except updating is faster. Since you don't need to RECREATE the method every time.
Why would you need to recreate the method? either you generated this 1 time, or if you had to update it to changing specs, you'd likely have to rewrite or update your function anyways. Also, I'm not opposed to have comments documenting this as a way of generating this list.
It solves the real problem of adapting to changes in spec simply.
Both approaches are practically equal in this reguard. the developer is writing the same function reguardless. but OP is running it upfront and your approach wants to do it at runtime.
Wrong. Making changes is simpler and more consistent. No risk of silent typos.
Silent typos when copy pasting? nobody should be updating anything in this list manually anyways. Nobody is typing this out, or manually editing this list, so why are you worried about typos somewhere nobody would type?
The only valid argument for rewriting this your way i can think of is if you want to save a tiiiny bit on the size of the javascript coming over the wire, but that's almost negligable in this case
All the time we describe a declarative idea of what we want, not explicitly the exact result. Because that is often the clearer way to actually describe things to humans.
Why would you need to recreate the method? either you generated this 1 time, or if you had to update it to changing specs, you'd likely have to rewrite or update your function anyways
Yes, update, not rewrite. We need 10 minute increments? easy change. In your world, the person is either now hand writing it, or remaking a generator function anyway.
Silent typos when copy pasting?
We've already established that the copy pasting is a complete nonsense position to have. because why write code to get the result and not just USE the code that gets the result?
the silent typos are when in there's typos that don't cause errors, like when hand writing the strings.
The copy-paste idea is absolutely stupid, stop mentioning that. Literally insane to recommend that as an idea. "Yes, lets remake the generator every time, what a fantastic idea."
The only valid argument for rewriting this your way i can think of
We already addressed that adjusting for spec changes is far easier. This is already a factual benefit. Change like 3 numbers and now you have 10 minute increments, or 15 with 4 small changes.
I think you must be misunderstanding the very minor differences in the actual workflow between these 2 approaches. They are nearly identical, but OP’s approach is memorialized.
There are many ways in which your code is less clear than OPs.
First of all, someone who barely knows programming can understand the list in OPs code. This is not the case for your generator.
You cannot tell immediately whether the list contains 24:00 as an option. This is a thing that stood out to many people in this chat. This detail isn’t immediately apparent in your code and may have been missed by some people. You have to calculate it in your head. That is objectively less clear than just declaring it.
It’s a constant, there are no parameters, just let it be a static constant
Your approach is an instruction for how to build a constant. OPs just declares the constant. Declaring is much simpler.
What your saying is like saying: instructions for building a chair is more straightforward than just having the chair. It’s objectively not.
To indicate that this approach is ‘insane’, hints that you just haven’t seen code like this before. It’s quite normal, it’s even essential in some contexts.
Generally software solutions aren’t designed around how fat-finger-able the code is. That’s not a problem I ever really encounter myself
Also, implying that your approach is better for changin spec implies you know what the changing spec will be. Start adding irregularities to the data sett: (ex: end time to to go 26:40 instead of 24:00) and it becomes less easy to change than the simple declaration.
Beautiful? In a shortest code contest maybe. But this is both far less readable, and far less efficient. It’s shorter in length but still manages to take much longer to read than the OP. What’s the point?
The point is I think that it's beautiful, that generating an array like that in 1 line is even possible. I made no statement regarding performance or readability. Of course stuff like this has no place in a serious prod. Relax.
Properly named it shouldn't be an issue. You don't necessarily need to read it if you understand what it's supposed to do.
The efficiency point is very true, but assuming you aren't trying to run this more than once (why would you run it more than once?) that hardly matters. It's more easily modified if the requirements change, and doesn't require any human to be sure the numbers are all right.
We use hour endings in energy trading so a day has HE 1 thru HE 24. And then some systems use 25 or 2s to represent the repeating HE 2 during fall DST.
Given the task I had at work the thing that is shown here was very much a problem, but this is not the place to discuss work, this is just a snippet that I thought was funny
Haha, the sentence to me indicated all of the words were in alphabetical order which broke down with “and”. But yeah I guess I don’t know how you’d describe the actual definition differently. Oh well!
Customer wanted a minute resolution, which means having an array with roughly 1440 elements all typed out like this. Also, the time picker was a drop down menu and you can imagine how impractical a drop down with >1400 elements is, plus the user didn't have the option to type the time manually. Not to mention the fact that they included 24:00 which brought some problems too
You don’t need to type all 1440 elements. Use a JavaScript command that generates these numbers and copy paste the result as a constant.
The rest seem like display related issues and problems in your logic. Neither of those would be fixed by replacing this with something else.
You likely need a ui that works better for minutes and logic that handles 24:00, but that’s not this variable’s fault
If you still want to represent the ui with a list, then declaring a long list is fine. If you want something like a slider, or a clock, you could probably scrap this list.
As I said, having a drop down with 1440 items is not practical at all, so I had to get rid of this list anyways as I had to find another way to do the time picker. There's really no need to look so deep into it
Edit: Also, why use a js function that generates the numbers if I could just make the list programmatically at run time? I mean I already wrote the command so I might use that inside my code while I'm at it
Hard coding things is fine. which is all this is. It’s not the most useful way to define time and 24:00 seems redundant without context. But on its own, this isn’t any kind of problem. Depends on what it’s intended to be used for, and is easily mapped & replaced if you need to.
118
u/thekwoka Mar 29 '23 edited Apr 01 '23
not sure why they have 24:00 as an option there...
This code takes way longer (like 4 orders of magnitude) than just just doing an array literal, but if you only need to do it once and you keep it....
idk, it's not the worst, it's just generally smart to have it be programmatic simply to avoid some fat finger mistake.
EDIT: since people in a fit, no the above probably would not be what I recommend for real use, but the idea of programming generative logic over hand coding. Quick rewrite with configurables:
46
u/LasagneAlForno Mar 29 '23
This isn‘t bad code per se. It depends how and where it‘s used.
Go on and use some fancy formula or library. It probably has worse runtime, worse readability and a much higher potential for errors.
26
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 29 '23
Image Transcription: Code
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!
10
u/Kfimenepah Mar 29 '23
And the var name is in italian 😱
9
u/private_static_int Mar 29 '23
00:00 and 24:00 are both there XDDDDD
1
u/M1dgetSlayer Mar 30 '23
Judging from the variable name, an albanian probably wrote this. I'm albanian and I am dissapointed :(
1
u/NickSicilianu Mar 30 '23
Someone did not think about scalability. Good luck changing that, if you want a minutes resolution.
2
u/KYIUM Mar 29 '23
24:00 and 00:00?
2
u/[deleted] Mar 30 '23
Lol
1
u/Michelle-Obamas-Arms Mar 30 '23 edited Mar 30 '23
Hard coding things is fine. which is all this is. It’s not the most useful way to define time and 24:00 seems redundant without context. But on its own, this isn’t any kind of problem. Depends on what it’s intended to be used for, and is easily mapped & replaced if you need to.
1
u/illyay Apr 01 '23
At least it did t have every time permutation ever.
8
u/[deleted] Mar 29 '23 edited Mar 29 '23
[removed] — view removed comment
7
u/[deleted] Mar 29 '23
[removed] — view removed comment