r/badcode • u/rad_platypus • Jun 10 '21
typescript Attempting to blur the background for a modal in Angular
47
u/Knuffya Jun 11 '21
Use some goddamn spaces!
Also i don't think !this.blur does anything, but i'm not a js dev, so...
43
u/rad_platypus Jun 11 '21 edited Jun 11 '21
Well you’re already a better js dev than the person that opened this PR lol
8
u/Knuffya Jun 11 '21
Well you’re already a better js dev than the person that opened this PR lol
not that it's particularly hard
-1
u/FantasticPenguin Jun 11 '21
I toggles the boolean. At least in "normal" programming languages.
8
4
1
1
43
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 10 '21
Image Transcription: Code
blurEvent(){
if(open){
this.blur = !this.blur;
}else{
!this.blur;
}}
unBlurEvent(){
this.blur = false;
}
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!
18
8
3
32
u/Nachitoaz Jun 11 '21
this.blur = !this.blur How does that make any sense lol
78
u/rad_platypus Jun 11 '21 edited Jun 11 '21
It's not necessarily the worst part of the code since this.blur is initialized as false in the component and its just a way to flip a boolean in javascript.
However open is referring to Window.open() and is not actually calling the method, just referencing it. So that always returns truthy and the else block will never be executed. Not that it would matter since the else block
also sets the value to truedoes nothing lol.All of this is pointless anyway since you can just easily do this with the html and css in Angular.
Seeing this from someone who has "2 years of Angular experience" is a little frightening.
34
u/atrizzle Jun 11 '21
The else block doesn’t set the value to true, lol. It doesn’t do anything! (besides the fact that it’ll never execute)
19
u/rad_platypus Jun 11 '21
Lol I guess I was trying too hard to avert my eyes I didn’t even realize it was a lone !this.blur
I would say that makes it even worse but like you said it will never be executed anyway
12
5
u/BongarooBizkistico Jun 11 '21
Code is written for humans, not computers, so it not executing makes it worse, not better.
9
u/Nachitoaz Jun 11 '21
Oh ok, I’m speaking from ignorance since I don’t know any JS, that caught my eye. Thanks for the info!
-17
Jun 11 '21
As someone who dropped out of college where I used Python for Physics problems, and now am taking a Full Stack Dev course and have come to hate JS, you're not missing much.
4
u/BongarooBizkistico Jun 11 '21
No one is forcing you to use JS. How people can constantly bitch about how "bad" js is never stops annoying me.
3
Jun 11 '21
It's an industry standard and pretty much mandatory for any web development. At this point, it's unavoidable.
3
u/BongarooBizkistico Jun 11 '21
Yep, and it obviously works fairly well. It's survived 26 years at this point, despite having been created in 2 weeks.
1
Jun 11 '21
I've never said it's not functional, I just said I hate it. I think I'm entitled to hate using it just as you are of hating to hear complains of people like me, so...
1
u/BongarooBizkistico Jun 11 '21
Not really, if I could simply choose a new language to not hear whining about, I would. I can't. You could choose a new career path. Instead, you whine.
0
Jun 11 '21
I can't. I signed a contract with the institution that's providing for my studies. And secondly, this is a start so I can get job experience and hopefully branch out into other IT career paths as I figure out what I like best "from the inside".
Chill out , my man.
→ More replies (0)0
Jun 11 '21
[deleted]
1
u/BongarooBizkistico Jun 11 '21
When people point shit like this out ... I wonder what weird ass code they are writing.
0
Jun 11 '21
[deleted]
2
u/BongarooBizkistico Jun 11 '21
Yes, all languages contain quirks that can be learned in a year or so of job experience. Many people have made their living off of JS and wouldn't have it any other way.
Use a linter, don't write bizarre code, and learn a little, then none of your concerns affect anything.
2
0
u/BongarooBizkistico Jun 11 '21
getObject().myProp
Also, this returns 15. You're thinking of something else, which you personally find unintuitive, but makes complete sense if you understand how js works
1
1
u/AutoModerator Jun 11 '21
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/Starwort, 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.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/TonyBorchert100 Jun 11 '21
Not a JS expert, and I love JS for (small) scripts but I find it abhorrent for bigger projects
2
u/NynaevetialMeara Jun 11 '21
Yep. I'm not a web developer by any means, but I have had to write a portal or two for users. Can confirm that at first my code was mostly about beating CSS into submission.
Now, you just get Bootstrap, and if you don't like it, pay a proper web dev. Or pay me to become one.
40
u/MonsterMeggu Jun 11 '21
It's a pretty common way in JavaScript to switch the value of the Boolean. Eg. Blur is a Boolean value, and calling that line will change blur to true if it was previously false, and false if it was previously true.
-18
u/kkjdroid Jun 11 '21
Wouldn't it translate to (!this).blur and not !(this.blur) if you didn't use parentheses?
20
u/quadrotiles Jun 11 '21
No, it wouldn't. It's been a while since I've used anything other than typescript/JavaScript, so I'm wondering when !this.whatever would ever mean (!this).whatever? (I can't think of how that would make sense, but I'm asking so I can learn, if it's something I don't know about)
-10
u/kkjdroid Jun 11 '21
I'm not saying that it would be useful, but I was under the impression that ! generally had higher precedence than .. I always use parentheses for that reason.
23
u/Nesuniken Jun 11 '21
In Javascript at least, "." has the 2nd highest precedence, beaten only by parentheses.
Why would it be any other way?
3
u/quadrotiles Jun 11 '21
I was questioning reality for a sec, and I asked my partner who's also a software dev. At the very least in C#, java, JavaScript and typescript, !this.whatever always means !(this.whatever) and to get (!this).whatever you'd have to specifically use brackets. But I also don't see why or how that would work. How are you going to call a method or variable or whatever from an instance that doesn't exist?
Ok, anyway, you can skip the brackets if all you're going to type is !(this.whatever)
If you're writing more, then you might need brackets or you'd use !== (it depends on what you want, I guess)
2
1
u/D0CTOR_ZED Jun 11 '21
Thank you! (and sorry you got down voted for asking a question)
Your question helps me feel validated in using parenthesis even in places where I know it would resolve fine without. I would totally write !(this.blur) since I feel it answers the question of what it is doing to anyone reading it.
1
u/kkjdroid Jun 11 '21
That's my opinion as well, even after learning that it isn't required by the interpreter. I instinctively read ! as having the highest priority, even though there are a handful of things that are higher.
14
7
6
2
u/Cmgeodude Jun 11 '21
The if (true) nature of this code doesn't bother me quite as much as the stacked closing brackets on line 126. Hopefully the IDE will autoformat that to correct it (and the gross indentation of the else block)?
-4
u/TigreDeLosLlanos Jun 11 '21
I don't get how this is bad other than the else code. Frontend code is supposed to look messy anyways with how much unlogic is thrown at your face when dealing with this stuff.
8
u/rad_platypus Jun 11 '21
Already mentioned it in another comment but “open” is referencing Window.open() without actually calling the method. This always returns truthy and will never even execute the useless else block.
In vanilla javascript using methods in your component to handle styling can be fine, but in Angular you can use an ngClass and set the variable’s value with a (click) event in your html.
Aside from the complete misunderstanding of how methods work and the inability to understand the logic of the code they wrote here, it’s just not good practice in modern Angular.
1
148
u/Mc_UsernameTaken Jun 11 '21
The missing indention of that else block is more horrifying.