Results 1 to 10 of 10

Thread: Ternary operator, begin/end

  1. #1

    Ternary operator, begin/end

    I'm all for language-like readability but sometimes I miss

    Code:
    x>y ? {z;w} : {q;e}
    instead of

    Code:
    if x>y then begin z;w; end else begin q;e; end;
    /end of random rant/

  2. #2
    I don't.

    IIRC there's an Iff OVERLOADed function collection somewhere (sysutils?). If it isn't you can build it by your own.
    Code:
      FUNCTION Iff (CONST TheCondition: BOOLEAN; CONST Value1, Value2: REAL): REAL; OVERLOAD; INLINE;
      BEGIN
        IF TheCondition THEN RESULT := Value1 ELSE RESULT := Value2
      END;
    
      FUNCTION Iff (CONST TheCondition: BOOLEAN; CONST Value1, Value2: STRING): STRING; OVERLOAD; INLINE;
      BEGIN
        IF TheCondition THEN RESULT := Value1 ELSE RESULT := Value2
      END;
    Using VARIANT or generics would help too.

    Anyway I'm not friend of borrow stuff from other languages. I like and use Pascal and Object Pascal as they were. If you like C# or Ada stuff then use C# or Ada.
    Last edited by Ñuño Martínez; 17-05-2017 at 08:48 AM.
    No signature provided yet.

  3. #3
    Using the ternary operator for assignments is cool and I agree, I sometimes miss it in Pascal. I love how some languages support left-hand ternaries in assignments.

    Using the ternary operator as a "shorter if" is bad practice. You trade writing 6 characters for making the conditional harder to spot.

  4. #4
    I agree with Super Vegeta. Having some "language candy" comes useful when you are in a hurry to get something done with the least effort. But when you return working back to that code after several months believe me you would rather have code with bigger readability.
    For that specific matter in the past I actually ended up refactoring parts of my code from being as short as possible to being more readable.

  5. #5
    Me, I'm so stupid I always have to write extra readable code otherwise I can't go back to it even 5 minutes later.

    Ternary however, is like + or -

    For example compiler directives have alternative way, one letter 'acronyms', that is way less readable than ternary operator.
    Last edited by Thyandyr; 19-05-2017 at 12:50 PM.

  6. #6
    Don't call yourself stupid for writing readable code. Based on my opinion writing readable code is very smart. Why?
    1. Having easy readable code means you can jump in and start expanding it at any time without spending time to relearning of how that code works.
    2. Easy readable code would also give you better starting position to start working with someone else in a team since they will understand your code more easily.


    Also not being able to understand some less readable code also doesn't mean that you are stupid. it only means that you have still something to learn.

    The only time that you should call yourself stupid is when stop learning because you think you can't learn either because you know everything or because learning seems to hard to you. So keep learning.
    I myself am an educated market salesman and komercialist but I still leaned about programming on my own to the point that I would probably be able to teach others a thing or two.

  7. #7
    Yes wasn't too serious, forgot to add smileys , but I do have horrible memory. One significant aspect is that I do not code full time, it's a side aspect of my job, and often done in time-fragmented chunks.

  8. #8
    I'm a hobby programer myself. Maybe if all goes well I will become full-time programer in the future. But till then I still have to learn a bit.

  9. #9
    Those overloaded iff functions are not so unreadable at all.

    I think they are great (especially for string concatenation), especially since with later delphi and fpc you can inline them, and not suffer any (correct me) performance penalty for calling a function (stack allocations, etc..)
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  10. #10
    It depends on what you do with the parameters. I mean, the next code:

    Code:
      TheVariable := IFF (Something, TheVariable + 15, TheVariable * 2);
    generates the same executable than:
    Code:
      Tmp1 := TheVariable + 15;
      Tmp2 := TheVariable * 2;
      TheVariable := IFF (Something, Tmp1, Tmp2);
    May be the compiler can optimize it but I doubt FPC or Delphi are so smart even with -O4. In that case C's "?:" would generate faster code.
    No signature provided yet.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •