#1 2022-12-27 21:08:51

tbo
Member
Registered: 2015-04-20
Posts: 224

Please avoid negation in variable names

I've already addressed this naming in another thread, and I seem to recall that you wanted to consider it more often in the future. Avoid negations in variable names. At these points in the source code I always have to stop and think twice if I have understood it correctly. And now I read:

constructor TTextWriter.CreateOwnedStream(aBuf: pointer; aBufSize: integer; NoSharedStream: boolean);
begin
  if (not NoSharedStream) and TextWriterSharedStreamSafe.TryLock then
    fStream := TextWriterSharedStream

Why not write better without rethinking:

constructor TTextWriter.CreateOwnedStream(aBuf: pointer; aBufSize: integer; UseSharedStream: boolean);
begin
  if UseSharedStream and TextWriterSharedStreamSafe.TryLock then
    fStream := TextWriterSharedStream

With best regards
Thomas

Offline

#2 2022-12-27 22:17:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,492
Website

Re: Please avoid negation in variable names

It is a biais I have indeed. I suppose it comes from the asm the compiler generates in such case.
A default value to false is shorter in asm than a default value to true.
For false, you store zero in a register, which is just a xor on self register on Intel/AMD, or you use the Zero register on ARM. wink

I guess it does not make any difference here, because this parameter is mainly for internal use.
Almost no "normal" user would change the default value

Offline

Board footer

Powered by FluxBB