vendredi 27 juillet 2018

Refactorisation of an if Block

In the following code I have 2 if block.
A, The first return the error when the object is null.
B, the second will try to send the object and if it' fails then return and error. It should be executed only if not A, the object is not null.

private bool SendToERP(Foo foo, out string error)
{
    error = "";
    var ticketIn = TicketFromFoo(foo, out string err);

    if(ticketIn == null)
    {
        error = err;
        return false;
    }

    if ( !GenericERP_TicketSubmit(ticketIn, out err))
    {
        error = err;
        return false;
    }

    return true;
}

The action after those condition are the same and I want to refactor on into a unique if block.

As I can't warp my head 2 condition and an and I wrote a simple truth table to help me. But it didn't help me mutch.

Alternative c.if in JSF

Does someone know a alternative to <c.if> for view render time, or even if that is possible. For what I have search until now it doesn't exist, or the alternative would be using the "rendered", but for me doesn't really work.

<af:listView value="#{bindings.date.collectionModel}" var="item" id="lv1">
    <af:listItem id="li2">
        <af:iterator id="i1" value="#{bindings.list.collectionModel}" var="row">
            <c:if test="#{item.bindings.attrid.inputValue eq row.attrid}">
                <ui:param name="varUI" value="true" />
                <c:set var="varC" value="true" />
            </c:if>
        </af:iterator>
        <af:outputText value="#{varUI}" id="otqaswq"/>
        <!--<af:outputText value="#{varC}" id="otqawq"/>-->
        <af:selectBooleanCheckbox text="Activated" id="sbc2" value="#{varUI}"/>
    </af:listItem>
</af:listView>

So in the code above I know that "c." they run in different time, view build time and not on the render time. What I needed was that the af:selectBooleanCheckbox would appear true or false depending if there are values with the same value. My question is, someone know a alternative way of doing this?

multiple string comparison in a single if statement shell script using OR gate

I am trying to compare the value stored in string variable with three different possible values and if any of them does not matches then trying to throw error, in a single if statement using the logical operator OR. But everytime I am getting error even though the value stored in the variable is same as one of the possible value. Please find the snippets I tried.

    if [[ "$TYPE" != "LOCAL" || "$TYPE" != "REMOTE" || "$TYPE" != "BOTH" ]]; then
        echo -e "\n\tINCORRECT OR NULL ARGUMENTS PASSED. PLEASE VERIFY AND CORRECT THE USAGE MENTIONED AS BELOW: \n"
        Usage
        exit 1
    fi


    if [[ "$TYPE" != "LOCAL" ]] || [["$TYPE" != "REMOTE" ]] || [["$TYPE" != "BOTH" ]]; then
        echo -e "\n\tINCORRECT OR NULL ARGUMENTS PASSED. PLEASE VERIFY AND CORRECT THE USAGE MENTIONED AS BELOW: \n"
        Usage
        exit 1
    fi

Barcode scanner app parses barcode string to URL

I'm trying to build an app for learning purposes. It's a simple QR code and Barcode scanner app with a two flows:

  1. If the pattern of the scan is an URL open the browser
  2. If the pattern is not an URL open the browser with google.com/+string from the scan.

I have achieved goal number one with the if statement

public void handleResult(Result result) {
    final String myResult = result.getText();
    Log.d("QRCodeScanner", result.getText());
    Log.d("QRCodeScanner", result.getBarcodeFormat().toString());

    if(Patterns.WEB_URL.matcher(result.getText()).matches()) {
        //  Open URL
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(result.getText()));
        startActivity(browserIntent);
    }
}

My challenge is now with goal number to which I just can't get to compile. I was thinking using an else statement and parse Uri with something like google.com + the string. The expected result is that if the scanner returns and value which is not a URL pattern open the browser go to google and search with the return value.

I hope I make myself clear. Pretty much a newbie here - trying to learn terminology and way to communicate.

Why is my output not showing up in the if statement pseudocode?

i have researched the threads but i couldn't quite get this pseudocode to work:

Input 9 as a value for x
if(x < y)
min= x;
else
min=y;
Output the value of min

this is what i wrote:

ORG 300
    INPUT
    STORE X
    IF,     LOAD Y
    SUBT X
    SKIPCOND 000
    JUMP THEN
    THEN,   LOAD min
    ADD X
    STORE min
    LOAD min
    ELSE,   LOAD min
    ADD Y
    STORE min
    LOAD min
    OUTPUT
    X, Dec 0
    Y, Dec 5
    min, Dec 0

i have put the input as 9 in dec. the output shows up as 14 and i don't know where i went wrong. please bare with me as this is the first time ive ever used the marie sim software.

Thanks.

webscrapping and Error in if (grepl("<|>", x)) { : argument is of length zero

I am extracting url links from 611 webpages. The code below helps me to download them. The problem is that I obtain the following error Error in if (grepl("<|>", x)) { : argument is of length zero, which suddenly stops the process and do not return the desired list of links. How can I skip this error message and continue with the data collection process?

real.links <- paste0("http://www.bursamalaysia.com" ,links) # if necessary, I can provide the information of the vector 'links'

doc.link <- lapply(real.links, . %>%  
            read_html() %>% html_nodes("iframe") %>% 
            html_attr("src") %>%  read_html() %>% 
            html_nodes("a") %>% html_attr("href") %>% print())

# When I inspect "print()", it seems that the function stops at the link 128

check If one variable only out of many is greaten than another vairable in PHP

I have a scenero, i want a situation to check and return value FO 1. IF ONLY ONE VARIABLE OUT OF THREE IS GREATER THAN ANOTHER VARIABLE 2. IF TWO OR MORE VARIABLE OUT OF THREE IS GREATER THAN ANOTHER VARIABLE

my code below

if(   $date1 and $date and $LatestMaintenancePcd ) > $today){
                       $indicator = 'success'; 
                    }

this only works for if all ther variables greater than today, but i want if one variable only is greater than today, also if two variable or more is greater than today