Forum > Beginners

ways to avoid begin-end’s?

<< < (8/10) > >>

alpine:
Sorry, inappropriate quoting at my side. My comment was quite more general.

As for the literal number, I use such quite often bitwise, e.g. OutOfPaper := DeviceStatus and 1, etc.

MarkMLl:

--- Quote from: alpine on May 10, 2024, 11:54:02 am ---As for the literal number, I use such quite often bitwise, e.g. OutOfPaper := DeviceStatus and 1, etc.

--- End quote ---

I think we all do, but I'm not sure we should: that's what sets based on enumerations are for.

However that does raise an interesting point. If you declare an enumeration (of e.g. result codes) e.g. associated with class tA, the last time I investigated there was no safe way of extending that when it was e.g. subclassed to tB. OK, you can obviously declare a more detailed enumeration, but that introduces the burden of making sure that the common part of the enumerations remains in step.

MarkMLl

kupferstecher:

--- Quote from: Weiss on May 08, 2024, 10:26:09 pm ---Wouldn’t it be neat if I could say something like
--- 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 x=1 then x:=2 AND DoSomething(x)
rather than currently

--- 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 x=1 then     begin       x:=2;       doSomething(x)     end; 
--- End quote ---

Why no custom formatting?

--- 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 x = 1 then begin x:=2; DoSomething(x); end;

--- 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 x = 1then begin x:= 2; DoSomething(x); end;

--- 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 x = 1 then begin x:= 2; endelse begin x:= 0; DoSomething(x); end;

--- 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 x = 1then begin x:= 2; DoSomething(x); endelse begin x:= 0; DoSthElse(x); end;

Thaddy:
it is about avoiding begin end. As long as DoSomething returns true everyting is fine ;)

MarkMLl:

--- Quote from: MarkMLl on May 09, 2024, 08:52:29 am ---Chained assignments, automatic type conversion, confusion between logical and bitwise operations...

--- End quote ---

I hope the community will tolerate my throwing this in as a belated illustration of just /how/ problematic that sort of thing can be.


--- Code: C  [+][-]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 ((options == (__WCLONE|__WALL)) && (current->uid = 0))        retval = -EINVAL; 
That was a backdoor that person-unknown attempted to get into the Linux kernel back in 2003, as discussed at https://lwn.net/Articles/57135/

HINT: consider carefully the placement of == and = in that.

The C-derivative community's response to that has been to mandate a style where comparisons have their less-mutable expression on the left, which reduces (but does not eliminate) the risk of a rogue assignment.

Frankly, this should not be necessary in a computer language: it is an abomination.

To spell it out: the result of (options == (__WCLONE|__WALL)) is a boolean, the result of (current->uid = 0) is an integer. The && (logical-and) operator should only be compatible with booleans, not integers or a mixture of the two.

ALL automatic type conversions are a problem, which is why Wirth became far more strict post-Pascal. In addition, many cases need to distinguish between a bitwise conversion (with assumed zero padding/removal) and functional conversion (e.g. between an integer and float with significant difference in representation): Modula-2 had distinct type-transfer and VAL() operations for this.

Two recent threads provide examples of these problems: https://forum.lazarus.freepascal.org/index.php/topic,67448.msg518992.html#msg518992 (where a boolean is extended to register size under certain conditions) and https://forum.lazarus.freepascal.org/index.php/topic,67215.msg516777.html#msg516777 where part of OP's confusion was apparently caused by a shift operator applied to a constant without an explicit associated size.

MarkMLl

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version