11 Replies - 910 Views - Last Post: 24 August 2016 - 05:42 AM Rate Topic: -----

#1 The Unliked  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-November 15

Putting Code Into Multiple Files?

Posted 05 August 2016 - 05:52 AM

Hi all,
I'm in the process of making a one of my largest programs so far. Currently I have 2k lines of code split over 2 forms (I know not that much because I'm still relatively new). Is 1k lines of code in one file too much? Should I split the code into several smaller modules?

Thanks in advance!
Is This A Good Question/Topic? 0
  • +

Replies To: Putting Code Into Multiple Files?

#2 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 350
  • View blog
  • Posts: 1,503
  • Joined: 07-September 13

Re: Putting Code Into Multiple Files?

Posted 05 August 2016 - 06:06 AM

There is no definite answer to your question with the information you have given us.

You have provided us with no information about what the form is doing, or what it looks like. if your doing intensive UI work and only UI work then it might be right.

Other things to take into consideration is Line Gaps, you say there is over 1k of lines of code however after each line of code do you have a line break??

At this point i can only go by my standards, and 1k is WAY to much however this may not be the case.

In the last 3 days i have created 4 different forms 2 just being a read-only form and 1 straight forward editable form and form which is a editable dragon drop form.

None of these forms go past 300 lines of code.

Ideally any business logic should not be done in the form, this may help, so any database transaction or even read/writing from files could be separated out into different class files.

I suggest reading up on OOP Design patterns and going from there.
Was This Post Helpful? 2
  • +
  • -

#3 The Unliked  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-November 15

Re: Putting Code Into Multiple Files?

Posted 05 August 2016 - 09:16 PM

Most of my code in the forms are doing the logic. How should I split up the code?
Was This Post Helpful? 0
  • +
  • -

#4 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 350
  • View blog
  • Posts: 1,503
  • Joined: 07-September 13

Re: Putting Code Into Multiple Files?

Posted 06 August 2016 - 02:30 PM

Again this is no where near enough information, look at the bigger picture, all code is doing logic.

Therefore you might as well said i have code how can you split code up.

So that in mind what does "most of my code in forms are doing the logic" mean?

No one on here is going to help you re-write your application in this cryptic format or at all really.
Was This Post Helpful? 0
  • +
  • -

#5 The Unliked  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-November 15

Re: Putting Code Into Multiple Files?

Posted 07 August 2016 - 10:40 PM

I'm just trying to kind of get an idea of how to split it up. I'll try to explain how my program work simply.

the program I'm writing is for a church to display songs.

It has 2 forms. One to control the program (search the database...ect...) and another form to display on a projector screen. Both forms currently contain over 1k lines of code.

The code is divided into many smaller subs. Their lengths range from 1 - 100 lines.

For example, in start of the first form, I have these subs:
loadRegistryItems (check if registry items are still there and load the items into variables) - 20 lines
setupscreen (puts form1 on monitor 1 and form2 on monitor 2) - 6 lines
loadDs (sets database connection string and loads data into my dataset) - 20 lines
form1_Load (obviously load event. this sub calls many other subs, including the previous subs I listed) - 50 lines

further on i have subs for displaying songs, adding songs to sing, capturing keystrokes and things like that.
Was This Post Helpful? 0
  • +
  • -

#6 TechnoBear  Icon User is offline

  • Lady A
  • member icon

Reputation: 185
  • View blog
  • Posts: 818
  • Joined: 02-November 11

Re: Putting Code Into Multiple Files?

Posted 08 August 2016 - 01:21 AM

Quote

loadRegistryItems (check if registry items are still there and load the items into variables) - 20 lines


This definitely sounds like something that could be moved outside of the form code behind.

Personally I like to keep my individual class files as limited as possible and to only contain related methods. So things like getting data from a data base will be in one class file, but parsing the data would be handled in another. It just makes it much easier to debug should there be issues.
Was This Post Helpful? 1
  • +
  • -

#7 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 350
  • View blog
  • Posts: 1,503
  • Joined: 07-September 13

Re: Putting Code Into Multiple Files?

Posted 08 August 2016 - 01:36 AM

An example of how I would potentially layout a object for the Registry items in a object is this:


    Private Class RegistryInformation

        ' Your declare your variables as properties
        Private newPropertyValue As String
        Public Property NewProperty() As String
            Get
                Return newPropertyValue
            End Get
            Set(ByVal value As String)
                newPropertyValue = value
            End Set
        End Property

        ' Factory method to create this class
        Shared Function GetRegistryItemList() As RegistryInformation
            Dim registryItemList As New RegistryInformation
            registryItemList.Fetch()
            Return registryItemList
        End Function

        Private Sub New()
            ' make the class private so it can only be created from inside itself 
        End Sub

        Private Sub Fetch()

            ' Code here that reads in your RegistryItems and whenever you want to add each

        End Sub

        Public Sub Save()
            ' Saves data back to the registry

        End Sub

    End Class


However this is a very basic and simple example, which would most likely work in this scenario but not in others where you would need to track the status of the object, as you don't want to be writing back clean objects to the database as that is just a wasted database trip.

I will point this out again as I did in the first post the best thing you can do is pick up a oop book and learn all about object orientated programming.
Was This Post Helpful? 2
  • +
  • -

#8 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,916
  • Joined: 12-December 12

Re: Putting Code Into Multiple Files?

Posted 08 August 2016 - 10:38 AM

During your studying of object-oriented programming, you will also encounter the principle of separation of concerns.
Was This Post Helpful? 2
  • +
  • -

#9 The Unliked  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-November 15

Re: Putting Code Into Multiple Files?

Posted 08 August 2016 - 10:03 PM

Thanks for all the replies. I'll definitely look into separation of concerns.
Was This Post Helpful? 0
  • +
  • -

#10 The Unliked  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-November 15

Re: Putting Code Into Multiple Files?

Posted 23 August 2016 - 04:20 AM

Ok, so I've been trying to split up my code into different modules and I've come across a problem. A lot of my variables are not accessible unless I do classname.variablename. This gets annoying as I have to do this for almost all my variables. Am I meant to keep referring like that, move the variable over to the module or just pass it as a parameter to each subroutine/function?
Was This Post Helpful? 0
  • +
  • -

#11 TechnoBear  Icon User is offline

  • Lady A
  • member icon

Reputation: 185
  • View blog
  • Posts: 818
  • Joined: 02-November 11

Re: Putting Code Into Multiple Files?

Posted 24 August 2016 - 02:59 AM

Quote

Am I meant to keep referring like that, move the variable over to the module or just pass it as a parameter to each subroutine/function?


I find it easier to refer to them as Classname.Variable personally. As for sending it as a parameter, it depends entirely on your project. Remember, not everything needs to be brought out of the local scope and not everything has to be a static object.
Was This Post Helpful? 1
  • +
  • -

#12 The Unliked  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-November 15

Re: Putting Code Into Multiple Files?

Posted 24 August 2016 - 05:42 AM

Ok thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1