Recent

Author Topic: Interesting USES block sorting behaviour with JEDI Code Format  (Read 1322 times)

Aistis

  • New Member
  • *
  • Posts: 13
    • Titbits of mind leakage
Interesting USES block sorting behaviour with JEDI Code Format
« on: February 09, 2024, 09:34:21 am »
I don't know if it's a bug or if I have mis-configured something, so I'm hoping you guys will help me out with my issue.

I constantly use JEDI Code formatting and have no qualms with it. Apart from an interesting behaviour when it comes to uses unit sorting.

As an example, my Windows machine sorts these units like this:
Code: Pascal  [Select][+][-]
  1. uses SysUtils, StdCtrls;
while my Linux machine sorts it like this:
Code: Pascal  [Select][+][-]
  1. uses StdCtrls, SysUtils;


On both machines I have configured English to be the computer's display language while using Lithuanian for regional settings (dates, currency, etc.). It seems that it uses the English alphabet for sorting on Linux (as one would expect) with Y going after T, while on Windows it uses (I assume) the Lithuanian alphabet, where Y comes before T.

The thing is, my whole Windows machine, be it user locale or system wide locale is set to English, yet it still seems to sort by the Lithuanian alphabet. And the reverse is true on Linux as shown above. It's not a huge problem, but it's still an annoyance as I'm running code formatting constantly, and it keeps showing up in git diff.

Obviously, I can turn off sorting completely, but I'd rather have it on and sort alphabetically (preferably in English) if possible.
« Last Edit: February 09, 2024, 09:56:42 am by Aistis »

Thaddy

  • Hero Member
  • *****
  • Posts: 14625
  • Sensorship about opinions does not belong here.
Re: Interesting USES block sorting behaviour with JEDI Code Format
« Reply #1 on: February 09, 2024, 09:39:25 am »
Any code beautifier should NEVER sort the uses clause differently from what the programmer defined, because unit order can be extremely important, e.g. mem managers and the like. If it sorts, it is a bug and not a small one..
Another reason is that some structures are defined differently in the rtl and e.g. the Windows unit and then the order is also important for which is chosen by the compiler. Or what if on unix cthreads is sorted away?

So turn sorting off, even if you don't like it.......
Sorting should be removed from any code beautifier that "supports" it!

It is neigh impossible for a beautifier to analyse every single unit's code to determine the order of the units in the units clause to the extend that the order is proper for the program or library or unit at hand. Even the compiler can not do that.

To paraphrase Asterix and the Helvatians: 7 lashes with a baguette for the idiot that implements that. :o
« Last Edit: February 09, 2024, 10:06:04 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Aistis

  • New Member
  • *
  • Posts: 13
    • Titbits of mind leakage
Re: Interesting USES block sorting behaviour with JEDI Code Format
« Reply #2 on: February 09, 2024, 10:02:23 am »
Any code beautifier should NEVER sort the uses clause differently from what the programmer defined, because unit order can be extremely important, e.g. mem managers and the like. If it sorts, it is a bug and not a small one..
Another reason is that some structures are defined differently in the rtl and e.g. the Windows unit and then the order is also important for which is chosen by the compiler.

So turn sorting off, even if you don't like it.......
Sorting should be removed from any code beautifier that "supports" it!

It is neigh impossible for a beautifier to analyse every single unit's code to determine the order to the extend that the order is proper for the program or library or unit at hand.

What you're saying does make sense. However, I believe uses sorting was on by default with both my Lazarus installs (or I might be mis-remembering). Whatever the case might be, if the order matters so much I wonder why the JEDI code formatting comes with such a function to begin with.
« Last Edit: February 09, 2024, 11:55:38 am by Aistis »

Thaddy

  • Hero Member
  • *****
  • Posts: 14625
  • Sensorship about opinions does not belong here.
Re: Interesting USES block sorting behaviour with JEDI Code Format
« Reply #3 on: February 09, 2024, 10:08:56 am »
Sheer Incompetence? Leave out the question mark.
Sorting, both on program level and unit level, can and will eventually break your code. Period.

Reported as issue 40756 on the Lazarus bug tracker with the request to entirely remove it. Well, at least not present it in the dialog may do...
« Last Edit: February 09, 2024, 10:48:18 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

DomingoGP

  • Jr. Member
  • **
  • Posts: 62
Re: Interesting USES block sorting behaviour with JEDI Code Format
« Reply #4 on: February 10, 2024, 12:18:04 am »
As Thaddy has explained very well, these are potentially dangerous options  >:D so I have added a warning in the configuration dialog in trunk.

Obviously, I can turn off sorting completely, but I'd rather have it on and sort alphabetically (preferably in English) if possible.
I have also changed the code so that it does not take into account the "locale" when sorting.

Quote from: Aistis
I believe uses sorting was on by default with both my Lazarus installs (or I might be mis-remembering).
No, its disabled by default in Lazarus.

Thaddy

  • Hero Member
  • *****
  • Posts: 14625
  • Sensorship about opinions does not belong here.
Re: Interesting USES block sorting behaviour with JEDI Code Format
« Reply #5 on: February 10, 2024, 07:57:32 am »
Labas, Aistis,

One further note on unit order, apart from sorting, is that it is good practice to put the FreePascal units before the Lazarus units.
Lazarus does that already automatically for you when you create a project.
That means in your example that sysutils should always be before stdctrls.
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

Aistis

  • New Member
  • *
  • Posts: 13
    • Titbits of mind leakage
Re: Interesting USES block sorting behaviour with JEDI Code Format
« Reply #6 on: February 11, 2024, 01:36:10 pm »
Labas, Aistis,

One further note on unit order, apart from sorting, is that it is good practice to put the FreePascal units before the Lazarus units.
Lazarus does that already automatically for you when you create a project.
That means in your example that sysutils should always be before stdctrls.

Thanks. I'll keep it in mind.

 

TinyPortal © 2005-2018