Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Try Catch is not a replacement for programming logic! Try Catch is not a replacement for programming logic!
Previous
 
Next
New Post
10/8/2007 7:01 PM
 

If you see code like this that has a Try/Catch block without actually handling the Exception then you have an opportunity to not only increase performance in your code, but also an opportunity to make debugging easier on other developers.

This is NOT best practice:

Try
     _Enabled = Boolean.Parse(Convert.ToString(ModuleSettings("DNN_Enabled"))) 
    _UseCaptcha = Boolean.Parse(Convert.ToString(ModuleSettings("DNN_UseCaptcha"))) 
Catch

End Try 

This is much better:
     _Enabled = Convert.ToBoolean(ModuleSettings("DNN_Enabled")) 
     _UseCaptcha = Convert.ToBoolean(ModuleSettings("DNN_UseCaptcha"))


DotNetNuke Modules from Snapsis.com
 
New Post
10/8/2007 9:12 PM
 

While I agree that it is very bad practice to use a Try/Catch block (particularly with an empty Catch) in this case or any case as a way of controlling non-exceptional program flow, please note that it is possible for ModuleSettings or TabModuleSettings to return null if the setting has not been previously defined - as is often the case for a newly added module whose Settings page has not been opened previously.  For this latter reason, I always initialize a module's settings to default values when the module is first added to a page.  Thanks to the introduction of generics in .NET 2.0, it is also possible to define a function like the following in your module's code (or better yet in a base class deriving from PortalModuleBase that can contain other methods common to all modules you develop):

Private Function GetSetting(Of T)(ByVal SettingName As String, ByVal DefaultValue As T) As T
   Dim obj As Object = Settings(SettingName)
   If obj Is Nothing Then
       Return DefaultValue
   Else
       Return Convert.ChangeType(obj, GetType(T))          'or CType(obj, T) - not sure which is better performance
   End If
End Function

Any thoughts on the performance implications of using such a function or of always initializing all of a module's settings to default values when the module is first added to a page?


Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
New Post
10/8/2007 11:15 PM
 

Hi Bill,

Yes, I understand that sometimes the object can be uninitialized, but using Convert.ToBoolean will return false without causing an exception in this case.  I also agree that it is much better to insure values are initialized and the earlier the better.  I don't think there is ever a good reason to use Try Catch to handle uninitialized values,  even if the object needs to be checked for Is Nothing first and assigned a default value at that time. 

I like the generic GetSetting, and I thing it will perform much better than throwing exceptions all over the place with the added benefit of maintainability.

Thanks for responding, I think it is great to hear other ideas about best practices.


DotNetNuke Modules from Snapsis.com
 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Try Catch is not a replacement for programming logic! Try Catch is not a replacement for programming logic!


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out