Search This Blog

Tuesday, July 11, 2023

Anonymouse methods in Delphi

In Delphi, the term "anonymous methods" refers to a programming feature that allows you to define and use inline functions without explicitly declaring them as separate named functions or procedures. Anonymous methods are also known as anonymous functions or lambda expressions.

Anonymous methods provide a convenient way to write encapsulated blocks of code that can be passed as parameters to other routines or assigned to variables. They are commonly used in event handling, multithreading, and callback scenarios.

Here's an example of how anonymous methods can be used in Delphi:


program AnonymousMethodsDemo;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TMathOperation = reference to function(a, b: Integer): Integer;

function PerformMathOperation(a, b: Integer; mathOp: TMathOperation): Integer;
begin
  Result := mathOp(a, b);
end;

procedure ExecuteAnonymousMethodDemo;
var
  addResult: Integer;
  subtractResult: Integer;
begin
  // Example: Performing addition using an anonymous method
  addResult := PerformMathOperation(10, 5,
    function(a, b: Integer): Integer
    begin
      Result := a + b;
    end
  );

  // Example: Performing subtraction using an anonymous method
  subtractResult := PerformMathOperation(10, 5,
    function(a, b: Integer): Integer
    begin
      Result := a - b;
    end
  );

  WriteLn('Addition Result: ', addResult);
  WriteLn('Subtraction Result: ', subtractResult);
end;

begin
  try
    ExecuteAnonymousMethodDemo;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  ReadLn;
end.

In the above example, we define a TMathOperation type which represents an anonymous method that takes two integers as parameters and returns an integer. The PerformMathOperation function accepts two integers and a TMathOperation instance and executes the specified math operation.

The ExecuteAnonymousMethodDemo procedure demonstrates two examples of using anonymous methods. It performs addition and subtraction operations by passing the required logic as an anonymous method to the PerformMathOperation function.

When executed, the program outputs the results of the addition and subtraction operations using anonymous methods.

Note that Delphi doesn't have native support for anonymous functions, but it supports anonymous methods through the reference to syntax as shown in the code.

Monday, July 10, 2023

Garbage collector in Delphi?

The short answer is no!

But...

Before talking about garbage collection we need to talk about another term in programming languages like C that is called a smart pointer, so what is a smart pointer?

SMART POINTER

In software programming languages like C or Delphi, a smart pointer is a programming construct or class that acts as a wrapper around a regular pointer. It provides additional functionality and automates memory management to help prevent common issues such as memory leaks, dangling pointers, and accessing deallocated memory.

The primary purpose of a smart pointer is to ensure proper memory deallocation and ownership semantics. When you allocate memory dynamically using functions like malloc() in C or New() in Delphi, it's important to release the memory when it's no longer needed to avoid memory leaks. Smart pointers handle this automatically by deallocating the memory when the smart pointer goes out of scope or is explicitly released.

Smart pointers typically use reference counting or garbage collection techniques to manage memory. Reference counting keeps track of how many references point to a particular memory location, and when the reference count reaches zero, the memory is deallocated. Garbage collection involves periodically identifying and reclaiming memory that is no longer accessible.

There are different types of smart pointers available, such as unique_ptr, shared_ptr, and weak_ptr (in C++), or interfaces like IInterface and records (in Delphi).
Each type has its own set of features and ownership semantics, allowing programmers to choose the appropriate smart pointer based on their specific requirements.

By using smart pointers, programmers can reduce manual memory management errors and make their code more robust, reliable, and less prone to memory-related bugs.

Smart pointer implementation Sample


In Delphi, the IInterface serves as the base for implementing smart pointers. You can define your own custom interface that extends IInterface and includes additional methods specific to your smart pointer implementation.

Here's a simple example of a custom smart pointer implementation using reference counting:

type
  TMySmartPtr = class(TInterfacedObject)
  private
    FData: TObject;
  public
    constructor Create(AData: TObject);
    destructor Destroy; override;
    function GetData: TObject;
  end;

constructor TMySmartPtr.Create(AData: TObject);
begin
  inherited Create;
  FData := AData;
end;

destructor TMySmartPtr.Destroy;
begin
  FData.Free; // Release the owned object
  FData := nil;
  inherited Destroy;
end;

function TMySmartPtr.GetData: TObject;
begin
  Result := FData;
end;


In this example, TMySmartPtr is a custom smart pointer class that manages the lifetime of an owned object (FData). The object is freed when the smart pointer goes out of scope or is explicitly destroyed.

You can use this smart pointer as follows:

var
  MyPtr: TMySmartPtr;
begin
  MyPtr := TMySmartPtr.Create(TObject.Create);
  // Use the smart pointer and its underlying object
  // ...
  // no need to free the created Object!
  // Smart pointer goes out of scope, object is automatically freed
end;


In this code snippet, an instance of TMySmartPtr is created and assigned a newly created TObject. When MyPtr goes out of scope or is set to nil, the smart pointer's destructor is called, which in turn frees the owned object.

Delphi also provides interfaces like IInterfaceList and TContainedObject that offer more advanced smart pointer capabilities, such as managing ownership hierarchies or implementing weak references.

Remember that this is a simplified example, and in real-world scenarios, you may need to consider additional factors like thread safety, circular references, and other edge cases. It's always recommended to thoroughly test and review your smart pointer implementation to ensure correctness and efficiency.

Is this sample the best approach in this regard?
Probably NO!😆

So what? 

Here is my smart pointer repository that collected 7 different versions of smart pointers implementations, please have a look and find the best approach based on your taste!

Please have a look at real-world samples here:

Thursday, July 6, 2023

Why hirinng managerrs love this 6-lettr word?!!!

 

Notice something weird about the subject line?


Of course, you did.

There are typos in it.

And typos catch your attention.

Now, for the subject line of an email or a blog post, this works fine, because it got you to open it.

But tiny mistakes in your resume? They do the exact opposite.

Here's why:

On average, 118 people apply to each job, which means that even if a lot of resumes don’t make it past the ATS (Applicant Tracking System), recruiters have a huge “virtual stack” of resumes to go through.

So what is the stage 1 of the hiring process?

Cut down the field!

Meaning, when a recruiter clicks on your resume, they’re looking for literally ANY excuse to use their favorite 6-letter word - DELETE - and move on to the next one.

Small typo?

Delete.

Wrote ‘managing’ when it should’ve been ‘managed’?

Delete.

Are dates not properly formatted?

Delete.

29 candidates with less than a 50% match in the ATS for the job description keywords?

Select All, DELETE.

It’s like choosing a movie on Netflix.

With so many options, the tiniest thing you don’t like makes you skip to the next one.

And even once the field’s been whittled down, remember: hiring is risky business. With the average cost per hire around $4,700, thousands of $$$ hang in the balance.

Do you really think an employer’s going to take a chance on someone who - in their eyes - didn’t take the time to properly edit their resume?

Nope.

That’s why if you’re going to run the gauntlet of job-hunting…

Don’t shoot yourself in the foot by letting the small mistakes get overlooked on your resume.

Before you send it out, I'd recommend you put yourself in the shoes of a hiring manager and go through it with a fine-toothed comb. 

Do not give employers an excuse to press ‘Delete’.

How to fix it?

If you want to find and fix issues in your resume try to learn about it online or use resume optimization websites like Resume Worded or Kickresume or any other website like that.

ADA and it's grandfather Pascal!

Flashback

Pascal is a procedural programming language that was developed by Niklaus Wirth in the late 1960s and early 1970s. The language was named after French mathematician and philosopher, Blaise Pascal. Initially designed as a language for teaching programming, Pascal quickly gained popularity among programmers due to its simplicity and readability.

The early versions of Pascal were implemented on mainframe computers, but later it was also available for microcomputers. The language was widely used in academic settings and was popular among hobbyists who had access to personal computers. In the 1980s, a version of Pascal called Turbo Pascal was released by Borland International, which became very popular due to its fast compiling times and compatibility with IBM PC-compatible computers.

In the early 1990s, Borland released Delphi, an object-oriented version of Pascal that included a visual development environment and a powerful class library. Delphi was designed to be an easy-to-use, rapid application development tool for Windows, and it quickly gained a large following among Windows developers. Its popularity eventually led to the creation of the open-source Lazarus project, which aims to recreate the Delphi IDE and class library using free and open-source software.

The philosophy behind the creation of the Ada programming language was quite different from that of Pascal. Ada was specifically designed for large-scale, safety-critical systems, such as those used in the military and aerospace industries. The language was created by a team of experts from government, industry, and academia, who collaborated to create a language that would be safe, reliable, and scalable.

One of the key design principles of Ada was its support for modularity and abstraction. The language includes features such as packages, generics, and tasking, which allow code to be organized into reusable units and executed concurrently. This makes it well-suited for complex systems that require high levels of reliability and maintainability.

While there are some similarities between Delphi and Ada, such as their support for object-oriented programming and their use of Pascal-like syntax, the two languages have different goals and design principles. Delphi is focused on rapid application development for Windows, while Ada is focused on safety-critical systems. However, both languages demonstrate the versatility of the Pascal language family and its ability to adapt to a variety of different programming paradigms and application domains, 

Which one is influenced by the other?

Ada was influenced by Pascal.

Ok, it's Enough! Show me the code!

let's compare the syntax of ADA and Delphi with some sample code!


// Sample Pseudocode
if (x > y) then
    z := x + y;
else
    z := x - y;
endif;

In ADA, the equivalent code would look like this:

-- ADA Code
if x > y then
    z := x + y;
else
    z := x - y;
end if;

In Delphi, the equivalent code would look like this:


// Delphi Code
if x > y then
    z := x + y
else
    z := x - y;
As you can see, there are some syntactical differences between the three languages:
In ADA, statements are terminated with semicolons (;),
while in Delphi they are terminated with optional semicolons.
ADA uses the end if keyword to terminate conditional statements,
while Delphi uses the end keyword.
In Delphi, the then keyword is followed by a new line instead of a space.

Overall, ADA and Delphi have similar syntax structures for
their control flow statements, but there are some minor differences
in how they handle keywords and semicolons.

Here are some more complex examples of code in both ADA and Delphi along with
an explanation of the differences in their syntax:

Example 1: Looping through an array


// Sample Pseudocode
for i = 0 to len(array)-1 do
    print(array[i])
end for 

In ADA, the equivalent code would look like this:


-- ADA Code
for i in 0..array'Length-1 loop
    Put_Line(Integer'Image(array(i)));
end loop;

In Delphi, the equivalent code would look like this:


// Delphi Code
for i := 0 to Length(array)-1 do
    WriteLn(array[i]);
As you can see, there are several differences between the two languages
in this example:In ADA, the for loop uses the in keyword to define
the range of the loop. ADA uses the Put_Line procedure to print output to the console,
while Delphi uses the WriteLn function. In ADA, the array'Length syntax is used to get the length of the array,
while in Delphi, the Length function is used.

Example 2: Creating a function with parameters

Here's some pseudocode to create a function that takes in two integers
and returns their sum:

// Sample Pseudocode
function addNumbers(num1, num2) {
    return num1 + num2;
}

In ADA, the equivalent code would look like this:

-- ADA Code
function Add_Numbers (Num1 : Integer; Num2 : Integer) return Integer is
begin
    return Num1 + Num2;
end Add_Numbers;
In Delphi, the equivalent code would look like this:

// Delphi Code
function Add_Numbers(num1, num2: Integer): Integer;
begin
    Result := num1 + num2;
end;
In this example, there are a few differences between the ADA and Delphi syntax:
In ADA, the function is defined using the function keyword and the return type is
specified after the parameter list. In Delphi, the return type is specified before
the function name. In ADA, the return keyword is used to return a value from a function,
while in Delphi, the Result variable is used.
Both languages use semicolons to terminate statements, but in ADA they are required. Overall, both ADA and Delphi have similar syntax structures for creating functions
with parameters, but they have some minor differences in how they handle keywords,
semicolons, and return values.

Example 3: tasking in ADA and modern Delphi.

In ADA, tasking is an essential feature for concurrent programming.
Here's an example that demonstrates the use of tasks in ADA:

-- ADA Code
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Task_Example is
   task type Worker_Task is
      entry Set_Value (New_Value : Integer);
      entry Get_Value (Result : out Integer);
   end Worker_Task;

   task body Worker_Task is
      My_Value : Integer := 0;
   begin
      loop
         select
            when Set_Value'Event =>
               accept Set_Value (New_Value : Integer) do
                  My_Value := New_Value;
               end Set_Value;
               
            when Get_Value'Event =>
               accept Get_Value (Result : out Integer) do
                  Result := My_Value;
               end Get_Value;
         end select;
      end loop;
   end Worker_Task;

   W1 : Worker_Task;
   W2 : Worker_Task;
begin
   W1.Set_Value(10);
   W2.Set_Value(20);
   
   Put("W1's value is: ");
   W1.Get_Value(Result => Integer'Image(Result));
   Put_Line(Result);
   
   Put("W2's value is: ");
   W2.Get_Value(Result => Integer'Image(Result));
   Put_Line(Result);
end Task_Example;
In this example, we define a task type called Worker_Task that has two entries:
Set_Value and Get_Value.
The Set_Value procedure sets the value of a private integer variable called
My_Value, while the Get_Value procedure returns the current value of My_Value.
The task then enters a loop where it accepts incoming requests to either
set or get the value of My_Value. Two instances of the Worker_Task task type are then created (W1 and W2),
and their Set_Value entries are called to set their respective values.
Finally, the Get_Value entries are called for both tasks to retrieve
their current values.
In modern Delphi (version XE7 and later), tasking is implemented using the
TTask class.

Here's an example of how tasks can be used in Delphi:

program Task_Example;
// Delphi Code
{$APPTYPE CONSOLE}

uses
  System.Threading,
  System.SysUtils;

procedure Worker_Task(Value: Integer; var ResultVar: Integer);
begin
  // Perform some work here...
  
  ResultVar := Value * 2;
end;

var
  T1, T2: ITask;
  Result1, Result2: Integer;
begin
  T1 := TTask.Create(
    procedure
    begin
      Worker_Task(10, Result1);
    end
  );
  
  T2 := TTask.Create(
    procedure
    begin
      Worker_Task(20, Result2);
    end
  );
  
  T1.Start;
  T2.Start;
  
  T1.Wait;
  T2.Wait;
  
  Writeln('Result1 = ', Result1);
  Writeln('Result2 = ', Result2);
  
  Readln;
end.
In this example, we define a procedure called Worker_Task that performs some work
and sets the value of a passed-in variable called ResultVar.
Two instances of the ITask interface are then created (T1 and T2) using the TTask.
Create a constructor and pass in anonymous procedures that call Worker_Task
with different values. The tasks are started using the Start method and then waited for using the
Wait method.
Finally, the results of both tasks are printed to the console.

Is it worth learning and investing in ADA programming language in 2023?

(Answered by ChatGPT)
Is there any job for that out there?

In 2023, Ada programming language is still widely used in industries
such as defense, aerospace, transportation, and healthcare, where safety
and reliability are critical concerns.
Therefore, learning and investing in the Ada programming language can be valuable
if you are interested in pursuing a career in these industries. There are several job opportunities available for Ada programmers,
particularly in the defense and aerospace sectors.
Job titles may include software engineer, systems engineer,
embedded software developer, and safety-critical software developer.
However, it's worth noting that while Ada remains relevant in certain industries,
its popularity has waned in recent years with the rise of newer languages
like Rust, Swift, and Kotlin. Therefore, before investing your time and effort
in learning Ada, it's worth considering the demand for Ada programmers in your
target industry and assessing whether learning other languages might offer
better job prospects.

Is it worth learning Delphi programming language in 2023?

(Answered by ChatGPT)
Is there any job for that out there?

Delphi is a programming language that has been around for many years
and has been widely used in various industries, especially for developing
desktop applications.
However, its popularity has decreased over time, and it's not as widely used
today as it once was.
That being said, there are still some job opportunities available for Delphi 
developers, particularly in industries where legacy systems are still 
running on Delphi codebases. 
For instance, some companies might need to maintain an existing application
 or upgrade it, which requires knowledge of the Delphi language. 
Additionally, some government agencies and defense contractors still use Delphi
for developing certain applications.
So, while Delphi may not be as popular as other languages like Java, Python,
or JavaScript, there are still opportunities for Delphi developers.
However, if you're new to programming and looking to learn a new language,
you might want to consider learning one of the more in-demand languages
to increase your job prospects.