Forum > Beginners
ways to avoid begin-end’s?
Thaddy:
Well, Mark, we have = and :=...
Warfley:
C has such a feature, it's called a comma operator and it's pretty much the most disliked and least used C feature there is. Most C books don't even cover it for that reason.
Learn from the mistakes others made, just type begin end, it's not going to hurt you.
Also additionaly I'd even argue you should always use begin...end even for single statement blocks, because when you later want to change a single statement to a multi statement block (e.g. when adding logging), it can happen that you forget adding the begin end and just broke your control flow.
IMHO there should be an option to throw an error if there is no begin end. It would also fix this atrocious bug:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if condition then; // semicolon after ; closes the statementbegin Exit;end
440bx:
--- Quote from: Warfley on June 01, 2024, 05:11:31 pm ---It would also fix this atrocious bug:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if condition then; // semicolon after ; closes the statementbegin Exit;end
--- End quote ---
That "atrocious bug" is one of the features I love most about Pascal.
I very often code something like this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if <some error condition> thenbegin <error handling code>end; To test the error handling code, I force its execution by simply adding a semicolon after the "then" and recompiling. This is a very convenient way of testing error handling code.
Also, I commonly put that semicolon in an {$ifdef test_error_handler} ; {$endif} that way I can choose which error handlers get tested. Combine that with the judicious use of IsDebuggerPresent() and DebugBreak() and that semicolon activates tracing the code (which does not need to be present in the release version of the code.) Good stuff all around... best "atrocious bug" in town. :)
I completely agree with you that begin/end should be used even when the language does not require them. Makes maintenance much simpler and also makes debugging simpler and easier.
Warfley:
Why not put // ( or put it between { and } ) in front of the line? It's more visible than the ; and changes the syntax highlighting to make it immediately visible this is deactivated
The chance that you accidentally overlook a semicolon is much higher than seeing a whole line being marked as a comment.
Also you can do this if begin end was forced, just instead of only ; put begin end; in there. That's much more visible and clearly signs that this is intentional and not a mistake
If you do something out of the ordinary it should require non Ordinary clearly visible syntax
440bx:
--- Quote from: Warfley on June 01, 2024, 07:00:48 pm ---Why not put // ( or put it between { and } ) in front of the line? It's more visible than the ; and changes the syntax highlighting to make it immediately visible this is deactivated
--- End quote ---
If I use a lone semicolon it's because I just want to immediately test the code and also immediately remove the semicolon after the code is tested.
when the error handling code is for a condition that is not "uncommon" then I put it between {$ifdef test_condition} ; {$endif} which, as you suggested, makes it very visible and repeatable by the activation of a define condition.
IOW, I basically agree with you that just using a semicolon is something that should only be done when it is extremely unlikely that it is going to be inadvertently forgotten. The reason I don't use // instead is because that is a much bigger change in the code. The semicolon doesn't remove the test condition, // does. I want the test code to be as close as possible to the original code.
Navigation
[0] Message Index
[#] Next page
[*] Previous page