Quantcast
Channel: SAP Business One SDK
Viewing all articles
Browse latest Browse all 49

On Naming and Such

$
0
0

So, we've been having a discussion around the office on what should be our standard, if any, regarding naming conventions, and when to use objects and sub-objects rather than to just use the full context of the objects. I might be using the wrong terminology here, but here's an example of what I mean. What's the difference between the following two sets of code?

 

Private oSAPAPI As SAPConnection

Dim oOrder As SAPbobsCOM.Documents = oSAPAPI.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)

oOrder.GetByKey("1")

oOrder.Lines.SetCurrentLine(1)

If oOrder.Lines.ItemCode = "foo" Then

    MsgBox("Foo!")

End If

 

Private oSAPAPI As SAPConnection

Dim oOrder As SAPbobsCOM.Documents = oSAPAPI.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oOrders)

oOrder.GetByKey("1")

Dim oOrderLines As SAPbobsCOM.Document_Lines = oOrder.Lines

oOrderLines.SetCurrentLine(1)

If oOrderLines.ItemCode = "foo" Then

    MsgBox("Foo!")

End If

 

Technically, they would serve the exact same function, yes? But the difference is that we're creating an additional object that needs to be managed. That takes up more resources, yes. I'm not sure whether or not there's been any research done on the cost expenditure of these resources, but I would expect them to be negligible.

 

I would prefer the latter method of writing code to the former. Why? Because I like compartmentalization and encapsulation, and separating things out into pieces. I also like for my codelines to be shorter horizontally, though it may cause the code to have more overall lines. I just find it easier to read. For another example, take this nested method:

 

Dim dInput As Decimal = Math.Round(Convert.ToDecimal("45.069023"), 2)

 

So, that'll convert the string into a decimal, and then round it off to two decimal places. But I hate this. I would prefer the following code:

 

Dim sInput As String = "45.069023"

Dim dInput As Decimal = 0.0

Decimal.TryParse(sInput, dInput)

If dInput <> 0.0 Then

    dInput = Math.Round(dInput, 2)

End If

 

Why? Multiple reasons:

 

  1. It doesn't go halfway across the screen horizontally, which is more pleasing to my eyes
  2. It gives me more possible breakpoints for debugging in case something goes wrong
  3. It gives me more places to put Watch Points in Visual Studio to watch for the value of the variables as they go through the different methods, bit by bit

 

Yes, I'm aware that I'm creating an additional unnecessary variable that takes up a little bit more memory. Yes, I'm aware that I'm creating additional lines of what could be considered unnecessary code, but I like having the extra debug space to work around in, and I think it makes it easier to solve problems with code when it's broken down like this.

 

So, what say you? Do you agree with me, or do you think it's just extra overhead that's not necessary? Why?


Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>