Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Bool expression??

Bool expression??

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharplinqfunctionalquestion
24 Posts 13 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Erich Ledesma

    I found this code somewhere:

    ...
    if (x == true)
    return true ;
    else
    return false ;

    Actual code was the body of a C# 3 lambda expression. Anyway, horrible.

    A Offline
    A Offline
    AEternal
    wrote on last edited by
    #3

    It's not THAT horrible. Yes, it's not exactly elegant, but sometimes this is done to make things clearer to other coders (especially less-experienced ones).

    P C E D 4 Replies Last reply
    0
    • A AEternal

      It's not THAT horrible. Yes, it's not exactly elegant, but sometimes this is done to make things clearer to other coders (especially less-experienced ones).

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #4

      AEternal wrote:

      sometimes this is done to make things clearer to other coders (especially less-experienced ones).

      But then how would they learn?

      B 1 Reply Last reply
      0
      • P PIEBALDconsult

        AEternal wrote:

        sometimes this is done to make things clearer to other coders (especially less-experienced ones).

        But then how would they learn?

        B Offline
        B Offline
        BadKarma
        wrote on last edited by
        #5

        The senior programmer could place comments like this:

        if (x == true)
          return true;
        else
          return false;
        
        //  the previous snippet could be replaced with
        //  return x;
        //  but i have chosen not to use this so that junior-programmers
        //  can understand how two different values can be returned by one single function
        //  
        //  To other senior programmers. Please leave the previous snippet or replace it by
        //  another snippet that would clarify the problem even better
        

        codito ergo sum

        A P M 3 Replies Last reply
        0
        • B BadKarma

          The senior programmer could place comments like this:

          if (x == true)
            return true;
          else
            return false;
          
          //  the previous snippet could be replaced with
          //  return x;
          //  but i have chosen not to use this so that junior-programmers
          //  can understand how two different values can be returned by one single function
          //  
          //  To other senior programmers. Please leave the previous snippet or replace it by
          //  another snippet that would clarify the problem even better
          

          codito ergo sum

          A Offline
          A Offline
          AEternal
          wrote on last edited by
          #6

          Fair enough. :)

          1 Reply Last reply
          0
          • E Erich Ledesma

            I found this code somewhere:

            ...
            if (x == true)
            return true ;
            else
            return false ;

            Actual code was the body of a C# 3 lambda expression. Anyway, horrible.

            P Offline
            P Offline
            Paddy Boyd
            wrote on last edited by
            #7

            What is x declared as? It's not a var is it?

            L 1 Reply Last reply
            0
            • P Paddy Boyd

              What is x declared as? It's not a var is it?

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #8

              Good catch! ;P Here could be trying (although not recommended) for "object == true".

              xacc.ide - now with IronScheme support
              IronScheme - 1.0 alpha 1 out now

              1 Reply Last reply
              0
              • A AEternal

                It's not THAT horrible. Yes, it's not exactly elegant, but sometimes this is done to make things clearer to other coders (especially less-experienced ones).

                C Offline
                C Offline
                codemunch
                wrote on last edited by
                #9

                It IS that horrible when you join a project and the other "experienced" consultant is writing a pile of redundant crap like that: if(x == true) { control.Enabled = true; } else { control.Enabled = false; } The above adds an extra comparison operation in addition to being ugly. Besides, as someone already mentioned, How will the juniors ever progress? Forgiveable for the OP though :) The project i joined has a metric buttload of that garbage. Even worse is these guys "architected" classes without methods (can you say pre-OOP structures boys & girls?) and then created a function library (which they called a "fascade" and is NOTHING like the fascade design pattern) where you pass in an instance of this methodless "class" to the method of a fascade class to do work on it (i.e. persist it). The reasoning? They thought adding methods to the class would make it too slow. It has been a rollercoaster of emoticons. :omg: :wtf: :mad: :sigh: :(( :rolleyes: e.g. class Person { public string FirstName; public string LastName; } class PersonFascade { public void InsertPerson(Person p) { /* do stuff */ } public void UpdatePerson(Person p) { /* do stuff */ } public void DeletePerson(Person p) { /* do stuff */ } }

                T 1 Reply Last reply
                0
                • B BadKarma

                  The senior programmer could place comments like this:

                  if (x == true)
                    return true;
                  else
                    return false;
                  
                  //  the previous snippet could be replaced with
                  //  return x;
                  //  but i have chosen not to use this so that junior-programmers
                  //  can understand how two different values can be returned by one single function
                  //  
                  //  To other senior programmers. Please leave the previous snippet or replace it by
                  //  another snippet that would clarify the problem even better
                  

                  codito ergo sum

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #10

                  Then it should be

                  return x ;

                  // If I ever catch any of you writing it as
                  //
                  // if (x == true)
                  // return true;
                  // else
                  // return false;
                  //
                  // I'll have you tied to an anthill and covered in honey!

                  V 1 Reply Last reply
                  0
                  • A AEternal

                    It's not THAT horrible. Yes, it's not exactly elegant, but sometimes this is done to make things clearer to other coders (especially less-experienced ones).

                    E Offline
                    E Offline
                    Erich Ledesma
                    wrote on last edited by
                    #11

                    Great point. Never thought of it that way. But it wasn't a sennior programmer making things clearer, nor a junnior one making misstakes. It's just some guy who has been programming for a while and I guess has no clue how boolean expressions work. It's started on C# by the way. Original code

                    x => if (SomeBoolMeth(x) == true) return true; return false ;

                    If you were a professor and find this in some test, not from a basic course, what would you do?? Again: Great point!!

                    1 Reply Last reply
                    0
                    • E Erich Ledesma

                      I found this code somewhere:

                      ...
                      if (x == true)
                      return true ;
                      else
                      return false ;

                      Actual code was the body of a C# 3 lambda expression. Anyway, horrible.

                      S Offline
                      S Offline
                      Sean Williams 0
                      wrote on last edited by
                      #12

                      what if x was a nullable boolean ? static bool getX(Nullable x) { if(x==true) { return true; } else { return false; } }

                      S 1 Reply Last reply
                      0
                      • S Sean Williams 0

                        what if x was a nullable boolean ? static bool getX(Nullable x) { if(x==true) { return true; } else { return false; } }

                        S Offline
                        S Offline
                        Sean Williams 0
                        wrote on last edited by
                        #13

                        that didn't quite come out right ( first post :P ) was supposed to be a bool after the Nullable of course static bool getX(Nullable x)

                        S 1 Reply Last reply
                        0
                        • S Sean Williams 0

                          that didn't quite come out right ( first post :P ) was supposed to be a bool after the Nullable of course static bool getX(Nullable x)

                          S Offline
                          S Offline
                          Sean Williams 0
                          wrote on last edited by
                          #14

                          uggh, ok last attempt I am sure you follow .. ;)

                          Member 4364689 wrote:

                          static bool getX(Nullable<bool> x)

                          D 1 Reply Last reply
                          0
                          • C codemunch

                            It IS that horrible when you join a project and the other "experienced" consultant is writing a pile of redundant crap like that: if(x == true) { control.Enabled = true; } else { control.Enabled = false; } The above adds an extra comparison operation in addition to being ugly. Besides, as someone already mentioned, How will the juniors ever progress? Forgiveable for the OP though :) The project i joined has a metric buttload of that garbage. Even worse is these guys "architected" classes without methods (can you say pre-OOP structures boys & girls?) and then created a function library (which they called a "fascade" and is NOTHING like the fascade design pattern) where you pass in an instance of this methodless "class" to the method of a fascade class to do work on it (i.e. persist it). The reasoning? They thought adding methods to the class would make it too slow. It has been a rollercoaster of emoticons. :omg: :wtf: :mad: :sigh: :(( :rolleyes: e.g. class Person { public string FirstName; public string LastName; } class PersonFascade { public void InsertPerson(Person p) { /* do stuff */ } public void UpdatePerson(Person p) { /* do stuff */ } public void DeletePerson(Person p) { /* do stuff */ } }

                            T Offline
                            T Offline
                            Tristan Rhodes
                            wrote on last edited by
                            #15

                            codemunch wrote:

                            class Person { public string FirstName; public string LastName; } class PersonFascade { public void InsertPerson(Person p) { /* do stuff */ } public void UpdatePerson(Person p) { /* do stuff */ } public void DeletePerson(Person p) { /* do stuff */ } }

                            Isn't your example standard practice? Person = Business Object PersonFacade = Database Wrapper You get exactly the same when you generate any DB Mapping using an ORM system, and i see nothing wrong with it. I always operate on the aim to separate data structure from functionality. That said, the naming conventions are misleading.

                            ------------------------------- Carrier Bags - 21st Century Tumbleweed.

                            C 1 Reply Last reply
                            0
                            • A AEternal

                              It's not THAT horrible. Yes, it's not exactly elegant, but sometimes this is done to make things clearer to other coders (especially less-experienced ones).

                              D Offline
                              D Offline
                              dojohansen
                              wrote on last edited by
                              #16

                              Either you've understood that "(x < 5)" is a boolean expression every bit as much as the boolean constants "true" and "false", or you haven't. There's nothing in between. The code makes it look as if the condition is somehow not a boolean expression, and therefore highlights the author's lack of understanding, of something quite fundamental, and therefore deserves to be on display as a coding horror, in my opinion. For some reason I see this done a lot with ternaries in C#: return (s == "toto"? true : false); Unfortunately proper use is less commonly seen, such as return (row != null? (int)row["count"] : -1);

                              1 Reply Last reply
                              0
                              • S Sean Williams 0

                                uggh, ok last attempt I am sure you follow .. ;)

                                Member 4364689 wrote:

                                static bool getX(Nullable<bool> x)

                                D Offline
                                D Offline
                                dojohansen
                                wrote on last edited by
                                #17

                                It still makes no difference. An if condition is a boolean expression and can only evaluate to true or false, regardless of whether the equals operator is working with operands that are structures (such as Nullable) or objects (such as the object == true mentioned above) or anything else. It is of course true that the expression "x" cannot in general be known to be equivalent to "x == true", but you can ALWAYS rewrite if (x == true) return true; else return false; to just return (x == true); And of course, if x is in fact a bool, it would be clearer to simply return x.

                                1 Reply Last reply
                                0
                                • T Tristan Rhodes

                                  codemunch wrote:

                                  class Person { public string FirstName; public string LastName; } class PersonFascade { public void InsertPerson(Person p) { /* do stuff */ } public void UpdatePerson(Person p) { /* do stuff */ } public void DeletePerson(Person p) { /* do stuff */ } }

                                  Isn't your example standard practice? Person = Business Object PersonFacade = Database Wrapper You get exactly the same when you generate any DB Mapping using an ORM system, and i see nothing wrong with it. I always operate on the aim to separate data structure from functionality. That said, the naming conventions are misleading.

                                  ------------------------------- Carrier Bags - 21st Century Tumbleweed.

                                  C Offline
                                  C Offline
                                  codemunch
                                  wrote on last edited by
                                  #18

                                  Well, the example wasn't too detailed but as-is it took OOP and bastardized it into a function library (PersonFacade) + simple data structures (Person). You do want your persistance layer (data) to be separate but working at the business layer (often from a different class) and calling data layer specific persistance methods is ugly not to mention a pain in the ass to manage. A much cleaner solution is to have a Save() method (and depending on your scheme, a Delete()method) which then passes itself to a data layer so in your business & UI logic you don't need to call data layer specific methods residing in a completely different class. The business object takes care of itself and your code is better organized. The data layer could look like the "facade" thing or could be a smarter all-encompasing gate keeper.

                                  1 Reply Last reply
                                  0
                                  • B BadKarma

                                    The senior programmer could place comments like this:

                                    if (x == true)
                                      return true;
                                    else
                                      return false;
                                    
                                    //  the previous snippet could be replaced with
                                    //  return x;
                                    //  but i have chosen not to use this so that junior-programmers
                                    //  can understand how two different values can be returned by one single function
                                    //  
                                    //  To other senior programmers. Please leave the previous snippet or replace it by
                                    //  another snippet that would clarify the problem even better
                                    

                                    codito ergo sum

                                    M Offline
                                    M Offline
                                    Michael P Scherer
                                    wrote on last edited by
                                    #19

                                    or he could just save the time it takes to write that long, verbose comment and just write return x;

                                    SoCoder - CAT Program

                                    B 1 Reply Last reply
                                    0
                                    • M Michael P Scherer

                                      or he could just save the time it takes to write that long, verbose comment and just write return x;

                                      SoCoder - CAT Program

                                      B Offline
                                      B Offline
                                      BadKarma
                                      wrote on last edited by
                                      #20

                                      No time or effort should be spared in the process of education of the younger generation. Even better would be.

                                      return (x == true) ? true : false;
                                      

                                      ;P

                                      codito ergo sum

                                      V 1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        Then it should be

                                        return x ;

                                        // If I ever catch any of you writing it as
                                        //
                                        // if (x == true)
                                        // return true;
                                        // else
                                        // return false;
                                        //
                                        // I'll have you tied to an anthill and covered in honey!

                                        V Offline
                                        V Offline
                                        Vasudevan Deepak Kumar
                                        wrote on last edited by
                                        #21

                                        PIEBALDconsult wrote:

                                        I'll have you tied to an anthill and covered in honey!

                                        Cute. This should get a Vote of '5'.

                                        Vasudevan Deepak Kumar Personal Homepage
                                        Tech Gossips
                                        A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

                                        1 Reply Last reply
                                        0
                                        • B BadKarma

                                          No time or effort should be spared in the process of education of the younger generation. Even better would be.

                                          return (x == true) ? true : false;
                                          

                                          ;P

                                          codito ergo sum

                                          V Offline
                                          V Offline
                                          Vasudevan Deepak Kumar
                                          wrote on last edited by
                                          #22

                                          With longer conditions and expressions, ternary operator becomes tedious to debug. X|

                                          Vasudevan Deepak Kumar Personal Homepage
                                          Tech Gossips
                                          A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups