175
u/the_monkey_of_lies May 14 '19
I'll fix it!!
if (this.showExtent == false && this.showExtent != true) {
var newShowExtent = true;
if (checkIsBooleanTrue(this.showExtent))
newShowExtent = false;
if (newShowExtent == false && newShowExtent != true)
this.showExtent = true
}
else if (this.showExtent == false && this.showExtent != true) {
var newShowExtent = false;
if (checkIsBooleanFalse(this.showExtent))
newShowExtent = true;
if (newShowExtent == true && newShowExtent != false)
this.showExtent = false
}65
u/Qesa May 14 '19
What are you, some sort of functional programming commie? Get those static
checkIsBooleanTrue/Falsefunctions out and use factories in accordance with proper OO design principles.15
u/the_monkey_of_lies May 14 '19
Don't worry, the function itself is filled to the brim with so many factories it will make your head spin!
36
u/Dworgi May 14 '19
You forgot this:
else { throw new LogicException("Boolean logic has been deprecated, please contact customer support!"); }32
10
7
59
u/farox May 14 '19
Easy
var newExtent = this.showExtent;
while(newExtent == this.showExtent){
newExtent = Random.Next(0, 1) == 0;
}
this.showExtend = newExtent;33
52
3
May 14 '19
Am I good?
showHideExtent(){
if( this.showExtend === false ) this.showExtent = true;
else { this.showExtent = false }
}
or
showHide(){
this.showExtend = this.showExtend === true ? false : true
}
That is how I'd have done it
2
0
37
u/PetrichorMemories May 14 '19
Remark Even for programmers, who saw that there was no implementation problem at all, the introduction of the boolean variable was --and probably still is-- a big leap, as is convincingly illustrated by the fact that for years one would still find even in published programs clauses like:
if c = true then ....
when of course
if c then .....
would suffice (End of Remark).
-- Edsger W. Dijkstra
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD12xx/EWD1284.html
9
u/Dworgi May 14 '19
My wife took ages to learn booleans. I tried to explain it, but apparently it's just something you have to grok.
10
u/PetrichorMemories May 14 '19
This is in part encouraged by language designers who define booleans as a kind of integer or enumeration type.
4
19
u/jorizzz May 14 '19
this.showExtent = !this.showExtent is probably the shortest way, but there are many steps in between that are also shorter.
60
23
May 14 '19
this.showExtent ^= trueis even shorter, but admittedly harder to read16
u/HasFiveVowels May 14 '19
this.showExtent ^= 1?5
3
u/northrupthebandgeek May 15 '19
It's unfortunate that so few languages use int1 for booleans; if they did (or only checked the least significant bit), then
this.showExtent++would work.2
u/HasFiveVowels May 15 '19
I had never considered that. That's an interesting idea. That said, I'm pretty glad that I don't have to run into that kind of code in the wild.
4
u/makians May 14 '19
Can you explain this syntax? I've never seen it before in any language.
17
7
u/b1ack1323 May 14 '19
Exclusive OR is ^ as a bitwise operator.
Which means one or the other is true but not both.
So if showExtent = 1
showExtent ^= trueWould yield flip the 1 to false because what you are really doing is.
showExtent = showExtent ^ trueTo read this in English it would be:
if either showExtent OR true is true, but not both. Then set showExtent to true. If they are both true or both false set to false.
3
May 14 '19
In the same way that
x += 1expands tox = x + 1, this expression expands tothis.showExtent = this.showExtent ^ true. Now^is the XOR operator, that returns true if exactly one of its arguments is true. It is then easy to check thatx ^ true = !x. It is supported by many languages (C,Java,JS, probably more..)2
u/makians May 14 '19
My classes never taught about binary operators so I always forget they exist, thank you!! Is it good or bad practice to use them or is it case dependent?
4
u/LowB0b May 14 '19 edited May 14 '19
case dependent... see for example flag setting when programming for linux in C, or stuff like right/left shift.
Anyway bitwise operators are great when what you need to do is set a specific bit inside something like an integer.
e:
something like this
status = mkdir("/home/cnd/mod1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);from https://linux.die.net/man/3/mkdir4
May 14 '19
Well here we're just code-golfing, I think everybody agrees that `this.showExtent = !this.showExtent` would be a better way to write this specific statement.
And in general, I think that the XOR operator should only be used when you actually want to express an exclusive or, or when you're doing some fancy bit-level magic. Aside from those cases, it's usually better for readability to just code what you mean :)
2
u/stone_henge May 14 '19
Now ^ is the XOR operator, that returns true if exactly one of its arguments is true.
That might have been true if
^was actually a logical operator. The logical equivalent is!=, while^is bitwise-logical, which makes no practical difference when it comes to logic where true and false are just the integers 0 and 1, but makes a huge practical difference for the things one should actually use bitwise xor for, like toggling a specific subset of bits in a register.36
2
1
1
1
1
1
0
May 14 '19
[deleted]
62
u/soundman10000 May 14 '19 edited May 14 '19
this.showExtent = !this.showExtent
edit: just figured out this whole damn thread is sarcasm, i'm an idiot.
82
u/ryeguy May 14 '19
Disgusting, inelegant, unreadable. You should be ashamed of yourself.
13
u/RTracer May 14 '19
this.showExtent = !this.showExtent == true ? true : this.showExtent == true ? false : true;
18
May 14 '19
This is supposed to be a parody/shitpost subreddit. Problem is, shitposting about programming tends to look like actual, in prod examples of terrible fucking code.
6
u/stevenr4 May 14 '19
Nice edit, take my upvote as I try to dig you out of the negative pit of shame
0
1
7
u/[deleted] May 14 '19
This is way more maintainable. Especially if you have to add more cases in the future.