Jump to content

Code Formatter


Recommended Posts

  • 4 years later...

Does anyone know if there is a code formatting udf available now? Of course tidy does a lot, but does not everything. Hereunder I put some samples where the code could be much cleaner

Example 1: Unnecessary white lines (or adding lines to make the code more readable).

While 1

    Sleep(100)


WEnd

'Func Form1Maximize()
EndFunc   ;==>Form1Maximize
Func Form1Minimize()

EndFunc   ;==>Form1Minimize
Func List1Click()   
EndFunc   ;==>List1Click
Func Form1Restore()

Example 2: Errors

Tidy also gives syntax errors, but has no option to automatically correct such errors (a report in the console would then be enough). For example

;### Tidy Error: If/ElseIf statement without a then..
If $x
    Sleep(100)
EndIf

Example 3: Layout best practices

Tidy also can't apply best order practices. For example options on top, then includes, then options, declarations on top of a function etc. The following code has an unfamiliar order of statements and also pieces of death code. However the syntax is correct and tidy.

Start()

While 1

    Sleep(100)
    Close()

WEnd


Func Close()
    If True Then
        Exit
        Sleep(100)
        MsgBox(0, "", "Goodbye")
    EndIf

EndFunc   ;==>Close


Func Start()
    $x = "Hello"

    MsgBox(0, "", $x)
    Local $x
EndFunc   ;==>Start

Opt("GUIOnEventMode", 1)
#include <GUIConstantsEx.au3>
#AutoIt3Wrapper_Run_Obfuscator=y

Example 4: Variables

I think it would be clearer if variables are alphabetical ordered and unused are deleted (like variable $d in the example below) or added locally if not declared.

Start()

Func Start()

Local $c, $a, $d
Local $b

    $a = "Begin"
    $b = "Middle"
    $c = "End"

    MsgBox(0, "", $a)
    MsgBox(0, "", $b)
    MsgBox(0, "", $c)

EndFunc
Link to comment
Share on other sites

Tidy is meant to "Tidy" your code, not fix your mistakes. If you are using the full install of Scite4Autoit3 then you can hit CTRL-F7 and go to the Tidy tab and see what options are available to correct some of those issues. For instance, /sf will sort your functions by name, /rel will remove extra lines (blank lines), etc.

In the long run though, best coding practices are the coders responsibility, and not Tidy's.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

skin27,

Nice piece of necro-resuscitation there! :mellow:

The short answer to your question is: No.

The longer replies to your points are:

Unnecessary white lines (or adding lines to make the code more readable).

Tidy will remove lines for you - from the Tidy page accessible from the full SciTE4AutoIt3 Help file (which I assume you have as you use Tidy):

*** Remove Empty lines from Script Source. 1=Remove all 2=Remove all more then 1
Remove_Empty_Lines=1
Remove_Empty_Lines=2

As to adding lines for clarity - a simple Replace action in SciTE should do what you want. If you add too many, then Tidy will remove the excess.

Tidy also gives syntax errors, but has no option to automatically correct such errors

How is Tidy supposed to know what you intended to code at that point? In your example it is obvious, but in many other cases it would not necessarily be so. At least Tidy warns you then there is a problem - asking it to solve it for you is a step too far in my opinion.

Tidy also can't apply best order practices

Do you not do this yourself as you code? And who decides what is "best order practice"?

I think it would be clearer if variables are alphabetical ordered and unused are deleted

Personally I order any variables declared at the top of a script/function in related clusters which I find the clearest way to do it. Furthermore I tend to declare variables as they are first used in the code and not declare them initially unless they are first used in a loop where the multiple checks of the scope would slow down the code.

You do realise that Au3Check will highlight unused Local variables for you to delete manually? And that Obfuscator will remove all unused variables if you ask? That way your compiled file will be as small as possible. I use the following directives:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/sf /sv /cs=0 /cn=0  ; I also add /om but that is for another reason

Sorry if this comes across as a bit negative, but you have to take some responsibility for yoru own code. I use Tidy and the other tools mentioned above a lot and find them invaluable, but I still want to retain the ultimate control over how the script is set out. However, you could perhaps try to write your own formatting UDF - I for one would be interested to see it.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Tidy is meant to "Tidy" your code, not fix your mistakes. If you are using the full install of Scite4Autoit3 then you can hit CTRL-F7 and go to the Tidy tab and see what options are available to correct some of those issues. For instance, /sf will sort your functions by name, /rel will remove extra lines (blank lines), etc.

In the long run though, best coding practices are the coders responsibility, and not Tidy's.

1) Tidy parameters: The tidy parameters are not very suitable. For example the /rel parameters removes empty lines, including the ones that makes the code more readable. The /sf parameter isn't very useful I think.

2) Best practices: Those practices can be extracted from code samples/UDF's and developers can decide on consensus what is reasonable. New coders can learn it this way and make their code readable from the start.

3) Auto-correct: Maybe this shouldn't be default, but a lot or syntax errors are so obvious I always take the syntax recommended by the checker. For example "If/ElseIf statement without a then.." I add then

And yeah I'm a lazy coder :mellow:

Link to comment
Share on other sites

1) Tidy parameters: The tidy parameters are not very suitable. For example the /rel parameters removes empty lines, including the ones that makes the code more readable. The /sf parameter isn't very useful I think.

I believe there's a parameter for /rel that tells tidy to only remove more than X number of lines. Again, Tidy can't know what you're trying to do, so how is it to know that you WANT blank lines some places, and don't want them other places. That's not logical at all.

/sf does what you were asking for when you asked if it could place the relevant code above the functions as it will sort the code so that all functions are at the end of the script in alphabetical order. Your so-called death code in the example is up to you to take care of, no piece of software is going to correct poor coding practices completely.

2) Best practices: Those practices can be extracted from code samples/UDF's and developers can decide on consensus what is reasonable. New coders can learn it this way and make their code readable from the start.

There's no such thing as a universal best coding practice, there are always minor or major differences of opinion as to how a program should be laid out. Once again, it's your responsibility to code in what you think is the best practice for you to understand it, and so that others are able to follow your script if you are going to share code with others.

3) Auto-correct: Maybe this shouldn't be default, but a lot or syntax errors are so obvious I always take the syntax recommended by the checker. For example "If/ElseIf statement without a then.." I add then

If you have a piece of software correcting your mistakes you're going to end up being a very mediocre programmer because you'll never learn to write good code to begin with, expecting something to hold your hand to fix things for you. Pointing out that you've made mistakes is a better approach because the Tidy program doesn't know what you were trying to do with the code you write, it's up to you to correct the mistakes and debug as needed.

And yeah I'm a lazy coder :mellow:

I can see that.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

There's no such thing as a universal best coding practice, there are always minor or major differences of opinion as to how a program should be laid out

Of course there no such thing, that's why it's called consensus or agreement, mostly established by an expert comity.

If you have a piece of software correcting your mistakes you're going to end up being a very mediocre programmer because you'll never learn to write good code to begin with, expecting something to hold your hand to fix things for you.

I don't care about being a good programmer, but about making nice applications as fast as I can. For example when using Koda I don't write the GUI code myself and I'm very happy it does that for me. When someone is holding your hand the person is tired to do it the next time, but fortunately a computer never gets tired :mellow:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...