Small width layout Medium width layout Maximum width layout Small text Medium text Large text
     Search
Downloads Downloads Directory Directory Forums Forums Forge Forge Blogs Blogs        Marketplace Marketplace Careers Program Careers
Community › Forums Register  |  

Maximum ASP
  Ads  
 


  Sponsors  

Meet Our Sponsors

FCKeditor Project
Salaro -- Skins and more
OnyakTech
CrystalTech Web Hosting™
Webhost4life, specialists in DNN hosting
Mad Development is a full service interactive agency focusing on the merge of design, technology, e-commerce, and affiliate marketing by providing total website solutions.
 


DotNetNuke Forums
 
  Forum  General DotNetN...  Extend It! ( Pr...  Try Catch is not a replacement for programming logic!
Previous Previous
 
Next Next
New Post 10/8/2007 5:01 PM
User is offline John Mitchell
3867 posts
www.snapsis.com
4th Ranked




Try Catch is not a replacement for programming logic!  

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"))


 
New Post 10/8/2007 7:12 PM
User is offline William Severance
842 posts
www.wesnetdesigns.com
7th Ranked






Re: Try Catch is not a replacement for programming logic!  

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
 
New Post 10/8/2007 9:15 PM
User is offline John Mitchell
3867 posts
www.snapsis.com
4th Ranked




Re: Try Catch is not a replacement for programming logic!  

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.


 
Previous Previous
 
Next Next
  Forum  General DotNetN...  Extend It! ( Pr...  Try Catch is not a replacement for programming logic!
 


Forum Policy

These Discussion Forums are dedicated to the discussion of the DotNetNuke Web Application Framework.

For the benefit of the community and to protect the integrity of the project, 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 DotNetNuke.
2. Discussion or promotion of DotNetNuke product releases under a different brand name are strictly prohibited.
3. No Flaming or Trolling.
4. No Profanity, Racism, or Prejudice.
5. Site Moderators have the final word on approving/removing a thread or post or comment.
6. English language posting only, please.

 


DNN Photo Gallery
DNN Photo Gallery is a truly unique photo management module released January 1st 2006. With DNN Photo Gallery you can REALLY integrate images into your existing portal and make them look like they were designed for your site.
DNN Photo Gallery
Swirlhost Inc.
Affordable DotNetNuke Hosting, Skin Development, Custom Module Development, and DotNetNuke Consulting. We will install your preference of DNN and now host with us and get a free license for the Swirl AJAX Chatroom Module.
www.swirlhost.com
Active Modules, Inc.
Creators of Active Forums, the best forum module for DotNetNuke
www.activemodules.com

DotNetNuke Corporation   Terms Of Use  Privacy Statement
DotNetNuke®, DNN®, and the DotNetNuke logo are trademarks of DotNetNuke Corporation
Hosted by MaximumASP