Forum > General

Syntax proposals

(1/10) > >>

damieiro:
Hi!

I'm newbie on this forum. So, sorry if not the correct place to put here the message
Where it can be posted syntax proposals?

Example:

--- 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";}};} ---{actual sintax without boilerplatecoding}Type   TMyclass=class;Var  MyClass:TMyClass; begin  MyClass:=TMyClass.Create; {constructor}  {do things}  FreeAndNil (TMyClass);end; 
Proposed extended sintax:


--- 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";}};} ---{actual sintax without boilerplatecoding}Type   TMyclass=class;Var  MyClass:TMyClass; begin  MyClass.Create;   {if Myclass is nil then call the constructor fully   and put it on MyClass ,   if not call only the method MyClass.create as usual}   MyClass.Free; end; 
Proposed sintax 2:


--- 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";}};} ---{actual sintax without boilerplatecoding}Type   TMyclass=class;Var  MyClass: new TMyClass; {already made getmen for this in heap, not stack, so it points to a valid mem location} begin  MyClass.Create; {would work, beause it's not nil}  MyClass.Free; end; 

Thaddy:
That all already exist. The FreeAndNil is a habit of some programmers to hide their own bugs, but generally many - if not all - expert programmers would advice against its use except under special circumstances.
Calling just free is alright - even better - in all but the borderline cases if the rest of your code is solid and well written.
Using FreeAndNil all over the place is in mnsho the hallmark of a programmer that doesn't know what his code is doing.... < expect some flames by those type of programmers: they should be ignored.
FreeAndNil can hide REAL bugs....>
If you are a beginner, just call free, if your code subsequently crashes, you made a mistake.

The second proposal is wrong altogether it works but has no reference so you can not actually use it because you don't know where it is?
It works in combination with the with statement, though....

--- 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";}};} ---{$mode objfpc}begin  with TObject.Create do  try    writeln(Classname);  finally    free;  end;  end.code]

Martin_fr:

--- Quote from: damieiro on December 18, 2018, 03:31:07 pm ---

--- 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";}};} ---  MyClass.Create;   {if Myclass is nil then call the constructor fully  
--- End quote ---

That has several problems.

1) It introduces huge potential for errors.

If MyClass is a local var, then it is not initialized and has a random value. So to fulfil the "is nil then call the constructor fully" condition you must write

--- 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";}};} ---  MyClass := nil;  MyClass.Create; And there goes any gain you may have wanted.

Also if reading the code, the reader does often not know if   MyClass would be nil or not. So it would be unclear what it means.

2)

--- 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";}};} ---  MyClass.Create; Already has a meaning. Changing it will/may break existing code.

It calls the constructor "Create" as a regular function.
And even if "MyClass" is nil, this is allowed.

This is valid code

--- 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";}};} ---TMyClass(nil).Createassuming TMyClass.Create is written to deal with this.

garlar27:

--- Quote from: Thaddy on December 18, 2018, 03:38:49 pm ---[...]
FreeAndNil can hide REAL bugs....>
[...]

--- End quote ---
Never heard of that  :o
Can you explain how it hide some bugs?

Handoko:
I am not an expert in programming. But I almost never use FreeAndNil, and all my programs works correctly. I know on very rare cases only, FreeAndNil is needed.

Navigation

[0] Message Index

[#] Next page

Go to full version