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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:37:00+00:00 2026-06-12T05:37:00+00:00

Here I have a sample of code. I would like to know other way

  • 0

Here I have a sample of code.

I would like to know other way to write this code; specially I would like to know other forms for the nested if.

Also I would like your opinion about commenting (what are the best practice, is my style of commenting sufficient in professional environment?)

If this question is out of scope for SO please let me know, I will remove it please do not down vote I just try to learn about programming ;-).

// Has User expressed his preference?
if (!repositoryDevice.HasDevicePreference(userDevice))   // If not ...
{
    // Save the preference
    repositoryPreference.Add(userDevice.UserId, candidateId);
    result = true;
}
else   // If yes ... 
{
    // If a User has express his preference more than 1 hour ago
    // allow the User to change his preference, otherwise not
    if (!HasUserRecentPreference(userDevice))
    {
        repositoryPreference.Add(userDevice.UserId, candidateId);
        result = true;
    }
}
  • 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-06-12T05:37:02+00:00Added an answer on June 12, 2026 at 5:37 am

    You can put else and if on the same line:

        result = true;
    
        // Has User expressed his preference or 
        // if a User has express his preference more than 1 hour ago
        // allow the User to change his preference
        if (!repositoryDevice.HasDevicePreference(userDevice) ||
            !HasUserRecentPreference(userDevice))
        {
            // Save the preference
            repositoryPreference.Add(userDevice.UserId, candidateId);
        }
        else
        {
            // result = false?
        }
    

    It is also possible (as someone else pointed out) to use the &&/|| operators to merge the two conditions into a single block of code. Assuming that the code in the two blocks are identical, that is of course the preferred solution: never write the same line twice.

    Otherwise, it looks very much good enough! 🙂 It’s generally considered a good idea to avoid source code complexity.

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

Sidebar

Related Questions

I am using the sample provided here and have used the code inside a
I have a bare-bones sample project here: http://dl.dropbox.com/u/7834263/ExpandingCells.zip In this project, a UITableView has
I have read some other articles like here and here but unfortunately there are
Very simple thing I'm trying to do here. I would like to have 2
I would like to write a Web Application that would have two buttons and
Here is the super simple code i have: HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.OptionWriteEmptyNodes
I have a simple situation here. lets face html code first => <form name=geoKey
enter code here Hi All, I have a simple windows service application that connects
Here's the code for a simple page I have that is not working: <head>
I have -- what I think -- is a simple question. Here's my 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.