Back to basics.

Posts   
 
    
Posts: 497
Joined: 08-Apr-2004
# Posted on: 07-Dec-2004 14:53:19   

Hi there.

I've been working away on a BLL class that deals with a whole lot of business rules that eventually determine whether some "key performance indicators" are good or bad.

Now - I work with a "domain manager" type clas for the business domain. My class manages the performance aspect of the system. It contains a number of methods that do useful stuff for the PL - things like "ReCacheData", and "GetPerformanceData(x,y,z)".

The way I have been creating this is by trying to create methods that serve a well-encapsulated purpose - you call "DoSomthing(x,y,z)", and you get something back (usually an entity). Inside these methods I have local variables, and thats pretty much it.

The situation I am having now is that I need to add a lot of BL logic to this class - in particular to one method that calculates the PerformanceValue. This method is gettting very big, and really needs to be split up into smaller methods. The problem I am having is that all my variables pertain to my method, and I don't want to start passing them around all over the place to other methods. Should I be making these variables private class-wide variables?

It might sound a stupid question - but I wonder about having too many class-wide variables, and if this will just add confusuin to the page....

Any thoughts on this quite basic question?

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 07-Dec-2004 17:27:07   

If your using c# and have vs 2005 you could try the refactoring tool. I've heard that it's pretty cool. Then, just cut and paste it back into you vs 2003 project. sunglasses

Fishy

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 08-Dec-2004 11:51:43   

Or you could download Re-Sharper. There's a trial...

http://www.jetbrains.com/resharper/

This tool is an absolute MUST for all developers, there's no way I could go back to VS2003 without it.

Marcus

Posts: 497
Joined: 08-Apr-2004
# Posted on: 08-Dec-2004 13:53:36   

My God - that tool rocks!!

Thanks V. much for pointing me at it - its saved me hours simple_smile

wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 08-Dec-2004 14:18:34   

MattWoberts wrote:

My God - that tool rocks!!

Thanks V. much for pointing me at it - its saved me hours simple_smile

Yeah, cool tool except when your PC is die'ng because the resharper steals sooooo.. much memmory. When i open my project my available memmory drops from 600MB available down to 120MB

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39752
Joined: 17-Aug-2003
# Posted on: 08-Dec-2004 17:26:14   

... or when resharper is simply dogslow on analysing your large project... I removed it after I saw it crawl on the llblgen pro designer code. After a while it worked ok, but then when I refactored something, it started analysing again... They seem to have fixed a lot of these issues, but still...

Frans Bouma | Lead developer LLBLGen Pro
Posts: 497
Joined: 08-Apr-2004
# Posted on: 08-Dec-2004 17:49:07   

Hmmmm, OK, I withdraw my original appraisal. Having tried to use some of the generated code, I have found what Otis found - its dog slow!

I have turned off code completion, it seems to be here it grinds to a halt.... So far so good...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39752
Joined: 17-Aug-2003
# Posted on: 08-Dec-2004 18:15:46   

Just for the people who can use 'em: my VS.NET macro's (for C#, but you can easily alter them for VB.NET)


Imports EnvDTE
Imports System
Imports System.Text
Imports System.Diagnostics

Public Module CSharp

    ''' Makes the selected lines the class property declarations region
    Sub MakeMemberRegion()
        MakeRegion("Class Member Declarations")
    End Sub

    ''' Makes the selection the class property declaration region.
    Sub MakePropertyRegion()
        MakeRegion("Class Property Declarations")
    End Sub

    ''' Makes a generic C# region of the selected lines and asks for the name of the region.
    Sub MakeRegion()
        Dim regionName As String = InputBox("Region name: ")

        MakeRegion(regionName)
    End Sub

    Sub MakeRegion(ByVal regionName As String)
        Dim selection As TextSelection = DTE.ActiveDocument.Selection()
        Dim start As EditPoint = selection.TopPoint.CreateEditPoint()
        Dim endpt As EditPoint = selection.BottomPoint.CreateEditPoint()

        DTE.UndoContext.Open("Make Region")
        Try
            ' insert the start of the region
            start.Insert("#region " & regionName)
            start.SmartFormat(start)
            Dim cursorPoint As EditPoint = start.CreateEditPoint()
            start.Insert(Environment.NewLine)

            If selection.IsEmpty Then
                start.Insert("#endregion")
                start.SmartFormat(start)
                start.Insert(Environment.NewLine & Environment.NewLine)
                Dim ts As TextSelection = DTE.ActiveWindow.Selection
                ts.MoveToPoint(cursorPoint)
            Else
                ' insert the end of the region
                If Not (endpt.AtStartOfLine) Then
                    endpt.Insert(Environment.NewLine)
                End If
                endpt.Insert("#endregion")
                endpt.SmartFormat(endpt)
                endpt.Insert(Environment.NewLine & Environment.NewLine)
            End If
        Finally
            DTE.UndoContext.Close()
        End Try
    End Sub


    ''' Creates a property. 
    ''' format: type name  has to be present at the current line.
    ''' Uses caMel casing.
    Sub CreateProperty()
        Dim typeName, propertyName, memberName, nameRead As String
        Dim ts As TextSelection
        Dim sb As New StringBuilder
        Dim boolIsOpened As Boolean

        Try
            ts = DTE.ActiveDocument.Selection
            If (ts.IsEmpty) Then
                ts.SelectLine()
            End If

            typeName = (ts.Text.Split(" ")(0)).Trim()
            nameRead = ((ts.Text.Split(" ")(1))).Trim()
            propertyName = nameRead.Substring(0, 1).ToUpper & nameRead.Substring(1)
            memberName = "_" + nameRead.Substring(0, 1).ToLower() & nameRead.Substring(1)

            ' actual property
            sb.Append("/// <summary>" & Environment.NewLine)
            sb.AppendFormat("/// Gets / sets {0}{1}", nameRead, Environment.NewLine)
            sb.Append("/// </summary>" & Environment.NewLine)
            sb.Append("public " + typeName + " " + propertyName)
            sb.Append(vbCrLf)
            sb.Append("{")
            sb.Append(vbCrLf)

            ' get
            sb.Append("get")
            sb.Append(vbCrLf)
            sb.Append("{")
            sb.Append(vbCrLf)
            sb.AppendFormat("return {0};", memberName)
            sb.Append(vbCrLf)
            sb.Append("}")
            sb.Append(vbCrLf)

            ' set
            sb.Append("set")
            sb.Append(vbCrLf)
            sb.Append("{")
            sb.Append(vbCrLf)
            sb.AppendFormat("{0} = value;", memberName)
            sb.Append(vbCrLf)
            sb.Append("}")
            sb.Append(vbCrLf)
            sb.Append("}")
            sb.Append(vbCrLf)
            sb.Append(vbCrLf)

            'Check to see if UndoContext object is already open.
            If DTE.UndoContext.IsOpen = False Then
                'Open the UndoContext object to track changes.
                Call DTE.UndoContext.Open("CreateProperty " & propertyName, False)
                boolIsOpened = True
            End If

            ' Replace the text
            ts.Delete()
            ts.Insert(sb.ToString(), vsInsertFlags.vsInsertFlagsInsertAtStart)
        Finally

            'If UndoContext was already open, don't close it.
            If boolIsOpened = True Then
                'Close the UndoContext object to commit the changes.
                DTE.UndoContext.Close()
                ' Format the Selection
                ts.SmartFormat()
                ts.MoveToPoint(ts.BottomPoint.CreateEditPoint())
                ts.LineUp()
            End If
        End Try
    End Sub


    Sub ForLoop()
        Dim selection As TextSelection = DTE.ActiveDocument.Selection
        selection.Text = "for (int i = 0; i < ; i++)"
        selection.NewLine()
        selection.Text = "{"
        selection.NewLine()
        selection.Text = "}"
        selection.LineUp(False, 2)
        selection.CharRight(False, 19)
    End Sub

    Sub ForLoopJ()
        Dim selection As TextSelection = DTE.ActiveDocument.Selection
        selection.Text = "for (int j = 0; j < ; j++)"
        selection.NewLine()
        selection.Text = "{"
        selection.NewLine()
        selection.Text = "}"
        selection.LineUp(False, 2)
        selection.CharRight(False, 19)
    End Sub

    'Description: Inserts if block
    Sub IfNoElse()
        DTE.ActiveDocument.Selection.Text = "if ()"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.LineUp(False, 2)
        DTE.ActiveDocument.Selection.CharRight(False, 3)
    End Sub

    ' Description: Inserts if-else block
    Sub IfElse()
        DTE.ActiveDocument.Selection.Text = "if ()"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "else"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.LineUp(False, 6)
        DTE.ActiveDocument.Selection.CharRight(False, 3)
    End Sub

    ' Description: Inserts switch block
    Sub Switch()
        DTE.ActiveDocument.Selection.Text = "switch ()"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "case :"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "break;"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "case :"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "break;"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "default:"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "break;"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.LineUp(False, 8)
        DTE.ActiveDocument.Selection.CharRight(False, 7)
    End Sub

    ' Description: Inserts generic exception block
    Sub Exception()
        DTE.ActiveDocument.Selection.Text = "try"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "catch (System.Exception ex)"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.LineUp(False, 4)
    End Sub


End Module

(yes that's VBA, vs.net macro's have to be written in this brittle 'language' cry )

Frans Bouma | Lead developer LLBLGen Pro
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 17-Dec-2004 16:48:40   

DevExpress has a refactor tool that is out in beta that does a really good job.