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

The Archive Base Latest Questions

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

I have various classes for handling form data and querying a database. I need

  • 0

I have various classes for handling form data and querying a database. I need some advice on reducing the amount of code I write from site to site.

The following code is for handling a form posted via ajax to the server. It simply instantiates a Form class, validates the data and processes any errors:

public static string submit(Dictionary<string, string> d){

    Form f = new Form("myform");

    if (!f.validate(d)){

        return f.errors.toJSON();

    }

    //process form...

}

Is there a way to reduce this down to 1 line as follows:

if (!Form.validate("myform", d)){ return Form.errors.toJSON(); }
  • 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-13T17:30:20+00:00Added an answer on May 13, 2026 at 5:30 pm

    Let’s break that down into two questions.

    1) Can I write the existing logic all in one statement?

    The local variable has to be declared in its own statement, but the initializer doesn’t have to be there. It’s prefectly legal to say:

    Form f; 
    if (!(f=new Form("myform")).validate(d))return f.errors.toJSON(); 
    

    Why you would want to is beyond me; doing so is ugly, hard to debug, hard to understand, and hard to maintain. But it’s perfectly legal.

    2) Can I make this instance method into a static method?

    Probably not directly. Suppose you had two callers validating stuff on two different threads, both calling the static Form.Validate method, and both producing errors. Now you have a race. One of them is going to win and fill in Form.Errors. And now you have two threads reporting the same set of errors, but the errors are wrong for one of them.

    The better way to make this into a static method is to make the whole thing into a static method that has the desired semantics, as in plinth’s answer.

    Errors errors = Validator.Validate(d);
    if (errors != null) return errors.toJSON();
    

    Now the code is very clear, and the implementation of Validate is straightforward. Create a form, call the validator, either return null or the errors.

    I would suggest that you don’t need advice on reducing the amount of code you write. Rather, get advice on how to make the code read more like the meaning it intends to represent. Sometimes that means writing slightly more code, but that code is clear and easy to understand.

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

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Solution files are pretty simple text files. Open one with… May 14, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer You said you understand this part, but just to emphasize,… May 14, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer function createCategoryComplete(e) { var obj = e.get_object(); alert(obj.CategoryId + '… May 14, 2026 at 7:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.