I need multiple conditions in While controller, but if I have more then one condition every time is infinite loop.
${__javaScript("${type}" != "book",)} && ${__javaScript("${author_surname}" != "Novak",)} && ${__javaScript("${author_name}" != "John",)}
As per Using the While Controller in JMeter article I think you need to put all the conditions into single __javaScript() function like:
${__javaScript("${type}" != "book" && "${author_surname}" != "Novak" && "${author_name}" != "John",)}
also as per the function documentation:
javaScript is not the best scripting language for performances in JMeter. If your plan requires a high number of threads it is advised to use __jexl3 or __groovy functions.
so it worth considering migrating
Related
I'm writing classes extending from XCTestCase to do some UI Automation tests. In my test base class I have something like:
- (BOOL)isOnLoginFormView
{
return self.app.textFields[#"User ID"] != nil && self.app.secureTextFields[#"Password"] != nil && self.app.buttons[#"<b>Login</b>"] != nil;
}
I use this method in another test case method. In a situation when the login form is not present it should continue with some other code (checking for the existence of another view) but my UI test fails in any case if the above method return NO. Is there anything I can do so the test is not failing if the above method returns NO? Optimally the test cases should only fail if I fail them explicitly with an assert.
Nevermind! I figured out it works with this:
return self.app.textFields[#"User ID"].exists && self.app.secureTextFields[#"Password"].exists && self.app.buttons[#"<b>Login</b>"].exists;
I am trying to build a secure php contact form to allow users (and hopefully not spammers) to send mail.
I am looking at the way of detecting new lines in the from: field, with which users will submit their email address and in the subject: field.
I have 2 alternatives aof the same function to detect new lines and I would like your opinion about which one would be the most reliable (meaning working in the most cases):
function containingnewlines1($stringtotest) {
if (preg_match("/(%0A|%0D|\\n+|\\r+)/i", $stringtotest) != 0) {
echo "Newline found. Suspected injection attempt";
exit;
}
}
function containingnewlines2($stringtotest) {
if (preg_match("/^\R$/", $stringtotest) != 0) {
echo "Newline found. Suspected injection attempt";
exit;
}
}
Thank you in advance for your opinions!
Cheers
The vastly more pertinent question is "Which one is more reliable?". The efficiency of either approach is irrelevant because neither approach should take more than a few milliseconds to execute. Trying to decide between the two based on a matter of milliseconds is a micro-optimization.
Furthermore, what do you mean by efficiency? Do you mean which one is faster? Which one consumes the least memory? Efficiency is an ill-defined term, you need to be more specific.
If you absolutely must make a decision based on performance/efficiency requirements then I'd recommend constructing a benchmark and finding out for yourself which one is the closest fit to your requirements, because at the end of the day only you can answer that question.
I added myself 2 more funcs and did a benchmark of 100000 loops:
function containingnewlines3($stringtotest) {
return (strpbrk($stringtotest,"\r\n") !== FALSE);
}
function containingnewlines4($stringtotest) {
return (strpos($stringtotest,"\n") !== FALSE && strpos($stringtotest,"\r\n") !== FALSE);
}
$start = microtime(TRUE);
for($x=0;$x<100000;$x++) {
containingnewlines1($html); // 0.272623 ms
containingnewlines2($html); // 0.244299 ms
containingnewlines3($html); // 0.377767 ms
containingnewlines4($html); // 0.142282 ms
}
echo (microtime(TRUE) - $start);
Actually, I decided to use the first function, as it covers 2 more cases (%OA and %OD) and as it also includes all the new lines characters variations used by different OSes (\n, \n\r etc).
Trying to get children of a given document type, with umbracoNavihide not set to false:
The following produces correct output.
#foreach (var child in root.Children.Where("ContentTypeAlias == \"DocumentTypehere\""))
{
if (child.umbracoNaviHide == "False")
{
continue;
}
<li>#child.Name</li>
}
This does not:
#foreach (var child in root.Children.Where("umbracoNaviHide == #0 && ContentTypeAlias == \"DocumentTypehere\"","False"))
{
<li>#child.Name</li>
}
umbracoNaviHide is not supported in Umbraco 5 as per the words of Niels Hartvig:
While these special aliases does [sic] [recte: do] a great job and are super easy to use
(albeit totally impossible to discover if you don't stumble across
docs that mention their usage), the problem with these are that
they're 'magic' strings which really is a mess (read: They're hacks
inside the core).
So they won't come back in v5 in the form we know from v4.
So, besides the Linq imitations being somewhat broken anyway, the short answer is that this (either form) shouldn't work (nor should Athul's answer).
The long answer is that you could use this property (and others like it) only if you explicitly support it as part of your Document Type. There is a feature request, though, asking for the 'built-in' umbraco... properties here should you care to follow and support it.
I would personally ask that you don't, however; as using these properties and relying on them are problematic (not least for the point mentioned by Neils himself). Make your own, dedicated properties that are aptly aliased for their task.
you can simply do it like
#foreach (var child in root.Children.Where("umbracoNaviHide != true && NodeTypeAlias == \"DocumentTypehere\" ")
{
<li>#child.Name</li>
}
You can just write for your check on umbracoNaviHide:
if (!child.umbracoNaviHide)
{
continue;
}
<li>#child.Name</li>
Just to add another approach - a few of the existing answers did not work for me - you can try this. Works for me in Umbraco 4.11
#foreach (var child in root.Children.Where(child => child.GetPropertyValue("umbracoNaviHide") == "0"))
{
..
}
I worked on a project which involved complex boolean logic. This complexity made the code very efficient, but unfortunately hard to read.
So we laid the logic out as below, which made it easier to see the groups within the compound statements, and also made it possible to add comments to parts of the logic.
(This code isn't real code from the project, the real logic was more complex)
if (
//comments here!
angle.angle < kQuiteLow
&& (
previousAngle.angle > kQuiteHigh
|| previousAngle.time == kUnknownTime
)
//comments in here too!
&& pairedAngle.angle < kQuiteLow
&& (
//and here
previousPairedAngle.angle > kQuiteHigh
|| previousPairedAngle.time == kUnknownTime
)
)
Have you ever seen this done anywhere else?
Are there any conventions or style guide recommendations on how to lay out very complex boolean logic?
I would refactor the code to use external methods to make it easier to read.
if( ValidAngle(angle, previousAngle) && ValidAngle(pairedAngle, previousPairedAngle) )
ValidAngle( angle, prevAngle){
return angle.angle < kQuiteLow && (previousAngle.angle > kQuiteHigh || previousAngle.time == kUnknownTime)
}
One suggestion is breaking the logic into methods. Easier also to transform your comments into the method name, so you won't need them. Or, if they are too complex, they are included in the method documentation.
if (anglesAreOk(...))
{
}
public bool analyseAngles() {
return angleOk(...) && previousAngleOk(...) && pairedAngleOk(...)
}
You g.t the idea...
I agree with samuelcarrijo and pb, extract the complex expressions into either methods or variables.
If you can't extract a method to a meaningful abstraction, you could consider code fragments or blocks.
From Fowler's refactoring book, I agree with the advice that complex boolean logic should be replaced with methods with meaningful names, e.g.
if(x && y || !b) { }
vs
if(customerIsRepeatCustomerFromIdaho(x,y,b)){ }
Or a variety of unit tests with meaningful names
e.g.
[Test]
public void CustomersFromIdahoGetDiscountsOnAlternatingTuesdays()
{
isRepeat=true;isFromIdaho=false;isTuesday=true;
Assert.AreEqual(Customer.CalclateDiscount(isRepeat,isFromIdaho,isTuesday),.10)
}
How about adding the limits to your classes?
e.g.
if ( angle.isQuiteLow() && previousAngle.isQuiteHigh() && previousAngle.isUnknownTime() )
I've been doing a massive code review and one pattern I notice all over the place is this:
public bool MethodName()
{
bool returnValue = false;
if (expression)
{
// do something
returnValue = MethodCall();
}
else
{
// do something else
returnValue = Expression;
}
return returnValue;
}
This is not how I would have done this I would have just returned the value when I knew what it was. which of these two patterns is more correct?
I stress that the logic always seems to be structured such that the return value is assigned in one plave only and no code is executed after it's assigned.
A lot of people recommend having only one exit point from your methods. The pattern you describe above follows that recommendation.
The main gist of that recommendation is that if ou have to cleanup some memory or state before returning from the method, it's better to have that code in one place only. having multiple exit points leads to either duplication of cleanup code or potential problems due to missing cleanup code at one or more of the exit points.
Of course, if your method is couple of lines long, or doesn't need any cleanup, you could have multiple returns.
I would have used ternary, to reduce control structures...
return expression ? MethodCall() : Expression;
I suspect I will be in the minority but I like the style presented in the example. It is easy to add a log statement and set a breakpoint, IMO. Plus, when used in a consistent way, it seems easier to "pattern match" than having multiple returns.
I'm not sure there is a "correct" answer on this, however.
Some learning institutes and books advocate the single return practice.
Whether it's better or not is subjective.
That looks like a part of a bad OOP design. Perhaps it should be refactored on the higher level than inside of a single method.
Otherwise, I prefer using a ternary operator, like this:
return expression ? MethodCall() : Expression;
It is shorter and more readable.
Return from a method right away in any of these situations:
You've found a boundary condition and need to return a unique or sentinel value: if (node.next = null) return NO_VALUE_FOUND;
A required value/state is false, so the rest of the method does not apply (aka a guard clause). E.g.: if (listeners == null) return null;
The method's purpose is to find and return a specific value, e.g.: if (nodes[i].value == searchValue) return i;
You're in a clause which returns a unique value from the method not used elsewhere in the method: if (userNameFromDb.equals(SUPER_USER)) return getSuperUserAccount();
Otherwise, it is useful to have only one return statement so that it's easier to add debug logging, resource cleanup and follow the logic. I try to handle all the above 4 cases first, if they apply, then declare a variable named result(s) as late as possible and assign values to that as needed.
They both accomplish the same task. Some say that a method should only have one entry and one exit point.
I use this, too. The idea is that resources can be freed in the normal flow of the program. If you jump out of a method at 20 different places, and you need to call cleanUp() before, you'll have to add yet another cleanup method 20 times (or refactor everything)
I guess that the coder has taken the design of defining an object toReturn at the top of the method (e.g., List<Foo> toReturn = new ArrayList<Foo>();) and then populating it during the method call, and somehow decided to apply it to a boolean return type, which is odd.
Could also be a side effect of a coding standard that states that you can't return in the middle of a method body, only at the end.
Even if no code is executed after the return value is assigned now it does not mean that some code will not have to be added later.
It's not the smallest piece of code which could be used but it is very refactoring-friendly.
Delphi forces this pattern by automatically creating a variable called "Result" which will be of the function's return type. Whatever "Result" is when the function exits, is your return value. So there's no "return" keyword at all.
function MethodName : boolean;
begin
Result := False;
if Expression then begin
//do something
Result := MethodCall;
end
else begin
//do something else
Result := Expression;
end;
//possibly more code
end;
The pattern used is verbose - but it's also easier to debug if you want to know the return value without opening the Registers window and checking EAX.