Sonntag, 20. Mai 2018

Formatting Sourcecode

Formatting Delphi/Pascal Sourcecode is an interesting  topic.

Take 10 developer and let them all hand-formatter some sample source-codes and you will get 10 different results.

You disagree?

Most developer has developed their own style in formatting sourcecode, because they are working alone and nobody else would see the code. Perhaps you are new to Delphi and used another programming language for many years. I can not remember what Delphiversion was this first with an included formatter CTRL+D. Perhaps before that, your are using an IDE plugin or external program.
If you are working alone - do whatever you like with your source-code, but if you have to work together with other developers in a team - it's time to think about "your" formatting.
Some developer like many empty lines and indent nearly everything or want to have a description with a Date, Name and Copyright over every method. Other developers hate empty lines and only ident one space.
Perhaps you have the "begin" at the end of line and the "end" at the first position like:
if A > B then begin
But since the IDE has Castalia or perhaps your are using a third-party-tool you got the funny colored helper-lines to show the corresponding  begin-end's, it is better that begin-end has the same ident.
Is there a right way to format your source-code? You may have the standpoint - only the default formatter-settings are the right way.
Keep in mind - if you are writing a program, you will read more sourcecode then you write or in other words writing sourcecode is not the trick, writing sourcecode that could be read easy and by other developers too, is the goal.
Of course the formatting is just the start, next topics are caps, spaces and naming. There are so many rules out in the field.
Class fields has an "F" at the beginning, parameters an "A", local var's an "L". Perhaps you like to mix your local language with English (better not). Are you rename all your visual controls?
Label1 -> lbName
Edit1 -> edName
Edit1 -> NameEdit
Do you like long var-names "Name_of_the_person" ( hope not with "_" )
So where to start?
In the next day's I will try to introduce some of this rules to a team of four very different developers. Let's see if I can crate a set of rules that everyone can live with.
But that will eventually be a topic for an other blogpost.

Donnerstag, 26. April 2018

MVVM 2.0 - I did it my way.

If you like, you can compare this with one of my older posts from 2016: MVVM - Or what I think MVVM is.  (Translated)


  • Because it’s cool and I’m a geek
  • to show – I’m better than other developers
  • so nobody else could maintain the code

no – perhaps – NO!!! Just joking!

Because we like to
  • separate forms from code
  • to get better maintainable code
  • have less-hardcoded dependencies
  • to be able to test the business logic
  • to test the workflow

Sure? Are you writing tests? If not – stop reading…
But perhaps you like to develop code you can use again in other applications…
Ask 10 developers to explain MVVM – at first; you will get a picture from, or, then everybody tells you: “This is the pattern and “so” it has to be implemented”
OT: Like many other patterns… You have to follow the rules of this 4 guys and the book from 1994! More than 500.000 copies of the book have been sold – not so bad at all…
Back to MVVM - If you ask for the details, you will get 10 ideas how to implement it.
But… We are Delphi developer – why should we try to implement things as Microsoft did in .net? Because this is the right way?

Let us dive into:
The core elements are View, Viewmodel, and Model. At the beginning, you could trade a TForm as the View, but this is not the same. A TFrom could be the container for many views at the same time. For the moment, we say TForm = View – the key things of MVVM are the bindings or better, the communication from Viewmodel to View and back. (And perhaps to the Model)
If we follow the rules – the view should have no logic, the Viewmodel is responsible for handling the view-logic and converting the data to the View and the Model contains the data. (hope this is right) I never took this approach.
Our view is not a stupid xml-only-description of visual controls. Our controls always have their own logic, we have styles and animations, able to do onMouseover/down/up things. Trigger doing fancy stuff.
The Model is dealing with the data and the Database, too? I don’t think so. What is a database?
Neither my ViewModel nor my Model know what a database is. The Models gets an interface to store or load data – without knowing where it ends.
How does the communication work?
The Model changes some data and now the ViewModel wants to inform the View or perhaps all Views, about this change.
At first, we need a Multicast event to inform more than one View about the new data. So every view has to sign in for the event. Now the Viewmodel could send a PropertyChanged Event like:

Every view – that is able to show the change, gets the Event and could ask the ViewModels Property PersonName.


PersonNameProperty is defined as:
Const PersonNameProperty : String = 'PersonName';

Use Consts and no magic String so we always have the right typo. OK…That is good, but:

In the View we end up in a

procedure PropertyChanged ( Const APropertyChanged : String);

Comparing with many If then else constructions (first bad thing) and because we have a const in the Viewmodel, the reference is not the same so the string-compare must compare all chars. (second bad thing).
If we have a huge view (yes we could perhaps split it) we and up with a too long comparing procedure.

Since Windows 3.1 - in the early days – Messages are send with the content or at least with a pointer to the content.
So why are we just sending change “hints”? This is like sending a SMS – I have news call be back, instead of “I will be late, arriving at 8pm”.

Sending Strings is good for testing. Eg: A property change of PersonName := 'NewName'; should fire 'PersonName' – I tried Const ID’s  like Const idPersonName : Integer = 42; Not so good for testing but you can use a case at the View.

I don’t like to repeat on every Property:
  if FPersonName <> AValue then
      FPersonName := AValue;
Same in every setter.

Then to the View:
if APropertyChanged = TPersonViewModel.PersonNameProperty then
  PersonName.Text := FViewModel.PersonName;

I first implement the MVVM Pattern the MS-way, but if you think – “To much writing” or “I did not test my code” – you are right and of course, debugging is not so easy, too…

In fact, development time takes a bit longer. This extratime cut’s down my Test-writing-Time… (Bad thing three), because I love TDD.

It is faster if you could use the same Model or perhaps the Viewmodel in another project, but that is another story.

These problems lead me to “my way” MVVM 2.0…

We have attributes and the RTTI!

I think: The best way to use a pattern is if the pattern is not so far from you normal workflow.

I like to design my Forms and so my Views as Forms, too – Frames are bad and often leads to problems with the IDE. So SubViews are Forms with a TLayout-Container that parent is mapped to the target-parent at runtime.

My new workflow is:
  1. Create a Form/View
  2. Change Class(TFrom) to Class (TMVVMForm) / or Frame for SubViews
  3. Put attributes at FormControls like [ViewModelLink] PersonName : TEdit;
  4. Create Procedures with attributes like
    Procedure PersonNameChanged(Const AValue : String);
  5. Register the Form at the ViewLocator with the necessary ViewModel
  6. That’s it.

In your NavigationService you could get the View from the ViewLocator for a given ViewModel and an optional Name. On Creation the View connects all bindungs and propertyChanges.
You like to change Names? All attributes take optional name parameters. For:
Edit1: TEdit; // Better rename this!
Now to the ViewModel:
  1. Create a class TPersonViewModel = Class(TRootViewModel)
    I don’t like ViewModelBase as Name – it sound like a database for ViewModels…
    All my DBClasses ends with Base – PersonBase not DBPerson!
  2. Define your private Fields as
    FPersonName : autochange<string> // FPersonName : String
  3. Property PersonName : String : read GetPersonName write SetPersonName;
  4. Procedure SetPersonName(Const AValue : String);
    begin // Auto-PropertyChanged if different.
      FPersonName.Value := AValue;
  5. That’s it.
Most of the stupid code writing is not necessary anymore and done in the background over the RTTI.
Of course, this is only a small part of this pattern, but now I can point my focus on the more advanced parts.
You like this approach? – Please leave a comment – if not…;-)

Mittwoch, 14. März 2018

Neural Network

Neural Network or Neuronal Network... Whatever...

Since many year's I want to test this kind of programming... But never had time for this.

You can find many videos on YouTube, but "all" are full of math or full of the wrong programming language. ( or both )

You can find some source files, but what is the minimum of LOC's you need?

For now: Less the 300 LOC in Delphi for a working network.

At the moment I have many ideas what I can do with this... But perhaps first dig into the next step:

Genetic algorithm.

I will include this in the FDK, if ready...

Best video I found: (C#)
This, I take to just live code this - with many modifications - in Delphi - a little bit of debugging and it works. ( not much longer than it takes to look the video)

Dienstag, 20. Februar 2018

FDK XE8 - 10.2


I've already find the time to compile my FDK for XE8 up to 10.2.2!

I finally found the F2018 error so XE8 and Seattle is working again.

So starting at this point I'll prepare the next update.