PDA

View Full Version : Ternary operator, begin/end



Thyandyr
16-05-2017, 11:35 PM
I'm all for language-like readability but sometimes I miss



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


instead of



if x>y then begin z;w; end else begin q;e; end;


/end of random rant/

Ñuño Martínez
17-05-2017, 08:36 AM
I don't.

IIRC there's an Iff OVERLOADed function collection somewhere (sysutils?). If it isn't you can build it by your own.


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.

Super Vegeta
18-05-2017, 06:37 PM
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.

SilverWarior
18-05-2017, 08:02 PM
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.

Thyandyr
18-05-2017, 09:52 PM
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.

SilverWarior
19-05-2017, 01:53 PM
Don't call yourself stupid for writing readable code. Based on my opinion writing readable code is very smart. Why?

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.
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.

Thyandyr
19-05-2017, 02:58 PM
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.

SilverWarior
20-05-2017, 03:53 AM
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.

JernejL
10-03-2018, 09:48 AM
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..)

Ñuño Martínez
12-03-2018, 05:08 PM
It depends on what you do with the parameters. I mean, the next code:



TheVariable := IFF (Something, TheVariable + 15, TheVariable * 2);

generates the same executable than:


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.