RUST Discussion


  • :belt_onion:

    @Buddy said:

    When I do single-line ifs, I put everything on the same line.

    that's what ternaries are for :trollface: then they can comment out entire if/else blocks in 1 swipe!

    [code]
    //isTrue ? doThis() : doSomethingElse();
    [/code]

    Also, what happens when your if looks like this:
    [code]
    //if( (thisThing or (thatThing and !someOtherThing))
    and thisOtherFinalBigThing
    and oneLastThingWeForgotTheFirstTime ) doTheThingNow();
    [/code]

    because
    [code]
    //if( (thisThing or (thatThing and !someOtherThing)) and thisOtherFinalBigThing and oneLastThingWeForgotTheFirstTime ) doTheThingNow();
    [/code] gets kind of ugly with the scrolling and whatnot.



  • @Buddy said:

    When I do single-line ifs, I put everything on the same line. If I need more than one line, I use braces. It's foolproof.

    Depends on the language. That's acceptable in C# where you can breakpoint part of a line, but unacceptable in C++ where you can't.



  • @darkmatter said:

    ternaries

    It's not a case of trying to be efficient with space, so much as not being motivated to add all the extra lines and braces and what not. Also, I'll sometimes allow myself something like

    if(condition) for (int i=0; i<count; i++) {
        doSomething();
    }
    

    just to save the extra level of nesting. Not with for i for j though, because single indenting nested loops just feels weird to me.


  • :belt_onion:

    @Buddy said:

    just to save the extra level of nesting.

    I don't know that I've ever seen anyone do that in practice before outside of "hey here's a trick you could do but seriously never do that because people will hate you" obfuscation/line saving suggestions.

    also, why'd you bother with the brace on the for loop...
    [code]
    if(condition) for (int i=0; i<count; i++)
    doSomething();
    [/code]
    :trollface:

    or even [code]if(condition) for (int i=0; i<count; i++) doSomething();[/code]



  • Like I said, it just feels wrong to have a loop without the braces. Unless its a very simple operation, like buf[i]=0. But then I'd probably put the braces on the if.

    The point is: Judicious use of language features. If I've got to type the pointless parens around the conditional anyway, why shouldn't I be able to reap the rewards, as long as I can still make my intent clear?


  • :belt_onion:

    @Buddy said:

    The point is: Judicious use of language features.

    Filed Under: Reasons the RPG (the language, not the game style) programmers hate the C# programmers.


  • :belt_onion:

    And if this forum were frequented by more 50+ year old coders, I'd expect a lot more likes for that post. As it is, probably half the people here are reading that wiki link and hating me for introducing them to a coder hell they had never dreamed possible instead of liking.

    RPG is one of the few languages created for punched card machines that is still in common use today. This is because the language has evolved considerably over time. It was originally developed by IBM in 1959.
    Seriously, a punch card language ⁉ I am almost ashamed to admit that I can do RPG programming and am under the age of 35....


  • @darkmatter said:

    RPG (the language, not the game style)

    What's with people on here thinking that ARs and RPGs and all that have anything to do with nerd stuff?

    @darkmatter said:

    a coder hell they had never dreamed possible instead of liking.

    Yes, but is it better than sql?


  • :belt_onion:

    @Buddy said:

    Yes, but is it better than sql?

    Well, in RPG-IV, you can embed straight sql in the free format code... so.... I don't know, is that a yes or a no?



  • Doesn't Visual Basic allow you to drop literal excerpts of XML into your source code?



  • @darkmatter said:

    is that a yes or a no?

    Here is an algorithm to determine the answer:

    Loop through all of the RPG files currently available to you.
    
     - For every new RPG file created for and using the features of RPG-IV, if that file contains less than 10% embedded sql, increment YES
     - For every pre-RPG-IV file recently updated to contain more than 10% embedded sql, increment NO
     - For every other file found, increment FILE_NOT_FOUND
    

    Filed under: <code class="lang-auto hljs trwtf">


  • kills Dumbledore

    @Buddy said:

    if(condition) for (int i=0; i<count; i++) {
    doSomething();
    }

    KILL IT WITH FIRE!


  • Banned

    while(*r++=*s++);
    

  • Discourse touched me in a no-no place

    @Jaloopa said:

    KILL IT WITH FIRE!

    Especially if condition is count > 0


  • BINNED

    That's tame. I was, I swear to resident deity, taught this at college, you know, where you should learn how to do stuff properly, as a way to find the last element of a linked list (given any element as a starting point):

    // curr is a pointer to a list element
    for(; *(curr->next) != NULL; curr = curr->next);
    

    Because a readable while loop would take too much space, probably.


  • Banned

    @Onyx said:

    Because a readable while loop would take too much space, probably.

    while((curr=curr->next)->next);?


  • BINNED

    I so saw this coming, I just wondered which of you dickweeds is gonna do it first :P


  • Banned

    If I was a dickweed, I would point out that both my and your snippet have bugs.


  • BINNED

    And I would then say that I don't give a fuck because it's terrible code anyway and should be replaced. Cue flamewar. Victor undecided. Someone gives up. But I have work to do so that will have to be delayed.

    EDIT: And I just found the bug in mine. No idea what I was thinking. Leaving it in as a proof that this kind of shit sucks because bugs are hard to find. At least, that's what my lawyer recommended me to do.


  • Java Dev

    I usually find them like this in our codebase:

    /* Find end of list */
    for( ptail = &head ; *ptail ; ptail = &((*ptail)->next) ) /**/;
    

  • Banned

    That looks strange. Why pointers to pointers?


  • Java Dev

    Useful when deleting or inserting items in a singly-linked list, the pointer points to the pointer you need too update as part of a list change.

    This one would be for appending; you can say item = malloc(sizeof *item); *ptail = item; and that will be a valid append both to an empty list and to a list that already has items.

    By contrast, if you find the last element, you always have to handle the 'empty list' case separate.



  • @Gaska said:

    while((curr=curr->next)->next);?

    if (curr) while (curr->next) curr = curr->next;
    

    @PleegWat said:

    the pointer points to the pointer you need to update as part of a list change.

    Nice.



  • @Buddy said:

    @PleegWat said:
    the pointer points to the pointer you need to update as part of a list change.

    Nice.

    I just realized that, in appreciating your approach, I forgot to call out your lack of style.

    @PleegWat said:

    ```
    /* Find end of list */
    for( ptail = &head ; *ptail ; ptail = &((*ptail)->next) ) /**/;

    
    There is no reason to use the for initializer for a variable that will exist outside of the loop. Similarly, it is bad form to do meaningful work within the condition or iterator. Your code would be clearer as
    ```cpp
    List *ptail = phead;
    while (*ptail) ptail = &(*ptail)->next;
    

    In fact, this would probably be a candidate for braces and newlines if I was writing it; there's a bit much happening with the tail for just one line.


  • Java Dev

    I'm typically in C, and defining the variable in the for body is either illegal or frowned upon. Update could go in the body here because there is no other body, but in real applications I prefer having it in the update. Particularly if the body may call continue. Also I dislike empty update statements more than empty bodies. A longer example:

    item * insert_name(item **pit, char *name)
    {
        item *it;
        for( /**/ ; *pit ; pit = &((*pit)->next) )
        {
            if( 0 > strcmp( (*pit)->name, name ) )
                { break; }
        }
    
        malloc_obj(it)
        it->name = strdup(name);
    
        it->next = *pit;
        *pit = it;
    
        return item;
    }
    

    In this case the initializer is empty, because it would be equal to the function name. For completeness sake, this is in a header:

    #define malloc_obj(o) (o) = malloc(sizeof *(o))
    


  • That feature was added to C (I think in C99, and most certainly by the time of C11). Quoth the N1548 draft of the standard:

    for ( declaration expressionopt ; expressionopt ) statement
    

    Not sure why it's still frowned upon, though...are you dealing with janky old "C" compilers that have barely evolved from the K&RCretaceous era?


  • Java Dev

    Co-workers more like, though the main culprit is no longer among us. And we're still under the 'All declarations at the top of the function' style, and I want to keep that one because there's people around here who happily write 10KLOC functions.

    Least I managed to get rid of -pedantic, so we no longer have to juggle around when the SQL statements get too long.



  • @PleegWat said:

    Co-workers more like, though the main culprit is no longer among us. And we're still under the 'All declarations at the top of the function' style, and I want to keep that one because there's people around here who happily write 10KLOC functions.

    Sounds like there are some 🐄-orkers around your place that need a major overhaul! Did they stare at reload.c for too long in their past life and go (mildly) insane as a result?


  • Java Dev

    Dunno. Possibly. As mentioned, the dinosaur is no longer among us. The main other offender ("If I haven't touched a file in half a year it's easier to rewrite from scratch", ostensibly worked in embedded in the past, also the 10kloc guy) is being kept away from all code as much as possible, with varying success. But I don't particularly mind the status quo and I don't want to give the Chinese team any excuse too deviate from it. Or, again, touch the C code in any way whatsoever.


  • Banned

    I'm using my necromancer powers to bring this topic back to live, just to say this:

    I just realized if if if condition is valid Rust syntax, provided enough code blocks evaluating to bool after that.



  • Deep down, you always knew it. You just refused to accept it.


  • Banned

    I'm the kind of person who loves stupid syntax tricks. I was the one who came up with {[{({()})}]} first.


  • FoxDev

    @Gaska said:

    {[{({()})}]}

    What.
    Thefuck.
    Is that.


  • Banned

    A one element array.


  • FoxDev

    :wtf:
    how?


  • Banned

    In Rust, braces is an expression that returns what's inside. Inside the first set of braces, you have square brackets which are used to denote arrays, with particular elements separated by commas. There are no commas inside, so it has a single element - a second set of braces, which evaluates to the expression inside - it's the third set of braces, redundantly wrapped in redundant parentheses. Inside those third braces, you have an empty tuple.

    So the whole thing is equivalent to [()] (or [{}], for that matter).



  • @Gaska said:

    I'm the kind of person who loves stupid syntax tricks.

    You'll love today's Old New Thing: http://blogs.msdn.com/b/oldnewthing/archive/2015/05/25/10616865.aspx :-)


  • FoxDev

    Wow… that operator is a terrible idea 😷

    Edit: Oh… wait… right, I see what's going on there 😆


  • Banned

    Seen it already. Took me a while too :D



  • Did you know about the arrow operator, though?

    for (x = 10; x --> 0;) { cout << x << ' '; }
    

    Prints

    10 9 8 7 6 5 4 3 2 1

  • Banned

    If I wasn't on mobile, I would post a picture of one particular pink pokemon.



  • @RaceProUK said:

    Wow… that operator is a terrible idea 😷

    Edit: Oh… wait… right, I see what's going on there 😆

    All the world's a 2's complement arithmetic module.



  • @EvanED said:

    You'll love today's Old New Thing: http://blogs.msdn.com/b/oldnewthing/archive/2015/05/25/10616865.aspx :-)

    Holy shit, Raymond Chen is trolling the fuck out of a lot of people in the comments... or is it officially "miss the joke day" today?



  • I saw that article in my feed reader this morning and my first thought was "is it April already?" and my second thought was "I wonder if Microsoft's compiler has a special case for optimizing ~-x and -~x".

    Also, isn't -~--x the same as x--?



  • @David Totzke said:

    We've had this in C# for ages.

    foreach (int number in Enumerable.Range(1,10))
    {
    Debug.WriteLine(string.Format("Value: {0} Tadpole Value: {1}", number, -~number));
    }

    You c++ folks need to get with the times.

    @Michael Geary said:

    Because all the major JavaScript engines are written in C++, JavaScript has also inherited the tadpole operators from the underlying C++ implementation. It is still an experimental feature, but you can enable it with the "use tadpole"; directive:

    (function() {
    "use tadpole";
    var n = 3;
    console.log( "3 + 1 = ", -~n );
    console.log( "(3 - 1) * (3 + 1) ", ~-n * -~n );
    })();

    @Sam Hughes said:

    Excellent post, I have question. I see these work on y, z, and n. Will you add support for i, j, and k?

    @JJJ said:

    I can't believe this post. The tadpole operators have been implemented in gcc for like, forever. But you're making it sound like it's some novel idea? How dare you.

    @Steve said:

    This is crazy. Not only because when these tadpoles grow up, your code will literally start JMPing all over the place, but they will also eat all your source flies.

    @Ryan Voots said:

    Perl has also supported this since 1994 in perl 5. This is just another example of other languages catching up to Perl.

    #!/usr/bin/perl

    use integer;

    print ~-5, "\n", -~5, "\n";

    @Craig said:

    Seems to work with VC++ 6, too! To enable it, you must use

    #define __ENABLE_ESCOTIC_SQUIGGLE_OPERATORS

    Looks like the name is still in flux, though.


  • Discourse touched me in a no-no place

    @Kian said:

    Prints

    10 9 8 7 6 5 4 3 2 1</blockquote>
    

    No, it prints:

    9 8 7 6 5 4 3 2 1 0 
    

    Much more useful.


Log in to reply