probably a pretty simple question here, but I find it weird. Luckily I found a way around it, but the fact that what I did works and what I have in the title doesn't work is confusing the hell out of me!
I just have a simple if statement...then execute a function. This code works:
if (200 > (x-target.x) && (x-target.x) > 0)
fireWeapon();
yet this code doesn't!
if (200 > (x-target.x) > 0)
fireWeapon();
AS3 does not give me an error either....It just simply does an if statement for the condition
if (200 > (x-target.x))
and seems to ignore the statement where it must be greater than 0. I would like to use the shorter, more mathematically nice looking method in the future, so let me know if there is a way around doing the && sign! Thanks.
if (200 > (x-target.x) > 0) code is working. but what you think is different. computer in order to interpret one sentence. Is evaluated as follows.
1) 200 > ( x-target.x ) If 200 is larger than (x-target.x) return true, not false.
2) true(1) or false(0) > 0 If left-statement is true return true,(because 1 is larger than 0) not false.
As a result, If 200 is larger than (x-target.x) always return true, not false. In general, the syntax used in the computer language is not the same as mathematic syntax.
And you want x> y> z must change to x>y && y>z && x>z.
Related
I recently updated gnuplot latest version, 5.22 and my code didn't work properly. I debugged and found the reasons.
str="1 2"
print word(str,3)+0
In the previous version, 5.06 or older, the print shows 0 values without error.
But the latest version got error, "Non-numeric string found where a numeric expression was expected"
Without +0, both results are the same, blank (no output), but the latest version treats it as string I think.
My code has lots of routine related to word(), so how do I resolve this problem in the new version?
Your code seems to make two potentially dangerous assumptions:
that requesting the third element from a list of two elements returns an empty string, rather than causing an error, and
that converting that empty string to a number will yield 0.
Assumption 1 seems to still hold in gnuplot 5.2.2, but assumption 2 does not. If you really wanted that then you could create a wrapper
f(x) = (x eq "" ? 0 : x)
and use f(word(str,3)) instead of word(str,3). However, there might be a better way to deal with non-existing elements.
Use words to check the index:
w2num(list, i) = (i > 0 && i <= words(list)) ? word(list, i)+0 : 0
Example:
w2num(list, i) = (i > 0 && i <= words(list)) ? word(list, i)+0 : 0
l = "10 20"
do for [i=-1:3] { print w2num(l, i) }
prints
0
0
10
20
0
I have an array of Nodes 'flags', and I want to set my object's position at the first object in that array, it works and the object actually gets positioned as intended, but when I make the comparison it fails and logs 'NO'.
The line of code that sets the position works, but the comparison fails, what's wrong here?!
start: function () {
this.node.position = this.flags[0].position;
this.movement();
},
movement: function() {
if (this.node.position == this.flags[0].position) { // Problem
console.log("YES");
}
else {
console.log("No");
Update:
When I do it like this it works:
if (this.node.position.x == this.flags[0].position.x) // or position.y
Well if you write javascript here (and it looks like you do) there're two things you should know:
You can't compare objects with == out of the box
({"a":1} == {"a":1})
Will return false (you may try it yourself in your browser.
As a workaround you could do something like:
function posCompare(p1, p2){
return p1.x === p2.x && p1.y === p2.y;
}
Then use it instead of == for positions
See how I use === instead of ==? Second thing to know is Use only ===. You can learn the difference Which equals operator (== vs ===) should be used in JavaScript comparisons? but I'd keep away from == anywhere. It's slower, it may cause strange errors here and there - just don't use it at all
I have this game that I made just yesterday, but for some reason, when you get too high of a number for your money or dollars per second, it just comes up as [object Object] dps or something.
Is there any way I can fix this?
<td class="money">${{money}}</td>
<td class="rate">{{rate}} dps</td>
That's the leaderboard output.
You ({{user.username}}) have ${{user.money}} and make ${{user.rate}} per second!
That's the output to show how much money and dollars per second you have.
The 'user rate' function:
buy: function(amount) {
if(Meteor.user().money >= amount && amount > 0)
Meteor.users.update({_id: this.userId}, {$inc: {'rate': (Math.floor(amount/500)), 'money': (0-amount)}});
},
})
I'm using Meteor and Notepad++.
The game is coded in Javascript, I'm just having trouble with the output when you get either 1. Too much money or 2. Too much Dollars per Second.
I don't know what you mean by 'environment info'.
The Max safe integer for javascript is 9007199254740991, i'm assuming you are not hitting that limit!
Add some Number type checks to your code, it sounds like you're suddenly adding strings or some other type of object to his score. eg:
if (!Number.isInteger(Meteor.user().money) || !Number.isInteger(value)) {
alert('found the problem!');
}
Don't use type coersion comparison, that will make "5" == 5, which you don't want when doing calculus.
Use if (typeof a === typeof b && a > b), because there is no "type-safe" comparison for >, <, <=, >=
Hope these pointers will send you in the right direction.
i have a bit of code that i wrote a few weeks ago (the code's purpose isn't so much important as its structure):
if (_image.Empty)
{
//Use the true image size if they haven't specified a custom size
if (_glyphSize.Width > 0)
imageSize.Width = _glyphSize.Width //override
else
imageSize.Width = _image.GetWidth;
if (_glyphSize.Height > 0) then
imageSize.Height = _glyphSize.Height
else
imageSize.Height = _image.GetHeight
}
else
{
//No image, but they can still override it with a custom size
if (_glyphSize.Width > 0) then
imageSize.Width = _glyphSize.Width
else
imageSize.Width = 0;
if (_glyphSize.Height > 0)
imageSize.Height = _glyphSize.Height
else
imageSize.Height := 0;
}
i was going over it tonight, and as i was cleaning it up, i realized that the cleaned version is must more concise:
//Figure out the final image width
if (_glyphSize.Width > 0)
imageSize.Width = _glyphSize.Width
else if (not _glyph.Empty)
imageSize.Width = _glyph.GetWidth
else
imageSize.Width = 0;
//Figure out the final image height
if (_glyphSize.Height > 0)
imageSize.Height = _glyphSize.Height
else if (not _glyph.Empty)
imageSize.Height = _glyph.GetHeight
else
imageSize.Height = 0;
Note: i've trimmed down the code to bare logical flow, and obfsucated the source language.
In the end i took the nested if's, and inverted them. Doing that allowed this shortening. My question is: how can i recognize this in the future?
What are the tell-tale signs that i've just written some code that can be refactored into something shorter?
Another example i had from a few weeks ago was something akin to a permission check: the user can perform an action:
if they have the permission they can do it
if they don't have the permission, but the override is in effect
Which i initially coded as:
if ((HasPermission || (!HasPermission and OverrideEnabled))
{
...do stuff
}
The logical conditions on that if clause seemed kind of wordy. i tried to reach back to my boolean algebra course to figure out how to simplify it. In the end i could do it, so i ended up drawing a truth table:
Permission Override Result
0 0 0
0 1 1
1 0 1
1 1 1
Which when i look at it is an OR operation. So my if statement became:
if (HasPermission or OverrideEnabled)
{
...
}
Which is obvious and simple. And so now i'm wondering how i couldn't see that to begin with.
Which brings me back to my SO question: What tell-tale signs could/should i be looking for in order to recognize that some block of code needs some TLC?
Here are some guidelines from Code Complete, off the top of my head. That is a good book to get for this sort of thing.
Nested if-else and repeated statements in blocks
Long for-loops
Repeated lines/statements or frequently used operations can be placed in a function
If for some reasons you are copying and pasting a line of code over and over again
I found discrete maths to have an influence in how I wrote if statements now. Usually, I see I am writing two same IF statements in 2 blocks, then I would do some mental 'factoring'.
Specifically related to boolean evaluation, it's worth noting that most(?) modern languages implement lazy evaluation.
That is, if "a" is true, then if(a) and if(a or b) are logically and functionally equivelant; the interpreter stops evaluating when it sees or after a true variable. This isn't very important when a and b are variables, but if they're callables [e.g. if(a() or b())], b() will not get evaluated when a is true.
You can save a lot of keystrokes (and processor time) by learning this well:
if(!userExists()):
if(!createUser()):
errorHandling()
else:
doStuff()
else: doStuff()
becomes
if(userExists() or createUser()): doStuff()
else: errorHandling()
Well done. Now, when I see this:
//Figure out the final image width
if (_glyphSize.Width > 0)
...
//Figure out the final image height
if (_glyphSize.Height > 0)
...
I think there is still more refactoring to do. Extracting code into methods isn't just a great way to eliminate redundant code. It's also a great way to make the code self documenting:
I'd be inclined to reduce the code to:
set_final_image_size
With set_final_image_size and its minions defined like so:
def set_final_image_size:
imageSize.Width = final_image_width;
imageSize.Height = final_image_height;
def final_image_width:
if (_glyphSize.Width > 0)
return _glyphSize.Width;
else if (not _glyph.Empty)
return _glyph.GetWidth;
else
return 0;
def final_image_height:
if (_glyphSize.Height > 0)
return _glyphSize.Height;
else if (not _glyph.Empty)
return _glyph.GetHeight;
else
return 0;
Now that you've separated the width and height logic, and noticed that it's identical - what if you were to add, say, getDimension(Direction direction) and setDimension(Direction direction, int length) to your classes? Now you've got
if (_glyphSize.getDimension(direction) > 0)
imageSize.setDimension(direction, _glyphSize.getDimension(direction))
else if (not _glyph.Empty)
imageSize.setDimension(direction, _glyph.getDimension(direction))
else
imageSize.setDimension(direction, 0);
Extracting the local brings us:
length = _glyphSize.getDimension(direction);
if (length > 0)
imageSize.setDimension(direction, length)
else if (not _glyph.Empty)
imageSize.setDimension(direction, _glyph.getDimension(direction))
else
imageSize.setDimension(direction, 0);
taking it a little further:
length = _glyphSize.getDimension(direction);
if (length == 0 && !_glyph.Empty)
length = _glyph.getDimension(direction);
imageSize.setDimension(direction, length);
Which, to my eyes at least, is starting to look pretty nice.
What would be the most readable/best way to write a multiple conditional check such as shown below?
Two possibilities that I could think of (this is Java but the language really doesn't matter here):
Option 1:
boolean c1 = passwordField.getPassword().length > 0;
boolean c2 = !stationIDTextField.getText().trim().isEmpty();
boolean c3 = !userNameTextField.getText().trim().isEmpty();
if (c1 && c2 && c3) {
okButton.setEnabled(true);
}
Option 2:
if (passwordField.getPassword().length > 0 &&
!stationIDTextField.getText().trim().isEmpty() &&
!userNameTextField.getText().trim().isEmpty() {
okButton.setEnabled(true);
}
What I don't like about option 2 is that the line wraps and then indentation becomes a pain. What I don't like about option 1 is that it creates variables for nothing and requires looking at two places.
So what do you think? Any other options?
if (HasPassword() && HasStation() && HasUserName())
okButton.setEnabled(true);
bool HasPassword() {
return passwordField.getPassword().length > 0;
}
etc.
Note that option 1 does not allow for short circuiting behavior. That is, you calculate the value of all of the conditionals before evaluating the result of the first.
I would modify option 1 so that you're using variable names that actually have a meaning. That is, change the name of "c2" to be something like "stationIDIsEmpty" (and move the NOT into the conditional). That way the conditional is readable without having to glance back and forth for every variable.
So my code would probably look like:
boolean enteredPassword = passwordField.getPassword().length > 0;
boolean stationIDIsEmpty = stationIDTextField.getText().trim().isEmpty();
boolean userNameIsEmpty = userNameTextField.getText().trim().isEmpty();
if (enteredPassword && !stationIDIsEmpty && !userNameIsEmpty) {
okButton.setEnabled(true);
}
I voted for Chris Brandsma's answer.
But just wanted to mention the main issue I have with Option 1 is you are losing the benefit of &&. With option one, although I think it's more readable, you are processing comparisons when they may not be required.
Personally, I like the second way, because I find that using that way can make the predication of the conditionals clear. That is, with that method done properly, you can make the conditional comprehensible by "verablizing" it (whether or not you actually speak it is irrelevant).
That is, with your second option, it becomes clear that your conditional translates roughly as this: "If the password length is greater than zero, AND the stationIDTextField (trimmed) is NOT empty, AND the usernameTextField (trimmed) is NOT empty, then..."
I prefer the following:
if (passwordField.getPassword().length > 0
&& ! stationIDTextField.getText().trim().isEmpty()
&& ! userNameTextField.getText().trim().isEmpty())
{
okButton.setEnabled(true);
}
With this coding style I accomplish two things:
I can easily see that each extra line of the if is part of the condition because of the && (or ||) at the beggining.
I can easily see where the if statement ends because of the { at the next line.
Option1 is prime for applying the refactoring 'Replace temp with Query'. The reason being that someone can stuff in code between the variable is initialized and the check and change the behavior of the code. Or the check might be made with stale values.. an update has been made to the textfields between initialization and checking.
So my attempt at this would be
if (GetPasswordLength() > 0
&& FieldHelper.IsNotEmpty(stationIDTextField)
&& FieldHelper.IsNotEmpty(userNameTextField)
{
okButton.setEnabled(true);
}
FieldHelper is a class with public static methods (also called a Utility class / Static class in C#)