Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 529595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:06:30+00:00 2026-05-13T09:06:30+00:00

I got a rather big class library that contains a lot of code. I

  • 0

I got a rather big class library that contains a lot of code.

I am looking at how to optimize the performance of some of the code, and for some rather simple utility methods I’ve found that the parameter validation occupies a rather large portion of the runtime for some core methods.

Let me give a typical example:

  1. A.MethodA1 runs a loop, iterating over a collection, calling B.MethodB1 for each element
  2. B.MethodB1 processes the element and returns the result, it’s a rather basic calculation, but since it is used many places, it has been put into its own method instead of being copied and pasted where needed
  3. A.MethodA1 calls C.MethodC1 with the results of B.MethodB1, and puts the result into a list that is returned at the end of the loop

In the case I’ve found now, B.MethodB1 does rudimentary parameter validation. Since the method calls other internal methods, I’d like to avoid having NullReferenceExceptions several layers deep into the code, and rather fail early, hence B.MethodB1 validates the parameters, like checking for null and some basic range checks on another parameter.

However, in this particular call scenario, it is impossible (due to other program logic) for these parameters to ever have the wrong values. If they had, from the program standpoint, B.MethodB1 would never be called at all for those values, A.MethodA1 would fail before the call to B.MethodB1.

So I was considering removing the parameter validation in B.MethodB1, since it occupies roughly 65% of the method runtime (and this is part of some heavily used code.)

However, B.MethodB1 is a public method, and can thus be called from the program, in which case I want the parameter validation.

So how would you solve this dilemma?

  1. Keep the parameter validation, and take the performance hit
  2. Remove the parameter validation, and have potentially fail-late problems in the method
  3. Split the method into two, one internal that doesn’t have parameter validation, called by the “safe” path, and one public that has the parameter validation + a call to the internal version.

The latter one would give me the benefits of having no parameter validation, while still exposing a public entrypoint which does have parameter validation, but for some reason it doesn’t sit right with me.

Opinions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:06:31+00:00Added an answer on May 13, 2026 at 9:06 am

    I would go with option 3. I tend to use assertions for private and internal methods and do all the validation in public methods.

    By the way, is the performance hit really that big?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some rather complex forms, that need to be configured by web designers.
i've got a site with a lot of referenced .js-Files; those are rather small
Got a class that serializes into xml with XMLEncoder nicely with all the variables
I'm implementing a cache in a class library that i'm using in an asp.net
I recent noticed, that when I start our rather big application, there are allways
Rather than have everything in one big dialog, I'm looking at being able to
I've got a rather complicated custom control that would boggle your mind if I
I've got a rather tricky problem that I've been trying to solve for the
I've got an applet that is rather large (4MB) on a web page used
I have just got the following bloody MDA exception on a rather simple little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.