43
woops
6y

Saw a girl from my uni upload a photo of her graduation with the following text:
while(!(succeed = try()));

I'm staring at it for 5 mins and i can't figure out if i'm retarded and should drop out of uni or that statement doesn't make sense.

Comments
  • 18
    It's bullshit.
  • 6
    From what I know, it doesn't make any sense whatsoever. I mean I understand what she's trying to say but that statement doesn't make sense
  • 6
    That line of code makes absolutely no sense.
  • 11
    The while loop is not fully declared and assuming try() doesn't fail unexpectedly 'succeed = try()' would evaulate into true which would in turn just eval to false because of the ! which would cause the while loop not to run, ever.
  • 6
    As far as i understand this she's trying to assign a not further declared method to a variable. This could work if try() returns a Boolean i think. Though this whole loop is kinda messed up.
    It's obviously more about the message than being written correct.
  • 3
    @WhAtEvErYoUmEaN so basically she's saying that we all should just give up and don't try even once. Just like turn around and walk away again.
  • 3
    @KittyMeowstika let's hope she's not a cs graduate though. Because that would be a little embarrassing
  • 2
    @nicogramm pretty much yeah.
  • 12
    Correct way to express her message should be:
    While (!succeed)
    {Try();}
  • 5
    @nicogramm that's why i i stared at it for 5 minutes, she is a CS graduate.

    well i guess she wanted to say while !(sucess){try;} but that assignment had me like what the shit is am i disabled and forgot the fundamentals
  • 5
    @KittyMeowstika which would mean that success is defined outside of our scope and try() accesses it or outside of the call stack. So her code is bad in every way.

    I propose:

    while(goal.attemptToReach()){
    if (goal.attemptSucceeded)
    {
    break;
    }else{
    goal.strategy = new Strategy();
    }
    }
  • 1
    @WhAtEvErYoUmEaN reminds me of the different StackOverflow devs: you would definitely be the certified i-follow-every-coding-convention type xD
  • 10
    Help me out here:
    Try returns false , it became true because of !, And the while executed one more time. This time try returns true, it became false and while stopped.
    Seems okay to me.
  • 2
    @rookiemaverick we don't know the data type if succeed or what the return value of try()'s gonna be. but the process of assigning a value to a variable evals to true if it succeeds.
  • 15
    It does make sense

    In C:

    (Succeed = try()) is an expression which evaluates to the new value of success

    !(...) is 0 if '...' was 1 otherwise 1

    And then just a simple while loop

    So it's saying

    While(try() evaluates to not 1)
  • 0
    @j4cobgarby are you telling me that shit really compiles?
  • 0
    @WhAtEvErYoUmEaN yes that's what I too meant
  • 3
    why would someone assign the return value to a variable on a while statement?

    why not jut while !(try()) assuming try returns a value
  • 0
  • 4
    Don’t see the problem with that.. Would work with most languages.
  • 2
    @WhAtEvErYoUmEaN and that kind of code is useful especially for error handling in C, it seems
  • 1
    @Bitwise But there is no inside of this while loop. It is terminated with a ; right after declaration.

    If it really is valid code I'd say it's a very bad way if putting it that requires an overly amount of time to understand and maintain.
  • 5
    @j4cobgarby I definitely learned something today...

    1: This is possible and valid
    2: Never write code like this.
  • 0
    @j4cobgarby it doesn't have to be 1 to be considered true.
  • 2
    @WhAtEvErYoUmEaN you will see a lot of code like that in legacy codebases.
  • 0
    @electrineer you're right sorry, I was being stupid, I got it right in the first sentence though xD
  • 2
    @WhAtEvErYoUmEaN It's sweet sweet C. I love it.
  • 0
    @WhAtEvErYoUmEaN from wikibooks.org:

    An expression statement consists of an optional expression followed by a semicolon (;). If the expression is present, the statement may have a value. If no expression is present, the statement is often called the null statement.
  • 0
    Succeed keeps getting assigned to the result of try function. If it returns null, the loop is broken out of.
  • 0
    I guess I won't be touching C anytime soon. Sweet sweet high level managed languages it is for me.

    For now.
  • 1
    @woops succeed might be a useful variable if a signal is caught.
  • 4
    Are you people serious? This is completely valid and sensible code?
  • 0
    @sSam right? Exactly
  • 0
    You'r dumb for wasting you'r time in other peoples careless mistakes
  • 0
    @Bitwise or then try() just returns some useful value like the grade and it's used later.
  • 1
    @sSam Valid? Yes. Sensible? Depends on the styleguide.
  • 0
    @carlosjpc I see what you did there
  • 1
    @electrineer it can be really good code depending on the context and since there's no context, there's nothing to discuss as it CAN be sensible.
  • 0
    @sSam oh, I thought you were asking if the code is valid and sensible.
  • 5
    Does REALLY anyone have never seen stuff like that?!

    This is also valid in Java (or, at least, it was with SE 7):

    while((line = br.readLine()) != null){
    // Do stuff
    }

    The loop's condition is always evaluated, so the function "try()" is executed at least one time. If that functiom returns false, the loop is executed one more time. This means "if you fail, continue trying until you succed".
    She totally deserves her graduation.

    Jesus, you are all arguing on what that try function returns. STOP BEING ASSHOLES, this isn't StackOverflow. The concept of that code is really simple (and syntax is correct too), so please stop wasting time arguing about bullshits.

    Peace.✌
  • 2
    @Bitwise is this better?

    while(!(succeed=try())) {}
  • 0
    @Bitwise how can it look like it's doing nothing when it's clearly calling a method...
  • 1
    The semicolon makes sense to me, that’s a way to block execution without actually writing a loop.
  • 1
    @Bitwise

    A while loop with just a semicolon and no body is a common practice for blocking execution until the condition is false.

    I understand that it’s valid. I don’t think you and I disagree.
  • 0
    @Bitwise

    Also, think mutex locks.
  • 2
    @Bitwise
    1. It clearly can loop.
    2. It clearly calls a method every loop.
    Anyways, this is useless discussion so I'm out.
  • 6
    I am amused by all this debate when you could, like, test it?

    It looks like C to me and it looks like it should work fine. To make sure, I wrote it up and ran it. Of course you have to define 'succeed' and 'try()', but once you do it absolutely runs exactly the way she intended it to. I actually think it is kind of a pretty cool line of code, although I can't think of a use case offhand. Also seems like perfectly readable and understandable C code that would not throw me at all if I ran into it in a code base.
  • 1
    there is nothing wrong with the code. the try function might be actually returning any kind of data type. maybe a boolean ( false ) if something goes wrong in the try function and also a useful information to work with

    while ( ! ( success = try () ) );

    ( sucess = try() ) expression is first of all executed, the value of try() is assigned to sucess if that value is false the while loop will continue until the value of success is something else. it is still the same thing as

    var success = try();

    while ( ! success ) {

    success = try();

    }
  • 1
    @catadoxa

    For what it’s worth, I agree. This shouldn’t trip up anyone.
  • 0
    @NoMad

    This is best understood with semaphores, in my opinion. Imagine you have a portion of a function that has a critical section, it might be written like this:

    // Entry Section
    wait(S);

    // Critical Section
    if (0 < V)
    V--;

    // Exit Section
    signal(S);

    But what does that wait(S) call do? There’s probably a while loop terminated with a semicolon, like this:

    while(!lock.attainedLock());
  • 2
    It makes sense in C. It’s a while loop that does nothing but continuously check a condition. That condition assigns a success value (presumably) to the success variable, which also becomes the result of the assignment in C, which checks whether it is != 0 then flips it with the !.

    I’d say it’s legit. The semicolon is the body of the loop but it does nothing. All the logic is in the condition.
  • 0
    It's a valid C loop, albeit an overly nagging one.
  • 1
    @j4cobgarby
    Php, C# and Javascript handle the statement likewise, if I recall correctly. And AFAIK there is no need for a loop body in those language.

    So it's barely readable and awful style, but technically correct in at least 4 languages.
  • 3
    It's a valid C code meaning "try until succeed" or " never give up"
  • 0
    we should add test to this statement
  • 1
  • 1
    @NoMad WHY DID YOU TAG MY USERNAME ON DEVRANT FOLLOWED BY A NUMBER, FELLOW HUMAN? I DON'T UNDERSTAND, PLEASE HELP ME UNDERSTAND.
  • 1
    @j4cobgarby I was beginning to go crazy. Thank god there is someone that still know how to code.

    You can definitely assign value in a evaluation and evaluate the resulting assignment.
  • 0
    @woops for the sake of making it comprehensible to more people? Duh.
  • 0
    The rant and whole comment system brought back my hate for the c language
  • 2
    @catadoxa @benrooke I think the first comments were made by HTML guys 😉 or those who are more common with overly restrictive languages like C#.
  • 1
    @electrineer I'm pretty sure this code works in c#
  • 2
    @Sefie I think it's quite elegant code, if you wanna see some horrible syntax then:

    int**(*f)(int**,int**);
  • 1
    boolean succeed = false;
    while(!succeed) {
    succeed = try();
    }

    guess you can do it like in the rant but it's more readable this way
  • 2
    @justmove not quite the same, you'd have to set the initial value of success to try() instead of false

    (Also tempted to point out 'false' isn't valid in C, but I'm not entirely sure it this is C)
  • 0
    Totally valid code. In most languages. It hasn't an empty while body. The body is just pulled into the while expression. So literally just another way of writing:

    while(!succeed){ succeed = try(); }

    The original code is also way neater. But ok...
  • 0
    Nah it makes sense. If she trys and it returns false, she failed and the loop goes again. If try() returns true then she succeeded and she can stop....

    I mean personally I try to keep pushing through the good times and the bad, but hey, whatever floats your boat.
  • 0
    Ummmm...

    While (!succeed) { try(); }

    While (!(succeed = try()))

    Even someone who doesn't code would be able to guess which one is right...
  • 2
    First of all, apart from the empty body, assigning return values inside the conditional part of the loop is a common way in most dynamic languages. In PHP it is the norm for traversing many collections:

    while( $row = $stmt->fetch() ) {
    // do something with $row
    }

    The fetch method gets the next row in the result until the end of the result set, in which it returns false to indicate EOR (end of results).

    Also, as I see others rewrite the given example to be:

    while( !success ){ try(); }

    I see infinite loop, unless they access "success" as a global variable inside "try()". Not a good rewrite at all, but I'll give the benefit of the doubt about pseudo code, even if they took the code written literally.

    I also agree that the code as written has many uses for blocking/waiting. It also bears similarities with the run-loop for games and other gui applications that wait for the user to do stuff, and depending on what, quit the app by exiting the loop.

    (and yes, pseudo code)😉

    // init stuff

    while( runloop() );

    // cleanup on isle 3

    bool function runloop(){
    // do lotsa stuff

    // if abort by user return false
    // else react to user bs

    // sleep some short time

    // return true
    }
  • 0
    It does make sense, in C. It has sme redundancy but its a valid code. Assignment is also an expression so the fllow is following.

    1. Call try()

    2. Assign its retudn val to var succeed

    3. Expression of assignment is value assigned itself

    4. negate the expr value

    5. you know what while does, so...
  • 0
    My coworker pointed out the real issue here: try is a reserved keyword in most languages and may not be used as a function name.
  • 2
    @Sefie but not all. Say, C
  • 1
    This kind of statement is also often used to read files

    string line;
    while ((line = file.readLine()) != null) ...
Add Comment