Forum > General
Interesting compiler feature
soerensen3:
Today I discovered an interesting compiler feature. In other languages it is quite common that you can do the following with an object:
--- 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 ( obj ) then ... which is equivalent to:
--- 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 ( Assigned( obj )) then ... While not possible with standard pascal libraries you can get the feature in pascal if you overload the assignment operator:
--- 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";}};} ---operator := ( obj: TObject ): Boolean;begin Result:= Assigned( obj );end; I think it the code is still understandable but shorter. This could also be applied to other types as pointers or ints and so on. But I think there it is less understandable.
What do you think, is this bad programming practice if used locally inside a library?
ccrause:
--- Quote from: soerensen3 on November 04, 2017, 12:37:11 am ---Today I discovered an interesting compiler feature. In other languages it is quite common that you can do the following with an object:
--- 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 ( obj ) then ... which is equivalent to:
--- 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 ( Assigned( obj )) then ...
--- End quote ---
Your approach hides a boolean expression. As long as someone trying to understand your code remembers this, it is probably OK. I am not so keen on the approach, but it is a personal preference.
BeniBela:
This is brilliant!
Could add an inline modifier
bytebites:
This kind of changes has previously been rejected with laugh.
balazsszekely:
Feature request for the haters: Perhaps you manage somehow to change begin end to {}. :D
Navigation
[0] Message Index
[#] Next page