Tool B4a Source Code Formatter (now open source)

SOURCE CODE FORMATTER
Cleans up your Basic4Android project source code

As a programmer I tend to write code without too much regard for neatness with the intention of cleaning it up at a later date. As my projects became longer it could take quite a while to correctly space and indent my code - so I wrote a VB program to take care of this for me.

The program is available free of charge and all comments and suggestions are welcomed. Erel has since uploaded this to the website, you can download this to a pc from here:

www.basic4ppc.com/android/files/B4ACoderSetup.zip

************************** Version 1.9 now available. ****************************

B4aCoder.jpg


1) Source code is formatted neatly in a user configurable manner.
2) Wrongly scoped and unused declared variables are listed.
3) Dead code is listed.
4) Fault listings appear in a datagridview on screen and can be printed to the printer.

So far we have tested it on a 22 module program and it has worked faultlessly. The interface has recently been updated to reduce flickering and additional functions handle public and private subs. Wrongly scoped variables are now noted and recommendations given to reduce a variables scope where appropriate.
Please leave a comment if you find the program useful.

Hope you like it.

UPDATE: Visual Studio (VB 2008) original source code is now available for programmers who would like to go on to improve this program for the benefit of the B4A community. www.basic4ppc.com/android/files/B4ACoder.zip. Download the zip file and place the contents in your Visual Studio projects directory.
 
Last edited:

Djembefola

Active Member
Licensed User
Longtime User
i'm getting an "access denied" error when opening a project:

System.UnauthorizedAccessException: Der Zugriff auf den Pfad "C:\b4aRebuildFile" wurde verweigert.
bei System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
 

klaus

Expert
Licensed User
Longtime User
I get the same error with Windows7.
I created the forder C:\b4aRebuildFile and set all authorisations but no success.

Best regards.
 
Last edited:

Tom Law

Active Member
Licensed User
Longtime User
OK I think I know whats going on here - try turning off the UAC in windows 7.
 

Djembefola

Active Member
Licensed User
Longtime User
Can I ask you to check whether you have write permissions on the folder that contains your b4a project.

Hi Tom,

yes , the folder has write permissions.

i have already tried this (on Windows 7):
1. added folder C:\b4aRebuildFile: no success, same error message.
2. added empty file (without file extension) C:\b4aRebuildFile
this leaded to a different error message directly after starting your program.
 

Tom Law

Active Member
Licensed User
Longtime User
Mabool is correct. I was writing the temporary file to the Windows root directory and you need admin permissions to do that. Just right click the desktop icon and run as administrator. I will get an updated version out to sort this.

VERSION 1.1 NOW UPLOADED
 
Last edited:

klaus

Expert
Licensed User
Longtime User
VERSION 1.1 NOW UPLOADED Works without problem :).

My comments:
- I have taken the habit to write the code directly with correct indentation.
- In your version of indentation I don't feel comfortabel.

I like to separate parts of a routine with empty lines without the apostrophe chararcter.
In your indentation routine you add also empty lines with an apostrophe in For/Next loops, If / Then conditions and others.

Just an example:
How I like it:
B4X:
Sub Activity_Resume
    Activity.Title = ProgName
    SQLTableRead
    If SelectedRow >= 0 Then
        SelectRow(SelectedRow, 0)
    End If

    Dim bdw As BitmapDrawable
    If flgFilter = True Then
        bdw.Initialize(LoadBitmap(File.DirAssets, "btnFilter1.png"))
    Else
        bdw.Initialize(LoadBitmap(File.DirAssets, "btnFilter0.png"))
    End If
    btnFilter.Background = bdw
    
    If SelectedRow = -1 Then
        btnDelete.Visible = False
        btnEdit.Visible = False
    Else
        btnDelete.Visible = True
        btnEdit.Visible = True
    End If
End Sub
Your code change:
B4X:
Sub Activity_Resume
'
    Activity.Title = ProgName
    SQLTableRead
'
    If SelectedRow >= 0 Then
'
        SelectRow(SelectedRow, 0)
'
            End If
'
            Dim bdw As BitmapDrawable
'
            If flgFilter = True Then
'
                bdw.Initialize(LoadBitmap(File.DirAssets, "btnFilter1.png"))
'
            Else
'
                bdw.Initialize(LoadBitmap(File.DirAssets, "btnFilter0.png"))
'
            End If
'
            btnFilter.Background = bdw
'
            If SelectedRow = -1 Then
'
                btnDelete.Visible = False
                btnEdit.Visible = False
'
            Else
'
                btnDelete.Visible = True
                btnEdit.Visible = True
'
            End If
'
        End Sub
Personally I prefer the first version it's for me easier to read but this is just my personal vision of the question.
In the example above there is a problem with indentaion of the If Then / End condition. In some other parts of the same program If Then / Else / End If is not indented correctly either.

Please don't take these comments as critics but as constructive comments.

Best regards.
 

Tom Law

Active Member
Licensed User
Longtime User
Much appreciated Klaus - If I get sufficient feedback I will consider building in some user configurable settings to allow people to adjust the formatting to suit their own personal preferences.
 

Harris

Expert
Licensed User
Longtime User
I formatted a simple project without issue.

Then I tried my large project. I now get this error trying to open the project file: "The given key was not present in the dictionary."

I don't know how to get around this one. The project will not open now. The backup copy I made before running still opens fine.


I am with Klaus on the style. Additional lines not required.
User prefs, as you suggested, would be a good idea. You are certainly on the right track.

Keep up the great work. I look forward to your efforts.

Thanks
 

Tom Law

Active Member
Licensed User
Longtime User
Cheers Harris, that is useful information - could I ask how many lines you have in your large project?


Tom
 

awama

Active Member
Licensed User
Longtime User
Code Re-formatting should be job of the IDE

Hi Tom Law,
I think an external program to format the source code should not be necessary. Code formatting is a "must have" feature for a software programming IDE, so it should not miss this simple function.
A shortcut in the IDE should be easy to check and reformatting the code structure.
Maybe Erel make us happy with the next update?
Additionally adjustable might be:
Different indentation for loops (do-loop, for-next ..) and conditions (if-then, select-case)
 

Tom Law

Active Member
Licensed User
Longtime User
Cheers Awama,

However until b4a has a built in facility perhaps this will do. I hope to have version 1.2 out shortly to allow you to configure your own code settings.

UPDATE: Version 1.2 is now available
 
Last edited:

tremara1

Active Member
Licensed User
Longtime User
key not found in dictionary error

I had the same error after using first version it seems a blank line was inserted at the start of the .b4a file. I opened it in notepad and removed the line and it worked again. Still a great tool though.....thanks.
 

Harris

Expert
Licensed User
Longtime User
Cheers Awama,

However until b4a has a built in facility perhaps this will do. I hope to have version 1.2 out shortly to allow you to configure your own code settings.

UPDATE: Version 1.2 is now available

I am sure Erel has many other tasks on his plate than assisting us who can't press the tab key at the appropriate time (that's ME!)

Thank you Tom for helping us out. Just installing .net on my desktop. Will answer shortly.
 

Harris

Expert
Licensed User
Longtime User
Holy Crap! I can read my code!!!!

This worked great om my main module which is 1537 lines long.
However it did not seem to touch any other module in my project.
I used default settings. What am I doing wrong?

Thanks a bunch...

OOPS! pasting code directly doesn't show the issue... removing next lines...

How does one paste code that looks properly formatted?

It has been more that a year and I haven't figured this out... Time to bow and humbley ask...

Thanks.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
+1, i agree that the 1st variant is preferable, if to get it from any other source code.
Also using the spaces for sure around the "=<>" and after ",", if it's not.
 

Tom Law

Active Member
Licensed User
Longtime User
Hi Guys,

Thanks to everyone who posted - continued user feedback will identify any problems and help create a better program.

tremara1
Could I ask you to retest the formatter to see if the blank line is still being inserted with this version at the beginning - I wonder if this was from an earlier version as I can't reproduce this. If it is could you let me know what the first readable line in your program is.

Harris
Many thanks for your positivity and helpful comments. At the moment only the main b4a module is being targetted for formatting, however as program development progresses I will also take a look at bas files.

peacemaker
Thank you for your comments, if I understand you correctly you suggest spacing out non alpha numeric characters with spacing - I will have a look at improving that also.

Clean readable code makes it much easier for problems to be identified reduces complexity and allows work to be optimised. Hopefully this tool will be of use to those of us that tend to rush optimistically into programming without consideration of tidyness or clean layouts. Before I created this program my code was like my desk - there was stuff piled everywhere - I knew where it was but nobody else did.

.
 

tremara1

Active Member
Licensed User
Longtime User
retested

Hi, I tried another project with the latest version with no problems.....I do not have the original version to retry with.
Just a note as to the fact it is a stand alone program, firstly I don't think my shoddy coding should be the IDE's problem , secondly I used to spend ages formatting my apps, with this it is a couple of clicks....no big deal I think. Having said this I want to try be more careful at the time of writing the code, but having a track record of starting out ok and as the app code develops it gets a little loose this will be a great addition.
 
Top