Incremental game. It's called "SpeckCoin Incremental". TomSka will not be happy about it. It has a subreddit, but there's like no one there. There's a Discord server for it, would you want to join?
I would advocate against this in most cases because it takes more thought when reading it. The simplest code to read is what I generally best; more programmers on your team will understand it more quickly, there's less confusion, you can read through code more quickly, it's prone to fewer errors, fewer bugs, etc. Start simple with your code and only optimize once you find performance issues, which a boolean assignment will almost never cause.
Simple code is still useful when you're the only programmer, because you may need to go back and look at code you haven't see in a year and you'll want to understand what you did.
I would advocate against this as it's not typesafe. javascript uses type coercion and converts variable "a" from a boolean to a number. This wouldn't compile in typescript if the type of variable "a" is a boolean.
It reads as "a xor-equals true". So if a is true, we have true xor true which is false. If a is false, we have false xor true which is true. So no matter the value of a, it is swapped
Not sure how to link to a specific place in a wiki article from mobile but check out a bitwise XOR operation.
And as prolific a commenter I am in code, I respectfully disagree with u/bphase. Once you've seen this once, you know what it does and its quite clear without a comment explanation.
Can you show an example of what you mean with pattern matching enums and constants? I know pattern matching with 'is' in C#, but Im not sure what you mean.
Actually I've needed to use one once. We had a database query that retuned a boolean and i didn't want to create an enum for that. Before the query was executed the first time the value had to be undefined so i set it to null.
Why would anyone ever use nullable booleans? Why do languages with built in nullable types even support them? A nullable boolean seems like a less intuitive solution than an enum or even a meaningful integer. Logically, they don't even represent a binary truth value anymore. And if you really want some sort of ternary logic: Again, integers. To be honest: What is nullability even used for in practice? Seems like syntactical sugar that rarely increases readability and hardly saves keystrokes.
Some people advocate for using == true to make it more obvious that a boolean comparison is going on, or because it's easier for them to read. I've also seen true == used in C++ before because using == true you can typo to = true and accidentally assign to something in memory and then it will evaluate the contents as true, a silent failure (newer compilers likely warn about this).
Personally I find it easy enough to read without the == true, and also use ! instead of == false.
The argument that advocates of this style would give is that it shows the reader that a boolean comparison is being done in an obvious way. You can omit == true and get the same result, it's just about code style.
Some people prefer seeing == true rather than a !. In some languages like C/C++ the syntax allows for an integer to be compared in the same way in a conditional, but using == true/false will cause a warning. I personally don’t use the extra syntax, but I understand why some people prefer it.
This relies on muted being true or false in the beginning. That seems to be a given in this case but I feel like people are hating on the example code too much for having an else block which makes sure that true is set as default.
No it doesn't. Javascript has the concept of truthy and falsy. Regardless of what muted is initially assigned to the runtime will coerce a boolean value from it.
is still the superior toggle format though. It takes care of any unexpected values of x.
If you just do x = !x, x being null will make it true, x being a string will make it false. Is that good? No, it is unintended behavior. Should such cases even be considered? Idk. If x is the property of an html element, every injected code and web extension can manipulate that. There might be other reasons why the type of x is not implied. If you don't want to worry about that, use the upper toggle format.
I don't understand what you mean by default. You would set the initial value in the html or on creation of the element. This is a toggle method. This method wouldn't be responsible for initializing the value.
getElementById either returns an element from the dom or null. If soundtrack is null in OP's example it will actually throw an exception because type null can't have a muted attribute.
If OP grabs an element that isn't a media element. muted will likely return undefined and it wouldn't make sense to add a muted attribute to a non-media element.
If you want to write guarded code I would check if getElementById returned a valid media element.
null is falsy, so if soundtrack.muted is null(which it isn't) it will get set to true in the expression above. You're not doing anything different by explicitly checking for null then setting it to true.
You could approach it as ‘a = !a’. In this case the line would be really long. You could either do ‘document.getElementById(“soundtrack”).muted = !document.getElementById(“soundtrack”).muted’ But that also makes the assumption that soundtrack exists otherwise you would throw an error of trying to call “muted” on a null value
So I would likely do it as: const soundtrack = document.getElementById(“soundtrack”) if(soundtrack) { soundtrack.muted = !soundtrack.muted }
pretty sure that "if(soundtrack)" makes it impossible to turn the soundtrack on again. Probably need explicit checking for "!= null" or "!= undefined". Don't remember JS well enough to be sure which one
Non-javascript developer here, and curious - what would you ever want besidess vanilla JS to solve this? You specified vanilla so I'm assuming you wanted to import something
If you would like to see the code in action, PM me and I'll give you the link. Not gonna put it here because I'm not trying to advertise it (it's a game). If lots of people insist, though, I will put it here.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/J-o-N_S-n-o-W, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
96
u/Flyspeck101 Now where's that function declaration Jul 30 '21
I don't think I'm gonna change it... after all, who is going to look into the source code and hunt for things like this?
120
u/PersonKiller1234 Jul 30 '21
Not sure if this was just kinda left there but for toggling bools in general, you can just use something like a = !a where a is a bool.
296
u/[deleted] Jul 30 '21
„== true“ always a good idea
2
u/MalbaCato Jul 31 '21
static is a valid keyword in js? tell me what now?
28
u/TryNotToShootYoself Jul 30 '21
Just curious, how else would you do this in vanilla JS? Aside from the redundant
== true5
u/GoldenPalazzo Jul 30 '21
I legit did this shit a year ago on a game wrote in Unity wtf
3
u/[deleted] Jul 30 '21
Trust me, even senior devs do this. But the reviewers lets it fly as long is it works…
9
u/Toast-Goat Jul 30 '21 edited Jul 30 '21
Image Transcription: JavaScript
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
2
u/[deleted] Jul 30 '21
Classic
2
u/MrDiamondDog Jul 30 '21
I used to do all of this including == true until i figured out the ! operator
2
u/[deleted] Jul 30 '21
Now i would say this is bad if i wasn't doing the same. Though he can just use toggle
2
u/BasePlate12 Jul 31 '21
muteToggle(){ toggle.muted = !toggle.muted}
2
u/tekion23 Aug 02 '21
My brain failed
2
u/RagVerse Aug 05 '21
So how do you fix this?
P.S. Asking for a friend ;)
2
u/[deleted] Jul 30 '21
Don't know where to start lol
1
u/[deleted] Jul 30 '21
so if its muted its not, otherwise it is?
1
u/noxtify Jul 30 '21
If (!x.muted)
1
u/BlackBearFTW Jul 30 '21
Even the method name sounds like you are trying to mute a toggle, made more sense if it was called toggleMute
-1
u/Jojojordanlusch Jul 30 '21
based
-9
u/Flyspeck101 Now where's that function declaration Jul 30 '21
If you would like to see the code in action, PM me and I'll give you the link. Not gonna put it here because I'm not trying to advertise it (it's a game). If lots of people insist, though, I will put it here.
-2
u/Not_Sugden Jul 30 '21
wait until they learn about variables
-3
u/EddieFromIndia Jul 30 '21 edited Jul 30 '21
document.getElementById("soundtrack").muted = !document.getElementById("soundtrack").muted-5
u/SheepLinux Jul 30 '21
let document.getElementById("soundtrack").muted = !document.getElementById("soundtrack").muted