Using constants instead of comments

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Using constants instead of comments

Zaaphod
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?


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants instead of comments

R0b0t1
On Tue, Apr 17, 2018 at 10:06 PM, James Richters
<[hidden email]> 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
>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants instead of comments

Free Pascal - General mailing list
In reply to this post by Zaaphod
James Richters <[hidden email]> schrieb am Mi., 18. Apr. 2018, 05:00:
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 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants instead of comments

Giuliano Colla
In reply to this post by Zaaphod
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants instead of comments

vojtech.cihak
In reply to this post by Zaaphod

Hi,

 

Why not define?

 

{$DEFINE DEBUG}

 

{$IFDEF DEBUG} Writeln('diagnostic info'); {$ENDIF}

 

V.

 

______________________________________________________________
> Od: "James Richters" <[hidden email]>
> Komu: "'FPC-Pascal users discussions'" <[hidden email]>
> 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?


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants instead of comments

wkitty42
In reply to this post by Zaaphod
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...


--
  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.*
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Using constants instead of comments

Mark Morgan Lloyd-5
On 18/04/18 12:15, [hidden email] wrote:

> 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]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal