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 498373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:52:22+00:00 2026-05-13T05:52:22+00:00

The following example is clearly fictional but it resumes how validation is done on

  • 0

The following example is clearly fictional but it resumes how validation is done on a code base I’m working with.

TypeA has two methods, with the following signatures:

public void FirstMethod(TypeB param)
public ValidationResult TryFirstMethod(TypeB param)

When FirstMethod is called it needs to perform validation on the parameter.
Hence it calls TryFirstMethod to retrieve an object representing the validation’s result.
If the instance of ValidationResult says that everything’s OK the execution continues otherwise an exception is thrown.

The utility of TryFirstMethod is that the caller, an hypothetical TypeC, can execute this method and check if the actual method would throw. By examining a property on ValidationResult.
Also, the instance of ValidationResult contains information on why the input was wrong, how to fix it and so on.
This justifies the need for this type instead of just, say, using a boolean.

In practice this works fairly well, it’s quite easy to validate data and to return localized error messages to the user.

The only issue is that since certain checks are quite complex performing them two times becomes a little expensive.
They are initially done by the caller to make sure that the actual method won’t throw.
And then again by the method in question to check that the input is valid.

I can’t find a clean way to avoid having to perform the checks two times.
To make things more complex there is the fact that the solution should work with standard methods, constructors and also when TypeB is a “primitive” type like string or int.

  • 1 1 Answer
  • 1 View
  • 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-13T05:52:22+00:00Added an answer on May 13, 2026 at 5:52 am

    It sounds like your core logic is something like this:

    1. Validate TypeB (call TryFirstMethod).
    2. If not valid, display error message.
    3. If valid (and, presumably, other members are valid as well) then to perform operation on the data (as part of FirstMethod?).
    4. The data operation (FirstMethod) re-validates the input.

    If this is (somewhat) correct, you should be able to change your design into something like:

    1. Call FirstMethod.
    2. Validate data.
    3. If invalid, throw validation exception.
    4. If valid, perform data operation.

    Your UI would then call FirstMethod and expect that the operation was successful unless an exception was thrown, in which case you’d display your validation messages as you do currently. With this design you only perform the validation a single time.

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

Sidebar

Related Questions

I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
I just inherited a project that has code similar to the following (rather simple)
You'll appreciate the following two syntactic sugars: lock(obj) { //Code } same as: Monitor.Enter(obj)
I have the following contrived example (coming from real code): template <class T> class
I have found the following example code (somewhere on the web, can't remember where):
In following example: Line1 <br /> Line2 I'm using <br /> to force Line2
The following example will not compile for me: #include <iostream> #include <functional> #include <string>
Example: Suppose in the following example I want to match strings that do not
I find the following example mildly surprising: >>> class Foo: def blah(self): pass >>>
What happens in the following example? struct B { }; struct D1 : B

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.