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

  • Home
  • SEARCH
  • 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 8810471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:07:31+00:00 2026-06-14T03:07:31+00:00

Consider this function, which you can think of as a truth table: public Foo

  • 0

Consider this function, which you can think of as a truth table:

public Foo doSomething(bool a, bool b) {

       if ( a &&  b) return doAB();
  else if ( a && !b) return doA();
  else if (!a &&  b) return doB();
  else if (!a && !b) return doNotANotB();

  else throw new Exception("Well done, you defeated boolean logic!");
}

The compiler insists on that last else clause. But from a truth table’s perspective, that is an impossible state.

Yes, it works, and yes, I can live with it. But I’m wondering if there is some mechanism in c# to avoid this sort of code, or if I’ve missed something obvious?

UPDATE:
For bonus points, and purely out of curiosity, are there any languages that deal with this sort of thing differently? Maybe it’s not a language matter, but rather one of a smart compiler (but the edge cases would be unimaginably complicated I suppose).

  • 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-14T03:07:32+00:00Added an answer on June 14, 2026 at 3:07 am

    Considering the truth table, the last condition is entirely superfluos. It can be dropped without altering the logic of your program, like this:

    public MyType doSomething(bool a, bool b) {
    
          if ( a &&  b) return doAB();
    else  if ( a && !b) return doA();
    else  if (!a &&  b) return doB();
    else/*if (!a && !b)*/ return doNotANotB();
    }
    

    Now you have a final catch-all if, and your compiler is happy. You don’t have to remove the condition altogether – I often find it a good idea to keep it around in a comment for readability.

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

Sidebar

Related Questions

Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
Consider: function Shape() { this.name = "Generic"; this.draw = function() { return "Drawing "
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Consider the following Java function: public void foo(Class<? extends Exception> cl, List<? extends Exception>
Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()
Consider the following function: public function foo(bar1:int, bar2:uint, bar3:String, bar4:Boolean):void{} What I want is
Consider this (rather pointless) javascript code: function make_closure() { var x = 123, y
Consider this code: var img = new Image(); img.onload = function() { console.log(this.width); };

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.