Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] Cleanup empty lines/sections #347

Closed
2 tasks
fvet opened this issue Mar 17, 2022 · 7 comments
Closed
2 tasks

[Suggestion] Cleanup empty lines/sections #347

fvet opened this issue Mar 17, 2022 · 7 comments

Comments

@fvet
Copy link
Contributor

fvet commented Mar 17, 2022

I really love the various cleanup rules that improve the code reading and enforce uniform code layout. However, during code review, we often find ourselves doing some minor cleanup of redundant code, that could be automated by some additional 'Cleanup Actions'


  • 1. Remove duplicate empty lines

In case a new line is followed by another new line, all extra new lines (except the first) could be removed / truncated. (see lines between field1 / field2)

Exception: if the new line is followed by a section end ( { or end) on could argue to remove the new line completely. (see lines within field2 section)

BAD CODE


        field(1; "Primary Key"; Code[10])
        {
            Caption = 'Primary Key';
            DataClassification = CustomerContent;
            NotBlank = false;
        }



        field(2; "Base URL"; Text[250])
        {
            Caption = 'Base URL', comment = 'NLB="Basis URL",FRB="URL Base"';
            DataClassification = CustomerContent;


        }

GOOD CODE

        field(1; "Primary Key"; Code[10])
        {
            Caption = 'Primary Key';
            DataClassification = CustomerContent;
            NotBlank = false;
        }

        field(2; "Base URL"; Text[250])
        {
            Caption = 'Base URL', comment = 'NLB="Basis URL",FRB="URL Base"';
            DataClassification = CustomerContent;
        }

Also remove empty lines between end of section markers ( } )

BAD CODE

            }

        }

    }

GOODCODE

            }
        }
    }

Also see StefanMaron/BusinessCentral.LinterCop#174


  • 2. Remove empty sections

By making use of various snippets, we are often left with 'empty' sections. These sections can be completely removed (as long as they contain no childs / code).

With sections, I refer to fields, keys, fieldsgroups, layout, actions ... (carrying no identifier)

BAD CODE

Table

    fields
    {
        
    }

    keys{    }

    fieldgroups
    {}

Page

    layout
    {
        
    }

    actions
    {
        
    }

GOOD CODE

@jwikman
Copy link

jwikman commented Mar 17, 2022

I really like this idea @fvet! But I would actually not want any empty lines at all between two fields...

So I would prefer this

        field(1; "Primary Key"; Code[10])
        {
            Caption = 'Primary Key';
            DataClassification = CustomerContent;
            NotBlank = false;
        }



        field(2; "Base URL"; Text[250])
        {
            Caption = 'Base URL', comment = 'NLB="Basis URL",FRB="URL Base"';
            DataClassification = CustomerContent;


        }

to be this

        field(1; "Primary Key"; Code[10])
        {
            Caption = 'Primary Key';
            DataClassification = CustomerContent;
            NotBlank = false;
        }
        field(2; "Base URL"; Text[250])
        {
            Caption = 'Base URL', comment = 'NLB="Basis URL",FRB="URL Base"';
            DataClassification = CustomerContent;
        }

But maybe that is just me..?
Maybe this is the challenge with these kind of features, a lot of this code cleaning is a matter of preferences...

I'm using Prettier when working with MarkDown and TypeScript and really like the cleanness that it produces - maybe someone should give it a go and create an AL Plugin for Prettier? 😁

@fvet
Copy link
Contributor Author

fvet commented Mar 17, 2022

@jwikman I in fact am in favour to remove the extra lines between the fields as well. Didn't want to go in detail on that part yet, since in some other cases (e.g. between procedures, regions, code, ...) I want to keep the empty line.

BAD CODE

   #region Unit Calculator
    internal procedure ShowUnitCalculator()
    begin
        Codeline1;
        Codeline2;


        Codeline3;
        Codeline4;

    end;


    internal procedure ShowUnitCalculator2()
    begin
    end;
    #endregion


    #region Unit Class
    internal procedure CreateItemUnitOfMeasureFromUnitClass()
    begin
    end;
    #endregion

GOOD CODE

    #region Unit Calculator
    internal procedure ShowUnitCalculator()
    begin
        Codeline1;
        Codeline2;

        Codeline3;
        Codeline4;
    end;

    internal procedure ShowUnitCalculator2()
    begin
    end;
    #endregion

    #region Unit Class
    internal procedure CreateItemUnitOfMeasureFromUnitClass()
    begin
    end;
    #endregion

@jwikman
Copy link

jwikman commented Mar 17, 2022

Totally agree on that @fvet 👍

@anzwdev
Copy link
Owner

anzwdev commented Mar 19, 2022

That's a good idea, I'll add it.

@anzwdev
Copy link
Owner

anzwdev commented Apr 23, 2022

I've released a new version of the extension, there are 2 new sets of commands that you can use:

  • "Remove Empty Lines from the Active Editor"
  • "Remove Empty Lines from the Active Project"
    and
  • "Remove Empty Sections from the Active Editor"
  • "Remove Empty Sections from the Active Project"

You can also use these commands with code cleanup using these 2 new additional "alOutline.codeCleanupActions" setting values:

  • "RemoveEmptyLines",
  • "RemoveEmptySections"

@fvet
Copy link
Contributor Author

fvet commented Apr 25, 2022

Will implement this right away.
Thanks for adding this new feature!

@mjmatthiesen
Copy link

Old issue I know, but is it possible to call these from OnSave the way props etc can be sorted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants