Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««23456»»

Complicated Comments Expand / Collapse
Author
Message
Posted Wednesday, February 11, 2009 2:57 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 10:16 AM
Points: 816, Visits: 1,993
Good point. But then
/*
Good comment not.
-/**/*/

does work. Does this remind you of the Perl parser?



ATB

Charles Kincaid

Post #655274
Posted Wednesday, February 11, 2009 3:05 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:18 AM
Points: 5,886, Visits: 13,041
Charles Kincaid (2/11/2009)
Let me see if I can show what is going on here and why. Lets number the lines
(00) PRINT '1' -- /* ;PRINT '2' */ ;PRINT '3' /*
(01) PRINT '4' --*/
(02) --/*
(03) PRINT '5'
(04) --*/
(05) /*
(06) PRINT '6'
(07) --/*
(08) */
(09) PRINT '7'
(10) --*/
(11) PRINT '8'

Line (00) has an inline comment starting with the double dash so 2 an 3 are in the comment
Same for (01) so 4 does print just like 1.
Line (02) has an inline comment so the block comment starter is par of the comment and is ignored. Same for (04) and (10).
Line (03) is not in any comment and gets executed. Same goes for line (11).
Line (05) starts a block comment. Lines (06) and (07) are part of the block comment.
Line (08) stops the block comment.


but line 09 is NOT executed so it is still part of the commented out code....................


---------------------------------------------------------------------

Post #655278
Posted Wednesday, February 11, 2009 3:31 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, February 4, 2011 7:20 AM
Points: 977, Visits: 1,499
Charles Kincaid (2/11/2009)
Good point. But then
/*
Good comment not.
-/**/*/

does work. Does this remind you of the Perl parser?



That's not as bad as it seems when you break it out.

/*
Good comment not.
-
/*
*/
*/

I've never had the pleasure of working with the Perl parser. Does it defy logic?


Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
Post #655299
Posted Wednesday, February 11, 2009 4:38 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 10:16 AM
Points: 816, Visits: 1,993
I wrote some stuff in Perl as that was what was available on the web server. It was not that bad to write code. You start with the inmost thing. Then surround with the next layer of processing. Then surround that with the next. Etc. Very powerful. I had very intense routines in a single line of code. Nice.

Now come back a couple of months later and need to make one little change. Trying to uncoil those lines to remember what they did. It's not so easy to read what you wrote.

Don't get me wrong here. Perl is fine. Many people use it and find it easy. I did not. It's the fault of me and not Perl.


ATB

Charles Kincaid

Post #655323
Posted Thursday, February 12, 2009 3:33 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 10, 2014 9:11 AM
Points: 1,155, Visits: 1,055
Here's an interesting combination:

This code works perfectly OK ( the unmatched block comment marker causes no problems because it is part of an inline comment):

print '1'
--/*note unmatched block comment marker on this line, following an inline comment marker
print '2'
print '3'

result: 1 2 3

Now, suppose we are asked to disable the part of the code that prints 1 and 2 ... easy, we say :) ... just put block comment markers around it ...

/*
print '1'
--/*note unmatched block comment marker on this line, following an inline comment marker
print '2'
*/
print '3'

result : Server: Msg 113, Level 15, State 1, Line 1
Missing end comment mark '*/'.

oops !


My attempt to explain the behaviour is: as soon as you are withiin a BLOCK COMMENT, the system ignores all INLINE COMMENT markers. For most statements, this doesn't matter, because the statement will not be executed anyway since it is part of the BLOCK COMMENT.

But START BLOCK COMMENT /* or END BLOCK COMMENT */ instructions which look as though they should be ignored because they follow a "--" , are actually processed, and may open a nested comment block or close the existing block or cause a syntax error.

Does that sound about right ?


Post #655492
Posted Thursday, February 12, 2009 3:46 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 8, 2013 4:59 AM
Points: 196, Visits: 79
Nice feature to get trapped on migration from SQL 2000 to 2005.
Post #655505
Posted Thursday, February 12, 2009 5:03 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, February 4, 2011 7:20 AM
Points: 977, Visits: 1,499
archie flockhart (2/12/2009)
Here's an interesting combination:

This code works perfectly OK ( the unmatched block comment marker causes no problems because it is part of an inline comment):

print '1'
--/*note unmatched block comment marker on this line, following an inline comment marker
print '2'
print '3'

result: 1 2 3

Now, suppose we are asked to disable the part of the code that prints 1 and 2 ... easy, we say :) ... just put block comment markers around it ...

/*
print '1'
--/*note unmatched block comment marker on this line, following an inline comment marker
print '2'
*/
print '3'

result : Server: Msg 113, Level 15, State 1, Line 1
Missing end comment mark '*/'.

oops !


My attempt to explain the behaviour is: as soon as you are withiin a BLOCK COMMENT, the system ignores all INLINE COMMENT markers. For most statements, this doesn't matter, because the statement will not be executed anyway since it is part of the BLOCK COMMENT.

But START BLOCK COMMENT /* or END BLOCK COMMENT */ instructions which look as though they should be ignored because they follow a "--" , are actually processed, and may open a nested comment block or close the existing block or cause a syntax error.

Does that sound about right ?



Yeah. But you gotta try this. You'll love it!

/*
print '1'
--/**/note unmatched block comment marker on this line, following an inline comment marker
print '2'
*/
print '3'



Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
Post #655550
Posted Thursday, February 12, 2009 5:19 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 10, 2014 9:11 AM
Points: 1,155, Visits: 1,055
Actually that just about makes sense, based on the assumption that "--" is ignored within block comments

so in this code:
/*
print '1'
--/**/note unmatched block comment marker on this line, following an inline comment marker
print '2'
*/
print '3'

... line 1 opens a comment block

print '1' is within the comment block so it doesn't print

on the line starting with "--" , the system ignores "--" , it then finds a complete (but empty) nested block comment ; followed by some text. The text is outside the nested comment, but INSIDE the original comment block that was started on line 1, so it does not cause an error.

print '2' is still within the original comment block

the next line ends the original comment block

and print '3' then executes
Post #655559
Posted Thursday, February 12, 2009 11:36 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, September 22, 2014 11:18 AM
Points: 305, Visits: 567
This problem originally came to mind when I started work on a tool for comparing the text from stored procedures (http://jessesql.blogspot.com/2009/02/comparing-stored-procedures-part-1.html). I wanted to allow the option of omitting all comments from the comparison, which of course would require me to identify and parse out comments from the text. In my research, I came across the question of how to treat nested comments - the "order of operation" of inline versus block comments, nested blocks, etc., and that's when I came across the scenario that formed this question of the day. I have to say that I was surprised, almost to the point of shock, that commenting functioned this way.

Anyways, glad everyone seemed to like the question (maybe not getting it wrong, but in terms of learing something surprising).


Jesse McLain
jesse@jessemclain.com
www.jessemclain.com
www.jessesql.blogspot.com
Post #655969
Posted Thursday, February 12, 2009 11:51 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, April 25, 2014 9:08 AM
Points: 1,190, Visits: 364
Jesse McLain (2/12/2009)
...
Anyways, glad everyone seemed to like the question (maybe not getting it wrong, but in terms of learing something surprising).


Thanks Jesse McLain for this question.

I surely learned something :D. But I want my developers away from writing comments like this. Comments are there to help you, not to confuse you :P.

KISS is the key -> Keep It Simple Stupid. Doing so, any dba or developer can easily understand your code.

D. Couturier
Database Administrator & Architect

Post #655984
« Prev Topic | Next Topic »

Add to briefcase «««23456»»

Permissions Expand / Collapse