Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-04

Using constants instead of comments

7 messages · 7 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I have a whole section of diagnostic writeln's in a program, and it's tedious to comment them all out/in as needed. I'm curious if doing something like

Const
   diagnosticdetail=false;
If diagnosticdetail then Writeln('diagnostic info');

Is the same as

// Writeln('diagnostic info');

And the Writeln will actually end up being left out of the compiled program altogether... since the constant will never allow it to run, or if not, is there a better way to optionally exclude diagnostic information entirely from the compiled program?

Reply by email

R0b0t1
to James Richters
James Richters wrote 13 lines
I have a whole section of diagnostic writeln's in a program, and it's tedious to comment them all out/in as needed. I'm curious if doing something like Const diagnosticdetail=false; If diagnosticdetail then Writeln('diagnostic info'); Is the same as // Writeln('diagnostic info'); And the Writeln will actually end up being left out of the compiled program altogether... since the constant will never allow it to run, or if not, is there a better way to optionally exclude diagnostic information entirely from the compiled program?

In C typically you would use conditional compilation. This would work well in FPC as well. Or, you can do what is more often done in higher level or managed languages, which is to use a log function that behaves differently based on the verbosity level.

Cheers,
R0b0t1

Reply by email

Sven Barth
to James Richters: I have a whole section of diagnostic writeln's in a program, and it's tedious to comment…

James Richters schrieb am Mi., 18. Apr.
2018, 05:00:

Show quoted text 18 lines
I have a whole section of diagnostic writeln's in a program, and it's tedious to comment them all out/in as needed. I'm curious if doing something like Const diagnosticdetail=false; If diagnosticdetail then Writeln('diagnostic info'); Is the same as // Writeln('diagnostic info'); And the Writeln will actually end up being left out of the compiled program altogether... since the constant will never allow it to run, or if not, is there a better way to optionally exclude diagnostic information entirely from the compiled program?

It should be excluded. You can confirm this by checking the assembler output generated with "-al".

Regards,
Sven

Reply by email

Giuliano Colla
to James Richters: I have a whole section of diagnostic writeln's in a program, and it's tedious to comment…

Il 18/04/2018 05:06, James Richters ha scritto:

is there a better way to optionally exclude diagnostic information entirely from the compiled program?

Did you consider to replace your Writeln with a DiagWrite (or whatever name you prefer),

and have a DiagWrite such as:

DiagWrite: Procedure(aMessage: string); inline;
begin
   if diagnosticdetail then Writeln(aMessage);
end;

This should do the job, and reduce your typing.

Giuliano

Reply by email

Vojtěch Čihák

Hi,
 
Why not define?
 
{$DEFINE DEBUG}
 
{$IFDEF DEBUG} Writeln('diagnostic info'); {$ENDIF}  
V.
 
______________________________________________________________

Show quoted text 5 lines
Od: "James Richters" Komu: "'FPC-Pascal users discussions'" Datum: 18.04.2018 05:06 Předmět: [fpc-pascal] Using constants instead of comments

I have a whole section of diagnostic writeln's in a program, and it's tedious to comment them all out/in as needed.   I'm curious if doing something like

 Const
   diagnosticdetail=false;
 If diagnosticdetail then Writeln('diagnostic info');

Is the same as

 // Writeln('diagnostic info');

And the Writeln will actually end up being left out of the compiled program altogether... since the constant will never allow it to run,  or if not, is there a better way to optionally exclude diagnostic information entirely from the compiled program?

Reply by email

Unknown author
to James Richters: I have a whole section of diagnostic writeln's in a program, and it's tedious to comment…
James Richters wrote
I have a whole section of diagnostic writeln's in a program, and it's tedious to comment them all out/in as needed. I'm curious if doing something like

what's wrong with traditional IFDEF?? use something like this...

{$IFDEF DEBUG}
writeln ('blahblahblah');
writeln (LOGFILE,'blahblahblah');
flush (LOGFILE);
{$ENDIF}

then you simply create the DEBUG define...

-DDEBUG

if you don't want it, leave it out and none of the bracketd code is even included...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list unless* *a signed and pre-paid contract is in effect with us.*

Reply by email

Mark Morgan Lloyd
to Unknown author
Show quoted text 12 lines
On 04/17/2018 11:06 PM, James Richters wrote:> I have a whole section of diagnostic writeln's in a program, and it's tedious to comment them all out/in as needed.   I'm curious if doing something like what's wrong with traditional IFDEF?? use something like this... {$IFDEF DEBUG}writeln ('blahblahblah');writeln (LOGFILE,'blahblahblah');flush (LOGFILE);{$ENDIF} then you simply create the DEBUG define... -DDEBUG if you don't want it, leave it out and none of the bracketd code is even included...

I agree. Another possibility is something like

{$if declared(customDebugWrite) }
   customDebugWrite(...);
{$endif declared()         }

so if customDebugWrite() isn't present in the program during compilation (commented out, or the entire unit containing it omitted) no attempt will be made to call it.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email