Wish Collapsing displayed code

TomA

Active Member
Licensed User
Longtime User
The B4A IDE will let you collapse subs to show only a single line. So code like this
B4X:
...
End Sub
Sub ShowPlace
    ' This shows the player info about the current location
    Dim x, y As Int
    Dim ExitsInfo As String
    Dim Exits(4) As String
    x = Main.Player.PlayerX
    y = Main.Player.PlayerY
    Exits(0) = ""
...about 150 lines of code
End Sub
Sub DoCmd(Cmd As String)
...
can be displayed as this
B4X:
...
End Sub
Sub ShowPlace
Sub DoCmd(Cmd As String)
...

This makes it much easier to work on modules that contain many lines of code.
It would be nice if there were a way to add some kind of flags so that blocks of code within a sub could be collapsed to a single line. For example, if code like this
B4X:
    Select Case Main.Player.PlayerLoc
        Case 0    ' Player is on surface
            If Main.Maps.Above(x, y).Visited Then
                If Main.Rooms.Surface(Main.Maps.Above(x, y).RoomPtr).ShortDesc = "ditto" Then
                    Printit(Main.Rooms.Surface(Main.Maps.Above(x, y).RoomPtr).LongDesc, False)
                Else
                    Printit(Main.Rooms.Surface(Main.Maps.Above(x, y).RoomPtr).ShortDesc, False)
                End If
            Else
...many more lines of code...
        Case 1    ' Player is in tunnel
            If Main.Maps.Tunnel.Visited Then
                If Main.Rooms.Tunnel(Main.Maps.Tunnel.RoomPtr).ShortDesc = "ditto" Then
                    Printit(Main.Rooms.Tunnel(Main.Maps.Tunnel.RoomPtr).LongDesc, False)
                Else
                    Printit(Main.Rooms.Tunnel(Main.Maps.Tunnel.RoomPtr).ShortDesc, False)
                End If
            Else
...more code...
could be collapsed to show something like this
B4X:
    Select Case Main.Player.PlayerLoc
        Case 0    ' Player is on surface
        Case 1    ' Player is in tunnel
            If Main.Maps.Tunnel.Visited Then
                If Main.Rooms.Tunnel(Main.Maps.Tunnel.RoomPtr).ShortDesc = "ditto" Then
                    Printit(Main.Rooms.Tunnel(Main.Maps.Tunnel.RoomPtr).LongDesc, False)
...more code...
It would be a big help in coding within some subs.
I have one sub that presently contains over 400 lines of code which I expect may reach well over twice that before it is complete. In this particular sub, there is a Select Case statement where each case contains many lines of code that, once tested, I do not really need to see when I am working on the rest of the program. It would make it much easier if I could collapse some sections to a single line while I am working on the program - would avoid having to do a lot of scrolling through code.
 

klaus

Expert
Licensed User
Longtime User
I have one sub that presently contains over 400 lines of code which I expect may reach well over twice ...
This is by far too many code lines in a single routine.
You should split it to get an overview.
... there is a Select Case statement where each case contains many lines of code ...
I'm almost sure that you have a lot of repetitive code which could be shortened a lot.
Could you show us this routine so we could have a lokk at it and see if it's possible to shorten the number of code lines.
Otherwise you can use Regions as already suggested by LucaMs.
 

TomA

Active Member
Licensed User
Longtime User
You're right, but to which structures apply that functionality? At the Select? the IF Then Else?

For this purpose, I use a lot of #Region / #End Region which can be collapsed also.

Somehow I missed the #Region / #End Region stuff - that does almost exactly what I want. Thanks for pointing me to it.
 

TomA

Active Member
Licensed User
Longtime User
This is by far too many code lines in a single routine.
You should split it to get an overview.

I'm almost sure that you have a lot of repetitive code which could be shortened a lot.
Could you show us this routine so we could have a lokk at it and see if it's possible to shorten the number of code lines.
The routine in question has many lines - it is parsing a sentence entered by the user in an adventure type game. The common stuff that needs to be done is all done in one place, a large part of the code is contained in a Select area where each case has enough differences to require unique code. Anything common to multiple cases is done with a subroutine. A short and partial example is:
B4X:
     Select ThisCmd
            Case "n", "north", "e", "east", "s", "south", "w", "west"
                MovePlayer(ThisCmd)
            Case "u", "up"
                If (Main.Player.PlayerLoc = 1) AND ((ThisCmd = "u") OR (ThisCmd = "up")) Then
                    If (Specials.Length = 1) AND (Specials(0) = "door") AND (Main.Maps.SurfaceDoor.DoorState <> 5) Then
                        Printit("The door is not open", True)                   
                    Else
                        MovePlayer(ThisCmd)
                    End If
                Else
                    MovePlayer(ThisCmd)
                End If
            Case "d", "down"
                i = 0  ' to be used for a match
                If (Main.Player.PlayerLoc = 0) AND (Specials.length <> 0) Then
                    For j = 0 To Specials.length -1
                        If (Specials(j) = "hive") OR (Specials(j) = "beehive") _
                                                    OR (Specials(j) = "gap") OR (Specials(j) = "fissure") Then
                            i = -1
                            Printit("You cannot go that way", True)
                        End If
                    Next
                End If
                If i = 0 Then  ' Not Player Loc 0 so nothing special to do
                    MovePlayer("d")
                End If
...many more cases...
    End Select
Some of the cases involve a verb followed by an object optionally followed by the object to which the action should be applied - Such as 'swing silver sword at huge orc'. There is no simple way to break that down into simpler routines other than having each case call a separate routine which doesn't really simplify things very much. Believe me, once the app is finished, I review the code in detail to make it as efficient as possible. (doing that sort of thing was one of my specialties before I retired)
 
Top