« first day (4591 days earlier)      last day (360 days later) » 

2:07 AM
No question. I just need a void to yell into:
For so many years, I've been writing "document.getElementByID('foo').bar" but today, I wrote "foo.bar" and it worked exactly the same way. So why the hell have I been writing "document.getElementByID('foo').bar" all these years!!??
Okay, maybe that was a question. Any answers?
 
2:33 AM
and the void yelled back: probably because being explicit about your intent instead of relying on the magic of globals is a good way to avoid hard to track bugs in your app.
 
Thanks for the response
Sorry, could you dumb that one down for me a little? Maybe with an example?
Do you mean to say that it's hard to tell from "foo.bar" that "foo" is the id of a html element?
I'm happy to pay that price for shorter code, especially since a keyword search of "foo" will instantly tell me what it is, and is a task I was likely to perform even in the "document.getElementByID('foo').bar" case anyway.
 
@JonathonPhilipChambers what I meant is that this behavior relies on the standard defining that creating a DOM node with an id implicitly creates a property on the global object of the same name as the id + that properties of the global object are also accessible as variables of the same name, which is highly obscure and hard to track. Some common downsides to relying on that magic:
- you might get a local variable of the same name - it will shadow the global one;
- you might want to change the id of the element. As soon as you do, you have to refactor everything that uses the implicit global variable instead of updating just one line;
 
Thanks. Let me try to paraphrase you to check my understanding:
 
Finally, shorter code shouldn't be the goal - no one's going to appreciate that in the end (especially the future you :)). The more years pass by, the less I become concerned with being "clever" or "succinct" as opposed to clear, readable, and maintainable.
 
@JonathonPhilipChambers Please don't post unformatted code - use the up arrow to edit your post, then hit Ctrl + K to format the code in that post. See the faq. You have 25 seconds to edit and format your message properly before it will be removed. Please separate code blocks from your actual question. Put your question in 1 message and then your code in a 2nd and format it.
1 message moved to Trash can
@JonathonPhilipChambers Please don't post unformatted code - use the up arrow to edit your post, then hit Ctrl + K to format the code in that post. See the faq. You have 25 seconds to edit and format your message properly before it will be removed. Please separate code blocks from your actual question. Put your question in 1 message and then your code in a 2nd and format it.
 
2:42 AM
re: example - yup, try for yourself and see :)
 
1 message moved to Trash can
 
Sorry, trying to figure out how to format code so it doesn't get trashed, but you saw it, right?
 
@JonathonPhilipChambers James will keep moving that message unless you format the code as code. The message from the bot contains the exact steps you need to do to fix that.
 
So, the main concern is a namespace collision, which resolves in favour of declared variables and overwrites element ids?
 
In any case, yes, I've seen the message, and already replied above.
 
2:44 AM
I must have missed a step. No point sending it again if you already get it
 
yup, just noting
@JonathonPhilipChambers one of them, but yeah. Not exactly "overriding", there is a term called "shadowing". Local variables shadow those declared in one of the upstream scopes, yes.
 
Thanks for taking the time to walk me through this. I think I'm understanding
You mentioned about changing the id of an element, but suppose I'm only using document.getElementById("Foo") and not document.getElementById(cleverlyComputedValue), wouldn't that benefit be lost anyway?
It's a good argument for calling ids by variable
But if I'm calling it by explicit string, isn't it the same? (assuming I control for shaddowing)?
 
@JonathonPhilipChambers no, I meant during refactoring. Suddenly, you have to rename every reference to the global variable instead of, you know, making one small change on one line. And if you've ever worked on a relatively big project, you know that the fewer changes are in the diff, the better for everyone.
 
Okay, I'll have one more stab it it. So if I use wellNamedVariable = document.getElementId("Foo") once, and "wellNamedVariable" a million times, I won't have to change wellNamedVariable. But if I just let Foo be the variable, now I have to change Foo a million times. Was that a fair paraphrase of what you just said?
 
2:54 AM
because right now, I'm using document.getElementById("Foo") AS the variable, which probably isn't recommended, but it's in my muscle memory until I change it.
so changing that to Foo.bar may not be ideal, but at least a step up in the correct direction
 
@JonathonPhilipChambers it's absolutely O.K. - I see nothing wrong with your muscle memory :)
 
Thanks again for your time. I was going to post this as a question, but it probably would have gotten downvoted to hell since it's a stylistic question in a technical database.
 
if anything, introducing more magic is a step in the wrong direction
@JonathonPhilipChambers More like because it's likely have been asked multiple times before. It would not be an opinion-based question (although it would probably attract some close votes), it can be answered factually.
 
I'm going to give you one more chance to talk me out of it, if you'd like to take that opportunity. But I do respect it's not your job or responsibility to protect me from my own stupidity. I may just have to learn this one the hard and painful way.
 
here you go, btw, with a couple more reasons to use getElementById always:
439
Q: Do DOM tree elements with IDs become global properties?

KooiIncWorking on an idea for a simple HTMLElement wrapper I stumbled upon the following for Internet Explorer and Chrome: For a given HTMLElement with an id in the DOM tree, it is possible to retrieve the <div> using its ID as a variable name or as a property of window. So for a <div> like <div id="exa...

 
2:59 AM
Using global variables instead of selecting the element with JS is pretty bad, I get horrible feeling every time someone does that. I have done no research, but I am pretty sure you will get plenty of bugs sometimes and it will be totally unresponsive. Just don’t do it. It’s worth the extra 3 seconds.
 
Thank you. I promise to at the very least read every word of the question and every word of the accepted answer
 
@JonathonPhilipChambers eh, it's whatever, to be honest: if it ends up working for you, go for it. However, it's important to know the shortcomings of every approach. You aren't necessarily shooting yourself in the foot if you go that route, but I'd advise against it.
 
I really hate the idea of using global variables as element references too. A few months ago, before I knew that worked, I had a variable “ui” and an element “#ui”. For about 4 days at least, I was like “what the heck is going on????” Why it kept returning an element when I referenced “ui”. Nowadays, I almost never use ids, and absolutely never use global variables as element references.
 
@ParkingMaster although you're probably right, I'm not working on a coding team or for a client. I'll actually be here to pick up the pieces when this thing breaks, and I'm actually hoping it does break, because it will force me to become a better developer to put it back together. But if I was working for a client or on a team, I'd follow a style guide to the letter without understanding why it works. Less risk, but also less opportunity for me to learn and grow.
 
@ParkingMaster heh, true, exactly what I've been talking about :) Here's a real-world example of such a bug happening, @JonathonPhilipChambers
 
3:03 AM
@JonathonPhilipChambers if it’s ok with you, then I’m fine with it. It’s my own personal opinion though
 
Both of you, I don't want to downplay the value of these stories and insights. You've given me a lot to think about
I'm at the early stage of my career when I'm still trying to figure out what my coding style even is.
 
@JonathonPhilipChambers lol, I don’t think those jobs let you choose your style, lol they’ll probably force you to use their coding style
 
@JonathonPhilipChambers I'd actually advise choosing a coding style from one of the well-known companies and sticking to it, it'll only serve you good. My personal favorite is Google's: google.github.io/styleguide
 
I've actually got a 50% stake in woolchambers.com.au Don't judge me too harshly for that website. It has a lot of known issues from when I was first learning to code, and I'll give it all a good refactor one day.
 
@JonathonPhilipChambers it’s actually not as bad as some websites I’ve seen
You should’ve seen my first website
 
3:09 AM
While I'm still solo, I do want to do a bit more "learn the rules so you better know how to break them" stuff, but one day I'll settle down, find an employer or some clients, and code much cleaner than I do now. Now, I'm young, free, and experimenting! I never got into drugs or alcohol, so instead I'll be self destructive by using too many if statements and letting my code get too far tabbed from the left. ;)
But, I gotta learn the rules before I break them, so what's the best style guide for php/html?
 
@JonathonPhilipChambers I’m still fairly new as it is. Ahh, the nostalgic days of coding random projects every day
 
(I will keep that link for javascript though)
spending an hour researching the differences between i++ and ++i
you can't do that when you're on the clock for a boss
 
@JonathonPhilipChambers I’ve never seen that before, what is the difference?
 
@JonathonPhilipChambers "code much cleaner than I do now" - well, you are up for a disappointment, my friend.
 
@OlegValteriswithUkraine 😂
 
3:13 AM
@ParkingMaster postfix and prefix increment
 
i++ returns the value of i before it was iterated. ++i returns the value of i after it was iterated. But there are also performance differences based on which compiler you use
 
@JonathonPhilipChambers oh, ok. I see it now. I have never used or seen that before. You learn something new everyday :)
 
I even learned that most compilers will automatically change you from the worse one to the better one if it knows it won't otherwise make a difference
because if nothing is collecting the returned value, they're functionally identical
 
Yeah, I never got a use case for that. I never return the value of i++, it really just has one job: add +1 to i.
 
but I can spend hours learning minor performance differences between two seemingly identical options, because I report to no one.
Sometimes you need i++; foo = i; but can instead do foo = ++i
one line instead of two
do that in a compiled language though, the compiler will correct you anyway. But I think you'll get speed gains in javascript or python
 
3:17 AM
@JonathonPhilipChambers thanks for the advice, it’s definitely going to help with clean and short code in the future.
And I thought I knew everything about Javascript. I think I do now though.
 
pfff. That's a bold statement.
 
I used to hate single use functions, but I'm starting to like them now. Especially if they're in a loop. My loops used to be so big that the top and bottom wouldn't fit on the screen at the same time. So if I was reading the middle of it, I'd forget I was in a loop, or where it looped to.
 
@JonathonPhilipChambers just as an FYI, ++ and -- are considered bad practice pretty much universally.
 
@OlegValteriswithUkraine not really, I only use it in for loops and it works fine. No support for other browsers, simple, short… what could be bad about it? Besides, for (i = 0; i < x; i += 1) just looks stupid
 
@ParkingMaster wasn't talking about loops, although I don't see anything "stupid" about the notation.
 
3:24 AM
I can see why you should avoid iterating if you can help it, but when code must iterate something, how would you do it without i++?
 
@OlegValteriswithUkraine I mean, it works, but just looks strange, I’ve never seen someone do that before
 
Lets suppose I've got a picture of a cow, and a counter that displays the number of times the cow has been clicked. How would you code that feature without using ++?
 
@JonathonPhilipChambers “i = i + 1;” “i += 1” “i -= -1”
 
@ParkingMaster well, you haven't seen a lot of code then :) stackoverflow.com/…
 
I know you can use i = i + 1; but that's not an improvement on i++; is it?
 
3:28 AM
@JonathonPhilipChambers the shorhand is i += 1
 
@JonathonPhilipChambers lol, what do you suppose? That Javascript reads my mind when I tell it to increment “i”? :)
 
I'd definitely use i++ if I wanted to count the number of times something is clicked
Couldn't imagine doing it any other way
 
@JonathonPhilipChambers me too. There is no other way, I don’t see a problem with it
 
i += 1 feels wrong. i += 2 is fine, but it feels wrong with 1
if it's 1, it should be i++
 
@JonathonPhilipChambers how about “i -= -1”?
 
3:30 AM
okay, let's stop trolling and find out the correct answer to this one. I want to learn how to avoid the dreaded i++ without writing ugly code
 
@ParkingMaster please, use backticks for code formatting
 
You have me intrigued. I must learn the answer
 
@OlegValteriswithUkraine I do if I’m on my laptop, but on my iPad it’s annoying to have to press to “123” then hold down the quote, select the backtick and then repeat.
Now you know what device I’m using and when, lol
 
Are you genuinely saying that i += 1 is better than i++? Really? Or am I misunderstanding?
 
@JonathonPhilipChambers not really, just that it's clearer about the intent.
 
3:33 AM
Well, I remain unconverted away from my sinful ways.
Anyway, I've got a feature to code, but it has been great chatting.
 
@JonathonPhilipChambers Oleg is going to try and improve every little piece of code, but hear me out just use whatever works. That’s my best advice, and yes “i++” is bad/good, that’s what it comes out to. If you have any more questions, feel free to ask, but for now I’m going to sleep, because it’s literally 11:30PM lol. :)
 
Off to practise some dark arts global magic, and suffer the consequences of my reckless behaviour.
1pm here. Good night, and thanks for the chat
 
@JonathonPhilipChambers no problem. Have a good “day” (lol feels weird saying that)
 
@ParkingMaster heh, 6:40 AM where I am at, and still working and chatting in the meantime :)
 
 
10 hours later…
2:00 PM
@OlegValteriswithUkraine just a coincidence, I found code from a few weeks ago that I wrote that says details.points += 1
instead of details.points++
 
@Wietlol Solution:
async function main() {
  let isComplete = false;
  let counter = 0;

  async function job() {
    while (!isComplete) {
      console.log(++counter);

      // Promise.resolve resolves in the next microtask, but setTimeout is a macrotask, so as long as a Promise.resolve is pending the timeout will never fire.
      // await Promise.resolve();

      // This creates a Promise that doesn't resolve until the timeout macrotask runs.
      await new Promise(resolve => setTimeout(resolve));
    }
  }
 
2:34 PM
 
> await new Promise(resolve => setTimeout(resolve));
looks like you forgot the duration for that timeout
but it still depends on what you want to do
while(!isComplete) is called a busy wait, which is generally bad
 
@Wietlol I do that lot too, lol. Forgetting the timeout and I'm like "Why is this happening so fast? Wait..."
 
 
1 hour later…
3:45 PM
@Wietlol In this code it's OK, since it's not a true busy wait. The await will yield to the execution and not hold up the thread. And the setTimeout also means that it would schedule a macrotask after which it would run again, thus allowing for stuff like UI updates in the browser (which are macrotasks themselves).
With that said, it's not a good solution to the original problem. Because, as stated, it was the maximum amount of value increases that could be done. This only increments once each loop, then yields, then would run at least 4ms later (minimum delay for setTimeout). It could instead increment 10 or 100 times each iteration, thus drastically increasing the maximum value.
Also, it doesn't need to do this async code and yield to the execution in the first place.
let start = Date.now();
while (Date.now() - start <= 1000){}
would give you a busy loop for a second. It can still just increment multiple times in the loop, too.
Overall, the whole "requirement" is nonsense. And basically any code thrown at it is also essentially nonsense. Because all of it would be chasing a non-existing actual goal. What is "maximum"? Is counter = Number.MAX_SAFE_INTEGER not the best thing without any loops?
 
 
1 hour later…
5:00 PM
posted on May 12, 2023 by Giuliana Pritchard

LTS-108 is being updated in the LTS channel to 108.0.5359.231 (Platform Version: 15183.94.0) for most ChromeOS devices. Want to know more about Long Term Support? Click here. This update contains multiple Security fixes, including: 1424337 High  CVE-2023-2135 Use after free in DevTools 1429201 High CVE-2023-2134 Out of bounds me

 

« first day (4591 days earlier)      last day (360 days later) »