IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Prism
Search Prism Group:

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi PrismLanguage Basics   Print This     
  From the December 2015 Issue of Prestwood eMag
 
Prism Language Basics:
Delphi Prism Code Blocks (begin..end)
 
Posted 16 years ago on 11/25/2008 and updated 5/8/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB101645

Languages Focus: Code Blocks

The rules for code blocks within a language dictate where you can declare variables, how you "bracket" code, etc.

Delphi Prism Code Blocks

Same as in Delphi for Win32 but Prism also supports inline variable declaration.

Syntax Example:
function DoSomething : integer;
var
 a, b : integer;
begin
 a := 1;
 b := 2;
 var c : integer; //Prism allows inline (local) variables.
 c := a + b;
  
 result := c;
end;

Delphi for .Net Code Blocks Example

The following is a Delphi for .Net example using VCL.Net.

Add SysUtils to your uses clause.

uses
  Borland.Vcl.SysUtils;

Here is the code for the function and click event:

function Add(a, b : Integer) : integer;
begin
  result := a + b;
end;
 
procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
begin
 MessageBox.Show(IntToStr(Add(3, 4)));
end;

Notice the function has to come before it's first usage. Alternatively, you can move the function below it's first usage if you prototype the function in the interface section:

interface
  function Add(a, b : Integer) : integer;

More Info


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2

I personally don't like these inline declarations.  Why have them?  They just goof up the readability of the routine.  Now, if I look at the declaration of local variables, I don't know how many more might be defined later.  So I don't have a good idea of the variable "context" before starting to read the logic (the code).

I've used languages that allowed such things LONG ago when I used PL/I.  And they didn't bother me then.  After many years of Pascal, I now understand the benefits of declaration separate from code.

I assume the main reason that someone would want the inline declaration is because it is too much of a bother to scroll back up to the declaration section to define it and then have to scroll back.

Maybe it would be better if the editor just had a command that pushed the declarations back up to the top.

On the other hand, again from PL/I, if the inline declaration has a different scope, that is something else again.  So if a new, nested begin end block contained an inline variable declaration, I suppose I could understand its use.  But I would rather see the editor pull such bits of code up to a local procedure.  Again for readability.

And if the inline variable and local block scope were being used to get some sort of performance advantage by not having to make a local call, I would rather see the optimizing compiler figure that out.

Readability is what makes me stay with Pascal.

Posted 15 years ago

Comment 2 of 2

I agree! I think it was added because other languages allow it. In particular C#. Kind of a catch up mentality. However, I agree with you, I think code is cleaner when variables are not inline.

Posted 15 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P144A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101645 Counter
15924
Since 11/25/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]