Guaging Knowledge via the Ternary Operator



  • @Anonymouse said:

    What the heck is a "<=>" operator? "x is less than, equal to or greater than 0"?

    In Commodore (and presumably other Microsoft-derived) BASIC(s), the answer to that question used to be yes!




  • @blakeyrat said:

    Who took a dump in your cornflakes this morning? Relax buddy.

    I'm only mimicking the tone you used, so relax buddy.

    I don't know what software industry you work for, but the one I'm in has a ton of friggin' idiots in it. If you have two equivalent options, and one is easier for friggin' idiots, you should use that one. Otherwise, the next friggin' idiot who gets a hold of your code (and it will happen sooner or later) is going to shit all over it trying to use the more advanced constructs you're comfortable with, but they are not.

    As someone else said, if the company cannot spare the expense for competent developers, then that is no longer my problem.  Should I forego lambda expressions in C# because some programmers might not be familiar with that specific syntax or with functional code in general?  Please.  Writing code to be maintained by the lowest common denominator only serves to make both the development and maintenance endeavours much more difficult for people who have more than two functioning brain cells.

    Programming is only about "writing code" if you only care about writing code. If you care about creating good, stable products, usable products, writing code is the least of it. What amazes me most is how many programmers that simply do not get that.

    What amazes me is how you think you can end your posts with ambiguous platitudes in order to hide your irrational and frankly ridiculous fear of simple concepts without getting called out on it.

    It sounds to me like you inherited somebody else's code that used the ternary operator and didn't know what it meant at the time, so instead of taking six seconds to learn about it, you ran out behind the building and cried for the rest of the week.  But I'll give you the benefit of the doubt and assume that you really just inherited some very poorly-written code abusing the construct - as one can abuse just about any construct - and decided to blame the syntax instead of the programmer, maintaining your fervent hatred for it in much the same way that some grown men and women continue to be afraid of spiders.



  • @blakeyrat said:

    @Zecc said:
    That's harsh! I thought the word was relatively known in this line of busines, and I only used it because I thought "get" (with quotes included) wouldn't be strong enough.
     

    It is known, but that's not the point. The point is you use that word instead of a word everybody knows as a way of sounding smarter than everybody else. I'm not a fan of that. Much like the ternary operator, you're using a word that's entirely redundant, yet not as well-known.


    Grok is a very common word among programmers (especially in online communities) and it is easy to understand the meaning from the context. Get over it.



  • @Aaron said:

    As someone else said, if the company cannot spare the expense for competent developers, then that is no longer my problem.
     

    So you don't give a shit if the program you worked on becomes buggy because your replacement couldn't maintain it. Fine, if you feel that way, although I don't want to be a customer of your company knowing that.

    @Aaron said:

    Writing code to be maintained by the lowest common denominator only serves to make both the development and maintenance endeavours much more difficult for people who have more than two functioning brain cells.

    One of the points I'm bringing up is that if you know how to use the ternary operator, you also know how to use if(). So I don't see how development becomes "much more difficult."



  • @blakeyrat said:

    I don't know what software industry you work for, but the one I'm in has a ton of friggin' idiots in it. If you have two equivalent options, and one is easier for friggin' idiots, you should use that one.

    So I shouldn't use map and grep in Perl? I shouldn't use function pointers in C? I shouldn't use anonymous types in Java? I shouldn't use lambda expressions, LINQ, delegates, or iterators in C#? I shouldn't use macros in LISP?

    If you chain yourself to the lowest common denominator you'll write LCD code. Hell, if {} else {} is really just nice syntax to abstract away jumps (goto). Maybe we shouldn't use if {} else {} because it is "too complex"?

    Here, let me list some language features and you tell me where I should stop

    1. if {} else {}

    2. while loop

    3. for loops

    4. functions

    5. iterators (foreach loops)

    6. pass-by-reference

    7. classes

    8. pointers

    9. polymorphism

    10. function pointers

    11. anonymous types

    12. first class functions

    13. map, grep, foldr, and friends

    14. closure

    15. lambda expressions

    16. macros (LISP styles)



  • @blakeyrat said:

    One of the points I'm bringing up is that if you know how to use the ternary operator, you also know how to use if(). So I don't see how development becomes "much more difficult."

    but which is easier to read, write and understand?

    int hello;

    if (world)

    {

    hello = 1;

    }

    else

    {

    hello = 2;

    }

    or
    int hello = world? 1 : 2;

    keep in mind it also helps you keep more code on the screen at once which makes the code easier to read



  •  You know, you guys don't have to agree with me. You also don't have to be complete assholes. I'm done with this thread.



  • @blakeyrat said:

     You know, you guys don't have to agree with me. You also don't have to be complete assholes. I'm done with this thread.

    WTF? I was serious, where would you draw the line and stop using language features?



  • @tster said:

    @blakeyrat said:
    @Zecc said:
    That's harsh! I thought the word was relatively known in this line of busines, and I only used it because I thought "get" (with quotes included) wouldn't be strong enough.
     

    It is known, but that's not the point. The point is you use that word instead of a word *everybody* knows as a way of sounding smarter than everybody else. I'm not a fan of that. Much like the ternary operator, you're using a word that's entirely redundant, yet not as well-known.

    Grok is a very common word among programmers

    Yeah, but that's only because we're geeky scif-fi nerd saddos.  No need to go drawing attention to it! 

    @tster said:

    (especially in online communities)

    Mention of "online communities" only makes things worse!


  • @blakeyrat said:

    You know, you guys don't have to agree with me. You also don't have to be complete assholes. I'm done with this thread.
     

    I only see one person consistently being a condescending dick here.

    @blakeyrat said:

    I'm sure that lowers my 1337ness in your eyes, but I'd rather be less 1337 than accidentally delete files.
     

    @blakeyrat said:

    Do you care more about having bug-free software, or proving you're smarter than your co-workers?
     

    @blakeyrat said:

    Well, first of all, you can't expect a programmer who uses the word "grok" in casual conversation to do anything except sit in the basement and watch Star Trek.
     

    @blakeyrat said:

    It is known, but that's not the point. The point is you use that word instead of a word *everybody* knows as a way of sounding smarter than everybody else. I'm not a fan of that. Much like the ternary operator, you're using a word that's entirely redundant, yet not as well-known.

    Also, that book sucked shit. I'll never understand why that's considered Heinlein's best work.

    @blakeyrat said:

    I don't know what software industry you work for, but the one I'm in has a ton of friggin' idiots in it. If you have two equivalent options, and one is easier for friggin' idiots, you should use that one. Otherwise, the next friggin' idiot who gets a hold of your code (and it will happen sooner or later) is going to shit all over it trying to use the more advanced constructs you're comfortable with, but they are not.
     

    @blakeyrat said:

    Programming is only about "writing code" if you only care about writing code. If you care about creating good, stable products, usable products, writing code is the least of it. What amazes me most is how many programmers that simply do not get that.

    Did you get all that?

    @blakeyrat said:

    So you don't give a shit if the program you worked on becomes buggy because your replacement couldn't maintain it. Fine, if you feel that way, although I don't want to be a customer of your company knowing that.

     

    And this one is just plain stupid, of course knowing how to use the language you are programming in makes a difference in your ability to write bug-free software.

    @blakeyrat said:

    Secondly, writing bug-free software has pretty much nothing to do with your knowledge of the language.
     



  • @blakeyrat said:

    @Aaron said:

    As someone else said, if the company cannot spare the expense for competent developers, then that is no longer my problem.
     

    So you don't give a shit if the program you worked on becomes buggy because your replacement couldn't maintain it. Fine, if you feel that way, although I don't want to be a customer of your company knowing that.

    I used to think that if code was sufficiently well-written, any programmer could maintain it.

    Then one day, I was told I was going to be leaving my current contracting assignment in two weeks, and I should wrap everything up and hand it off.  Two weeks later, I was told, "We don't have anything for you to do, our bench room back at the office is currently full, and the company you were working for has graciously allowed you to keep showing up for work there.  So, hang out in your old cube until further notice."  Having nothing better to do, I finished documenting my old code, cleaned it up, refactored it, made it all pretty.  I had several people review it, including some who did not know the language (C) in question.  The last review was from someone who wasn't a programmer; I asked her to look it over and tell me in her own words what she thought it did.  She explained it perfectly.

    The first thing my replacement (when they finally hired a replacement for me, a couple years later) did was deleted all the comments to speed up the code.  Then he spewed crap code all over the thing.  About four months after he was hired, someone contacted me for assistance with it.  I said, "I put everything under SCCS version control; you should be able to just pull the last version I did out again".  (It was what was available on the system.)  The response was, someone had deleted the version control directory, because they didn't know what it was.

    What can you do in the face of THAT?



  • @tgape said:

    What can you do in the face of THAT?

    Easy - laugh and put the phone down. Insulting their competence is optional but recommended.



  • @kraagenskul said:

    Many if-else statements can simply be modified to use the ternary operator, but many programmers are either unfamiliar with it or unwilling to use it. I try to use it where ever possible and I find you can guage a coder's skill based on their using it. But that measurement went out the window the other day:

     x = (logic statement) ? true : false;

    Multiple times throughout their code, and the occasional:

      x = (logic statement) ? false : true;

     

    I can do better:

      (x > max) ? (max = x) : (max = max);

     Found in production code.



  • @North Bus said:

    @blakeyrat said:

     BTW, the ternary operator is also in Javascript of all places.
    This makes me laugh for reasons I don't entirely understand.

    Say, maybe you ought to check your boiler for a carbon monoxide leak..




  •  Not yet mentioned :

    if (Something)

       SomeEVar = SomeValue ;

    else

       SomeFVar = SomeOtherValue ;

    Is that intented or a typo?

     

     

    SomeVar = Something ? SomeValue : SomeOtherValue ;

    Probem solved not even possible.


    Any time copies are made of anything, they may get out of sync.

    Do not repeat yourself.


  • I forgot : if the problem is just ?: symbols considered too obscure, then languages should allow this :

     

    SomeVar = if(Something) SomeValue else SomeOtherValue


    But making it any more verbose juste makes it less readable IMHO.


     



  • Another couple months and it'd have been an even 3 year old necro. Amateur.



  • 3 years before anyone noticed the typo, dosent that go to prove his point about introducing obscure bugs?



  • @Musaran said:

    SomeVar = if(Something) SomeValue else SomeOtherValue
     

    which is the Python way of doing things

    What is blakeys opinion on Python & why dont we see much of it here.

     



  • @ip-guru said:

    @Musaran said:

    SomeVar = if(Something) SomeValue else SomeOtherValue
     

    which is the Python way of doing things

    What is blakeys opinion on Python & why dont we see much of it here.

     

    inb4 "no ones uses Python".

    Actually what you wrote is illegal syntax*. It should be:

    SomeVar = SomeValue if Something else SomeOtherValue

    I've recently used that same structure in a personal project of mine.

    It felt wrong. And I know exactly why. Because the condition is written in the middle of the two alternative values.

    @ip-guru said:

    3 years before anyone noticed the typo, dosent that go to prove his point about introducing obscure bugs?
    3 years ago there was no typo. Musaran's post wasn't here. That wasn't a quote from the thread.

     

    *Just re-read and realized that was a false accusation. Sorry about that.



  • @ip-guru said:

    What is blakeys opinion on Python & why dont we see much of it here

    Well, he doesn't care about Python, but ask him about Pythong and you will hear a mouthful.



  • @Zecc said:

    Actually what you wrote is illegal syntax*. It should be:

    SomeVar = SomeValue if Something else SomeOtherValue

     

     

     

    Opps My bad.

    I wasnt sure when i first came across this eithe, but it is much better than tha and/or hack that was previously (ab)used to achive the same results.

    I didnt realiste the "typo" wasnt from the thread either, probably time to refill the grave on this one

     


  • Garbage Person

    @PJH said:

    @Tyler said:
    std::cout << "Good morning " << ((gender == Male) ? "Sir" : "Ma'am") << std::endl;
    I do, however have issues with things like:

    		syslog(LOG_NOTICE, "WebRelay %s:%d Relay: %s Input: %s Reboot: %s",
    			inet_ntoa(sin->sin_addr.s_addr), ntohs(sin->sin_port),         
    			relay  == 0 ? "on" : "off",                                    
    			input  == 1 ? "on" : "off",                                    
    			reboot == 0 ? "Auto Reboot off" :                              
    			reboot == 1 ? "Pinging" :                                      
    			reboot == 2 ? "Waiting for response" :                         
    			reboot == 3 ? "Rebooting" :                                    
    			reboot == 4 ? "Waiting for reboot" :                           
    				      "Unknown"                                            
    			);
    ... What the fuck does that even do?

     FUCKING FUCK NECRO BASTARD


  • Discourse touched me in a no-no place

    @Weng said:

    @PJH said:

    [Nearly 2 years ago]@Tyler said:
    std::cout << "Good morning " << ((gender == Male) ? "Sir" : "Ma'am") << std::endl;
    I do, however have issues with things like:

    		syslog(LOG_NOTICE, "WebRelay %s:%d Relay: %s Input: %s Reboot: %s",
    			inet_ntoa(sin->sin_addr.s_addr), ntohs(sin->sin_port),         
    			relay  == 0 ? "on" : "off",                                    
    			input  == 1 ? "on" : "off",                                    
    			reboot == 0 ? "Auto Reboot off" :                              
    			reboot == 1 ? "Pinging" :                                      
    			reboot == 2 ? "Waiting for response" :                         
    			reboot == 3 ? "Rebooting" :                                    
    			reboot == 4 ? "Waiting for reboot" :                           
    				      "Unknown"                                            
    			);
    ... What the fuck does that even do?

    Sends to /var/log/messages such informative debug such as:


    WebRelay 192.168.100.1:32767 Relay: on Input: off: Reboot: Pinging

    WebRelay 192.168.100.8:1024 Relay: off Input: off: Reboot: Waiting for reboot

    WebRelay 172.19.52.2:12345 Relay: off Input: off: Reboot: Rebooting






    I will note - in passing - that the code originally quoted is still in our code-base but has been obsoleted. So shouldn't be being used.

    FUCKING FUCK NECRO BASTARD
    Heh.


  • @blakeyrat said:

    If you're writing software for your own personal use that nobody else will ever see, then sure: go ahead and knock yourself out. But you don't need me to tell you that.

    The problem comes when you're working on a team, and the guy who maybe isn't as sharp as you are gets wind of this, that's when it turns into a disaster. Sure, *you* know how to use it properly, but what happens when *you* leave the company? What happens when *you* passes the module off to somebody else to maintain?

    I'd say the rule to writing code (on your own or in a team) is that it's plainly and easily readable. If you look back on your code and start wondering what a certain bit is doing then you might not be writing it well enough. Likewise, if you're writing something that your team cannot easily read and understand, then you might need to alter your coding style a bit.

    There's nothing inherently wrong with ternary operators. They simplify an if statement and can make the code more succinct while still remaining easy to understand. Yes, nesting these is not always a good idea™, and using them to assign a boolean value in languages which would automatically return one anyway is daft, but that doesn't make them bad, it just means the person who wrote those is an idiot.

    It's the same with if statements. I've seen huge messes of if statements with long chains of elseif parts, which was easily replaced with a switch that made the whole thing more readable. Does that make elseif bad? No, it just means it can be misused.

     



  • @blakeyrat said:

    Plus, it's relatively obscure, and I can guarantee there's entire teams of programmers writing bug-free software right now that have never heard of it.
    Obscure? Are you kidding me? Every developer I know is aware of this, and I know quite a few developers who program in quite a few different languages.

    @blakeyrat said:

    So anyway, we're talking about an operator that's entirely extraneous, that is prone to abuse, and that not every programmer is taught.
    Sorry, are you really suggesting that programmers get where they are because they're taught all they need to know? I'd be amazed to see any school teach to that level.

    @blakeyrat said:

    The assumption that language knowledge has *anything* to do with the bugginess of produced software... I need some evidence there. I'm not going to take that one on faith, my experience says it's wrong, my gut-check says it sounds wrong. I'm sure products like Lotus Notes were written by some very smart people.
    It's just plain old common sense that knowledge of a language reduces the bugs. For example, John Smith doesn't know much about interacting with a database in his language of choice beyond that basics. Now he's just written a load of code with gaping injection holes in it. A bit more knowledge about how to sanitise in his language of choice would have helped there.

     

     



  • @tster said:

    @blakeyrat said:

     You know, you guys don't have to agree with me. You also don't have to be complete assholes. I'm done with this thread.

    WTF? I was serious, where would you draw the line and stop using language features?
    He does that whenever he realises his views aren't that popular and he has run out of arguments.

     


  • :belt_onion:

    @ASheridan said:

    @tster said:

    @blakeyrat said:

     You know, you guys don't have to agree with me. You also don't have to be complete assholes. I'm done with this thread.

    WTF? I was serious, where would you draw the line and stop using language features?
    He does that whenever he realises his views aren't that popular and he has run out of arguments.
    I'm sure tster is going to appreciate your answer in case he ever decides to rejoin this forum that he left more than 2 years ago

     



  • @bjolling said:

    I'm sure tster is going to appreciate your answer in case he ever decides to rejoin this forum that he left more than 2 years ago
    Yeah, I didn't realise this thread was that old until I'd read some of the more recent of the newer comments. I just look at the threads most recently responded to as a guide to how recent they are. There ought to be a warning from the forum software or something about resurrecting dead threads!



  •  @ASheridan said:



    @blakeyrat said:
    Plus, it's relatively obscure, and I can guarantee there's entire teams of programmers writing bug-free software right now that have never heard of it.
    Obscure? Are you kidding me? Every developer I know is aware of this, and I know quite a few developers who program in quite a few different languages.

    @blakeyrat said:
    So anyway, we're talking about an operator that's entirely extraneous, that is prone to abuse, and that not every programmer is taught.
    Sorry, are you really suggesting that programmers get where they are because they're taught all they need to know? I'd be amazed to see any school teach to that level.
    I'm going to own up to what Blakey has been suggesting: Before coming to this site, I had no idea the Ternary operator existed. I've spent 3.5 years at uni, and slightly more then a year in a corporate development environment. I haven't used it since finding out because it seems kind of redundant, but I can sort of understand why it's used.

    I won't make the arguement that a complex operation using the ternary operator is bad, because it's not much better to use an if statement in that case. Better to just spread the logic over multiple lines and declare a few more local variables, and summarize it into a bool.

    But does that make me a lacking developer? No one but the OP (Long gone) has implied such to my knowlledge, and everyone agrees that it's a redundant tool. Yet I don't see many joining Blakey on pointing out the subject of this thread and calling BS.



  • @Adanine said:

    and everyone agrees that it's a redundant tool.

    Do they? I don't think it's a redundant tool. I've seen it used quite a bit in PHP for providing a default if form controls (mainly input fields) are left blank.

     

    @Adanine said:

    But does that make me a lacking developer?

    Well, yes - you're a developer lacking knowledge of a specific technique. The reason why you lack it sounds simply down to what you were taught and what you encountered. Whether or not your code quality suffered as a result of not using this technique in an environment where it was scarcely used is another question (it sounds like it didn't).

    I'll refute the assertion that "I was never taught it, I've not encountered it.. ergo nobody uses it and it must be redundant" though. That kind of closed-mindedness means you'll never accept different ways exist - which means you'll never learn new things.



  • @Cassidy said:

    Do they? I don't think it's a redundant tool. I've seen it used quite a bit in PHP for providing a default if form controls (mainly input fields) are left blank.
    Something I learned just now. From PHP 5.3 onwards you can omit the middle operand and it will default to using the value of the conditional expression, effectively acting like as an "Elvis operator" for coalescing values:
    echo $val ?: 'default';

    Anyway, TRWTF is the ternary operator being left-associative in PHP. Another reason why you shouldn't nest ternaries.

    That and having both && and || and and and or, the latter two having less precedence than the former. Complicate much?



  • @Zecc said:

    "Elvis operator"
     

    :D

     

    Side detail: JS already has this behaviour with the || operator.



  • I hadn't realized "Elvis" and "lives" are anagrams of each other. Heh.@dhromed said:

    Side detail: JS already has this behaviour with the || operator.
    PHP returns booleans as a result of boolean operators. Which is fine, if a little unexpected for people coming from C-like languages.

     



  • @Zecc said:

    booleans as a result of boolean operators
     

    Crazy talk!



  • @blakeyrat said:

    I recommend making use of a GUI which contains a safety mechanism known as a "Recycle Bin" (or "Trash Can" if you prefer.) These revolutionary ideas have been around now for over twenty years, and I think it's high time we adopted them.

    I'm sure that lowers my 1337ness in your eyes, but I'd rather be less 1337 than accidentally delete files.


    I used to think that, but the complex nature of writing rm commands (especially when directories are involved) makes it pretty difficult. It's not like you can accidentally press Shift + 8.



  • Coalescing in C# is

    val ?? altVal;

    Much better.



  • @Zecc said:

    Something I learned just now. From PHP 5.3 onwards you can omit the middle operand and it will default to using the value of the conditional expression,

    That's.. actually quite neat. I can see it looking confusing to people that haven't encountered it (but then, that's what comments are for).

    @Zecc said:

    effectively acting like as an "Elvis operator"
     

     Uh-huh.



  • @Cassidy said:

    @Adanine said:

    and everyone agrees that it's a redundant tool.

    Do they? I don't think it's a redundant tool. I've seen it used quite a bit in PHP for providing a default if form controls (mainly input fields) are left blank.

     

    @Adanine said:

    But does that make me a lacking developer?

    Well, yes - you're a developer lacking knowledge of a specific technique. The reason why you lack it sounds simply down to what you were taught and what you encountered. Whether or not your code quality suffered as a result of not using this technique in an environment where it was scarcely used is another question (it sounds like it didn't).

    I'll refute the assertion that "I was never taught it, I've not encountered it.. ergo nobody uses it and it must be redundant" though. That kind of closed-mindedness means you'll never accept different ways exist - which means you'll never learn new things.

    Sorry, redundant wasn't the right word. I don't mean it's inferior in any way, only that there is an alternative, and both options have the same capabilities (That I know of). Why bother teaching two ways to do the same action?*

    [Edit] *Sorry, that came out wrong too. By "Why bother teaching two ways..." I meant in a way that shows both usages constantly over the course content. It's likely the operator was a bullet point in one of the first lectures when we were learning if/then/else, but not shown elsewhere. There is a lot of reasons to teach two ways of one action!



  • @Adanine said:

    There is a lot of reasons to teach two ways of one action!
     

    Yeah... that was my feeling.

    I don't teach "if/then/else", I teach "conditions" and show several ways of doing it, allowing students to discuss reasons for chosing one over the other.

    I also cover some "redundant" stuff, if only to show it's no longer considered "best-practise" but that they may encounter it when maintaining legacy code for the first time (or some fuckwit contracting developer who tries to pass off an unfamiliar but outdated construct as a shining example of their competance) so at least they recognise WTFs alternative syntax.



  • @PJH said:

    @Tyler said:
    std::cout << "Good morning " << ((gender == Male) ? "Sir" : "Ma'am") << std::endl;
    I do, however have issues with things like:
    		syslog(LOG_NOTICE, "WebRelay %s:%d Relay: %s Input: %s Reboot: %s",
    			inet_ntoa(sin->sin_addr.s_addr), ntohs(sin->sin_port),         
    			relay  == 0 ? "on" : "off",                                    
    			input  == 1 ? "on" : "off",                                    
    			reboot == 0 ? "Auto Reboot off" :                              
    			reboot == 1 ? "Pinging" :                                      
    			reboot == 2 ? "Waiting for response" :                         
    			reboot == 3 ? "Rebooting" :                                    
    			reboot == 4 ? "Waiting for reboot" :                           
    				      "Unknown"                                            
    			);

    rebootIndex = (reboot < 5 ? reboot : 5 ); // or min( reboot, 5 );
    const char * const rebootStr[] = 
      { 
           "Auto Reboot off", "Pinging", "Waiting for response", 
           "Rebooting", "Waiting for reboot", "Unknown" 
      };
    

    const char * const onOffStr[] = { "off", "on" };
    syslog(LOG_NOTICE, "WebRelay %s:%d Relay: %s Input: %s Reboot: %s",
    inet_ntoa(sin->sin_addr.s_addr), ntohs(sin->sin_port),
    onOffStr[ relay==0 ], onOffStr[ input == 1 ], rebootStr[rebootIndex] );


  • Discourse touched me in a no-no place

    @Cbuttius said:

    @PJH said:

    @Tyler said:
    std::cout << "Good morning " << ((gender == Male) ? "Sir" : "Ma'am") << std::endl;
    I do, however have issues with things like:

    		syslog(LOG_NOTICE, "WebRelay %s:%d Relay: %s Input: %s Reboot: %s",
    inet_ntoa(sin->sin_addr.s_addr), ntohs(sin->sin_port),
    relay == 0 ? "on" : "off",
    input == 1 ? "on" : "off",
    reboot == 0 ? "Auto Reboot off" :
    reboot == 1 ? "Pinging" :
    reboot == 2 ? "Waiting for response" :
    reboot == 3 ? "Rebooting" :
    reboot == 4 ? "Waiting for reboot" :
    "Unknown"
    );

    rebootIndex = (reboot < 5 ? reboot : 5 ); // or min( reboot, 5 );
    const char * const rebootStr[ = 
      { 
           "Auto Reboot off", "Pinging", "Waiting for response", 
           "Rebooting", "Waiting for reboot", "Unknown" 
      };
    

    const char * const onOffStr[ = { "off", "on" };
    syslog(LOG_NOTICE, "WebRelay %s:%d Relay: %s Input: %s Reboot: %s",
    inet_ntoa(sin->sin_addr.s_addr), ntohs(sin->sin_port),
    onOffStr[ relay==0 ], onOffStr[ input == 1 ], rebootStr[rebootIndex] );

    Very nice unasked-for partial refactorisation of the code. That can crash where the former didn't.



    FWIW, the current solution for code that is currently in use (the quoted code isn't) for this is usually a const char* variable2str(int variable) function defined near some enums that mirror them (where applicable.)



  • @North Bus said:

    @Tyler said:

    @PJH said:

    @kraagenskul said:
    and I find you can guage a coder's skill based on their not using it wherever and whenever possible.
    FTFY. HTH. HAND.
     

     

    There's nothing inherently wrong with the ternary operator. Just don't be a moron and:

    a) nest it

     

    Yeah... I've recently had to deal with far too much of the following:

    <font face="Lucida Console" size="2">nresetDN = (divSel==3'b000)?((div2N[9:0]==10'h000)?1'b0:1'b1):(divSel==3'b001)?((div2N[10:0]==11'h000)?1'b0:1'b1):(divSel==3'b010)?((div2N[11:0]==12'h000)?1'b0:1'b1):(divSel==3'b011)?((div2N[13:0]==14'h0000)?1'b0:1'b1):(divSel==3'b100)?((div2N[14:0]==15'h0000)?1'b0:1'b1):(divSel==3'b101 )?((div2N[15:0]==16'h0000)?1'b0:1'b1):(divSel==3'b110 )?((div2N[19:0]==20'h00000)?1'b0:1'b1):1'b0;</font>

     Just a single case statement and/or some well-applied white space would solve so many problems. 

    I'd give the (now gone from the company) programmer applause for writing one of the most unreadable lines of code I've seen in quite a while, though.

     

     This code looks like Verilog hardware code, and a case statement might have lead to strange hardware when it was synthesised into a load of logic gates, using a dimwit logic synthesis tool.

     

     

     

     



  • Here's an attempt at properly indenting the beast:

    nresetDN = (divSel == 3'b000)
                                  ? ( (div2N[9:0]  == 10'h000)   ? 1'b0 : 1'b1 )
             : (divSel == 3'b001)
                                  ? ( (div2N[10:0] == 11'h000)   ? 1'b0 : 1'b1 )
             : (divSel == 3'b010)
                                  ? ( (div2N[11:0] == 12'h000)   ? 1'b0 : 1'b1 )
             : (divSel == 3'b011)
                                  ? ( (div2N[13:0] == 14'h0000)  ? 1'b0 : 1'b1 )
             : (divSel == 3'b100)
                                  ? ( (div2N[14:0] == 15'h0000)  ? 1'b0 : 1'b1 )
             : (divSel == 3'b101)
                                  ? ( (div2N[15:0] == 16'h0000)  ? 1'b0 : 1'b1 )
             : (divSel == 3'b110)
                                  ? ( (div2N[19:0] == 20'h00000) ? 1'b0 : 1'b1 )
             : 1'b0;
    


  • Guaging his ego:
    same function, with more line noise.
    Whilst the benefit?
    


  •  Musaran: Troll, or retarded?

     



  • @blakeyrat said:

    Whenever you have advice that sums to, "don't use it unless you're really careful," you're better off just not using it at all. If someone thinks it's ok to use it (for example, they see one you checked in), you know they'll abuse it the first chance they get.

    I've seen enough unreadable disasters stemming from this that I'm firmly in the "never use it" group.

    By that token, absolutely no programming feature should be used whatsoever, since they can _all_ be abused. It seems that most places that enforce really strict coding style tend to be lazy about doing code reviews. If your peers say "what does this mean?" or "I don't understand what you're doing here" then "clever" abuses don't make it into the code base.

    A ternary, when used in the correct situations, winds up being far more readable than the long form if/else statements, especially when doing a lot of optional argument management at the beginning of a function/method. So by that same readability rule, observant peers would probably request a much leaner list of ternary statements than the long form.



  • @Soviut said:

    @blakeyrat said:

    Whenever you have advice that sums to, "don't use it unless you're really careful," you're better off just not using it at all. If someone thinks it's ok to use it (for example, they see one you checked in), you know they'll abuse it the first chance they get.

    I've seen enough unreadable disasters stemming from this that I'm firmly in the "never use it" group.

    By that token, absolutely no programming feature should be used whatsoever, since they can _all_ be abused. It seems that most places that enforce really strict coding style tend to be lazy about doing code reviews. If your peers say "what does this mean?" or "I don't understand what you're doing here" then "clever" abuses don't make it into the code base.

    You are missing the point - the ternary operator is not needed because it can be expressed using a simple if..else statement. It is also easy to abuse and hard to maintain (because as soon as the condition needs to be made more complex the statement becomes unreadable). For these reasons it is better not to use it.

    @Soviut said:

    A ternary, when used in the correct situations, winds up being far more readable than the long form if/else statements, especially when doing a lot of optional argument management at the beginning of a function/method. So by that same readability rule, observant peers would probably request a much leaner list of ternary statements than the long form.

    I agree that a group of repeated ternary statements would be pretty clear, but I don't at all agree that it is easier to read than an if..else statement, and it is certainly not 'far more readable', unless you have problems reading if..else statements?




Log in to reply