You can simply return the Boolean value of this.visited instead of using an if statement.
Edit: I just realized this is JS which means this.visited may not necessarily be a Boolean and could be any other truthy/falsy value. In that case, I guess this makes sense.
youll save more of your time if you and other people are able to understand what the code does at a glance. !! doesnt read as "cast to boolean," I have to waste time thinking about it. Boolean() is far more obvious as to its function. Unless you're writing code golf, brevity isn't the goal, understandability is.
Tbf !! is at first glance a double negation of an expression (because ! is commonly used as negation), and only reason why you would use it is to cast something to Boolean. So readability is not a reason why not to use it.
Also you still need to pause at statements like this, because javascript type conversion can be weird and at that point you will figure out what !! is. And behavior of Boolean() is also confusing af if you are not used to it.
So realistically speaking you can use both !! and Boolean(), but you should comment what are you casting into boolean, and either be 100% sure that's the only thing that will enter that function or check it.
Because I would rather spend 5 minutes googling some fancy syntax than trying to figure out why something has a non-standard behavior.
You think you can do this, until you realize that "this" is undefined and you cry in the shower for a few hours until you come back and refactor your code to use .bind(this). Don't ask me how I know
Using methods instead. There is no need for the inline syntax sugar, because smaller default casts like integer to decimal aren't needed. Methods such as toString().
You might not want to expose your internal variable, which could be set to an unexpected value any moment in time and be changed by an external actor. The state of an object is typically managed by its own class, and external actors can use the object's actions (its methods ) so there's a separation between the state and the interface.
Yes exactly. I need to add that in JavaScript there are no interfaces neither private fields, so it's a safe bet to use the method instead of the variable. But I'm making some assumptions about the code based on my experience.. the person who wrote it could not have even thought about these things 😁
One simple answer: because other objects also have an isVisited method and you're looping over an array of them or something. Maybe other objects need more complex behavior than just setting a field. And maybe you don't want external code touching that field. JS makes it very easy to shoot yourself in the foot, so this type of thing is useful but it may not be immediately obvious why if if you don't have the full picture. Oh, and the value if visited may not be a boolean, so this method is also useful in returning true or false. Sure, you could just return !!this.visited, but that's less readable
Not really, this.visited could be null or undefined. At which point you’ve changed the behavior of the function. The implementation in the OP only returns booleans, whereas yours could return anything.
I'm not a die hard code cleanliness guy, but, if the isVisited function gets extended later, it's nicer to have the logic contained in the accessor, and not at every place it's used. Code that requires an "isVisited" check, shouldn't need to know how to run that check. Makes for cleaner code on larger projects I think 🤷♂️ but no one's going to shoot you for doing it
51
u/KomaedaEatsBagels Dec 19 '21
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!
40
u/geeshta Dec 20 '21
Java getters be like
75
u/SupercraftD Dec 20 '21
How do u do these framed code snippets
25
u/pablossjui Dec 20 '21
What is exactly bad about this?
I guess it's a bit unreadable
34
u/DavidRoyman Dec 20 '21
Reason it isn't bad code:
- isVisited is a better naming than Visited, especially if the rest of the code is camel case.
- isVisited can be extended later on.
Reasons it is bad code:
- Looks like it's JS
1
u/Knuffya Dec 20 '21
what is a stoped?
1
u/[deleted] Dec 20 '21
Eh... what language is this? Looks like Java but no accessibility modifier or return type on the func?
7
u/[deleted] Dec 20 '21
You can just use (this.visited) instead of the this whole function
2
u/SaucyParamecium Dec 20 '21
sob
1
u/thefriedel Dec 23 '21
My teacher does this always in Python... horrible