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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:47:46+00:00 2026-05-26T14:47:46+00:00

I have a piece of code, which I am not sure how to refactor..

  • 0

I have a piece of code, which I am not sure how to refactor.. It is not very readable and I would like to make it readable. Here is a the problem

There are two columns in database which can be either NULL, 0 or have a value each. On the web page there is a checkbox – enable and text box – value for each of those two columns.

x = checkbox1
z = textbox1
y = checkbox2
w = textbox2

The logic is if both the checkboxes are not selected, then both the values should be 0. If either one is selected and other is not, then others value should be NULL. and for the one that is selected, if the textbox is empty its value should be NULL else should be the value in the textbox

if{x}
{
   if(z)
   {
      a = NULL;
   }
   else
   {
      a = z;
   }
   if(y)
   {
      if(w)
      {
          b=w;
      }
      else
      {
          b = NULL;
      }
   }
   else
   {
      b = null
   }
}
else
{
   if(y)
   {
      a = NULL;
      if(w)
      {
          b=w;
      }
      else
      {
          b = NULL;
      }
   }
   else
   {
      a = 0;
      b = 0;
   }
}

Trust me this is a valid scenario. Let me know if this makes sense or I should give more information

  • 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-26T14:47:46+00:00Added an answer on May 26, 2026 at 2:47 pm

    Using some logical ands and nots, we get something more readable.
    We can save a little by defaulting to NULL (thus not needing to set the other to NULL). We can also save by putting the code for checking if a textbox is set or using null into a little function.

    In pseudo code:

    a = NULL
    b = NULL
    if (not checkbox1) and (not checkbox2):
      a = 0
      b = 0
    if (checkbox1):
      a = valueornull(textbox1)
    if (checkbox2):
      b = valueornull(textbox2)
    
    
    function valueornull(textbox):
      if textbox value:
        return value
      else:
        return null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of fortran code, and I am not sure which standard
I have a piece of code which looks like this: Snippet A: class Creature
I have this piece of code which I'm hoping will be able to tell
I am somewhat new to R, and i have this piece of code which
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have a piece of code with a) which I replaced with b) purely
I have a little piece of rails code which lets users enter a review.
I have a piece of code in ANSI C which uses the time.h library
I have a piece of code looking like this : TAxis *axis = 0;
I have few questions connected with struct and typedef, there is piece of code

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.