Can I use boolean algebra to reduce the number of lines in my code?












1















I am recently studying computer science and I was introduced into boolean algebra. It seems that boolean algebra is used to simplify logic gates in hardware in order to make the circuit design minimal and thus cheaper. Is there any similar way that you can use it to reduce the number of code lines in your software in higher level languages like C++, C# or any other language?










share|improve this question









New contributor




themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    1















    I am recently studying computer science and I was introduced into boolean algebra. It seems that boolean algebra is used to simplify logic gates in hardware in order to make the circuit design minimal and thus cheaper. Is there any similar way that you can use it to reduce the number of code lines in your software in higher level languages like C++, C# or any other language?










    share|improve this question









    New contributor




    themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1








      I am recently studying computer science and I was introduced into boolean algebra. It seems that boolean algebra is used to simplify logic gates in hardware in order to make the circuit design minimal and thus cheaper. Is there any similar way that you can use it to reduce the number of code lines in your software in higher level languages like C++, C# or any other language?










      share|improve this question









      New contributor




      themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I am recently studying computer science and I was introduced into boolean algebra. It seems that boolean algebra is used to simplify logic gates in hardware in order to make the circuit design minimal and thus cheaper. Is there any similar way that you can use it to reduce the number of code lines in your software in higher level languages like C++, C# or any other language?







      boolean boolean-logic






      share|improve this question









      New contributor




      themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 hours ago









      Doc Brown

      131k23241380




      131k23241380






      New contributor




      themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 4 hours ago









      themisthemis

      1114




      1114




      New contributor




      themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      themis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          6 Answers
          6






          active

          oldest

          votes


















          5














          You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, substracting or multiplying numbers. It is a multi-purpose tool, not just a tool for reducing the number of code lines in a program (as well as it is not a tool for just simplifying logic gates in hardware). However, it can sometimes be used for such cases (as well as for the opposite, or for completely different purposes).



          For example, if your program contains an overly complicated boolean expression or sequence of conditionals, boolean algebra might help you to simplify the expression and the surrounding code. But that does not necessarily leads to less lines of code. In fact, sometimes complex one-line code snippets get more maintainable when you split them up into several lines of code, and boolean algebra can help you to do this correctly.



          So IMHO your question is like "can I use a pocket calculator to find the shortest route when traveling from A to B"? Sure you can, when you take a map with distance information for individual roads and use the calculator to add them up, and then pick the route with the smallest sum. But you could also use the calculator for finding longer routes, or for calculating completely different things.






          share|improve this answer

































            1














            Boolean algebra is logic reduced to its most basic form. It maps nicely to a binary counting system, the second most basic imaginable counting system.




            it seems that boolean algebra is used to simplify logic gates in hardware




            Not quite. It makes it possible to implement any kind of logic in hardware in the first place. Once you have that, you can use software to build on that and create more complex logic again.



            So no, you do not reduce the number of code lines using boolean algebra. You use it whenever there is a binary choice to be made. If your problem is complex, you are going to have a lot of choices in your software solution.



            So it is the other way around: simple logic requires less boolean algebra, complex logic requires more. And more logic == more code.






            share|improve this answer































              1














              Well, OK. The reason electronics engineers want to reduce the number of logic gates in a circuit is probably a matter of construction cost and operational performance of their final product. They are building the very thing that will do the work in the end.



              When you are programming in any language other than assembly, the computer in the end will not execute your code, but a transformed version of it. This transformation from the programmer friendly language to the computer friendly language may be called «compilation». Boolean logic is indeed used by the compiler to achieve operational efficiency.



              You may code in assembly and do the compiler's job yourself, but on a day-to-day basis, it becomes less and less worth the hassle. Modern compilers know more and more of the tricks, and better than you.



              On the contrary, if you ask whether there exist a similar practice in higher-level languages, yes, it's called «refactoring», which means applying well-defined transformations to code chat change the structure and not the behaviour. In the 90s, Martin Fowler published a book that is a catalog of probably the most recurring basic refactorings in object languages.






              share|improve this answer































                1














                Of course you can use Boolean Algebra for different cases in your project. This technique allows to get the same output with less steps and components when designing a circuit.



                However in case of High-level languages it may result in more problems. HLL languages are designed for readability and ease of understanding. You can simplify your several lines of code into a single one, but it will become unreadable and complex, so much more time is required to grasp the idea of the code.
                Most of compilers perform such kind of optimizations by default.



                All in all, I suggest not to think a lot about reducing lines of code, but consider behavior of the program in runtime, memory usage and architectual design. Generally when you are applying Boolean Algebra to your circuits you are acting as a compiler for most of existing languages.






                share|improve this answer































                  1














                  Yes, you can. But should you?



                  Boolean algebra serves to reduce logical expressions to their minimal form, but whether this is good or bad is left to the programmer. Let's take this validation code:



                  ...
                  if (person.Money == 0) return;
                  if (person.Money != 0 && person.Age < 15) return;
                  if (person.Age > 90) return;
                  if (person.Children > 3) return;
                  if (person.HasWhiteShirt() && person.HasBlueSocks()) return;
                  ...


                  This is a list of checks on the object person, and will exit the function as soon as one of those is true. The meaning of that is pretty clear, and anyone that reads it, even a non-programmer, can understand what checks we're doing.



                  There are different ways to write that, though. Let's go for lines of code and simplify it, as you asked in your question, and see what happens:



                  if (... || person.Money == 0 || person.Age < 15 || person.Age > 90 || person.Children > 3 || (person.HasWhiteShirt() && person.HasBlueSocks()) || ...) return;


                  We now have a single line of unreadable conditionals, and by simplifying we also lost the info that that <15 age check is somehow related to money. Editing this code is gonna be way more difficult in the future, so reducing it was a mistake.



                  In conclusion: Always go for readability, regardless of the minimal form of a logical expression.






                  share|improve this answer































                    -5














                    Subject: Generating PDF after submitting the survey in Limesurvey ?
                    Dear all,
                    I would like to aks how can print out PDF after submitting the survey in Limesurvey ?
                    Could we use css code for this ?
                    Many thanks for your help!
                    Best regards.






                    share|improve this answer








                    New contributor




                    liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.




















                      Your Answer








                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "131"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: false,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: null,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: false,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });






                      themis is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f385783%2fcan-i-use-boolean-algebra-to-reduce-the-number-of-lines-in-my-code%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown




















                      StackExchange.ready(function () {
                      $("#show-editor-button input, #show-editor-button button").click(function () {
                      var showEditor = function() {
                      $("#show-editor-button").hide();
                      $("#post-form").removeClass("dno");
                      StackExchange.editor.finallyInit();
                      };

                      var useFancy = $(this).data('confirm-use-fancy');
                      if(useFancy == 'True') {
                      var popupTitle = $(this).data('confirm-fancy-title');
                      var popupBody = $(this).data('confirm-fancy-body');
                      var popupAccept = $(this).data('confirm-fancy-accept-button');

                      $(this).loadPopup({
                      url: '/post/self-answer-popup',
                      loaded: function(popup) {
                      var pTitle = $(popup).find('h2');
                      var pBody = $(popup).find('.popup-body');
                      var pSubmit = $(popup).find('.popup-submit');

                      pTitle.text(popupTitle);
                      pBody.html(popupBody);
                      pSubmit.val(popupAccept).click(showEditor);
                      }
                      })
                      } else{
                      var confirmText = $(this).data('confirm-text');
                      if (confirmText ? confirm(confirmText) : true) {
                      showEditor();
                      }
                      }
                      });
                      });






                      6 Answers
                      6






                      active

                      oldest

                      votes








                      6 Answers
                      6






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      5














                      You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, substracting or multiplying numbers. It is a multi-purpose tool, not just a tool for reducing the number of code lines in a program (as well as it is not a tool for just simplifying logic gates in hardware). However, it can sometimes be used for such cases (as well as for the opposite, or for completely different purposes).



                      For example, if your program contains an overly complicated boolean expression or sequence of conditionals, boolean algebra might help you to simplify the expression and the surrounding code. But that does not necessarily leads to less lines of code. In fact, sometimes complex one-line code snippets get more maintainable when you split them up into several lines of code, and boolean algebra can help you to do this correctly.



                      So IMHO your question is like "can I use a pocket calculator to find the shortest route when traveling from A to B"? Sure you can, when you take a map with distance information for individual roads and use the calculator to add them up, and then pick the route with the smallest sum. But you could also use the calculator for finding longer routes, or for calculating completely different things.






                      share|improve this answer






























                        5














                        You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, substracting or multiplying numbers. It is a multi-purpose tool, not just a tool for reducing the number of code lines in a program (as well as it is not a tool for just simplifying logic gates in hardware). However, it can sometimes be used for such cases (as well as for the opposite, or for completely different purposes).



                        For example, if your program contains an overly complicated boolean expression or sequence of conditionals, boolean algebra might help you to simplify the expression and the surrounding code. But that does not necessarily leads to less lines of code. In fact, sometimes complex one-line code snippets get more maintainable when you split them up into several lines of code, and boolean algebra can help you to do this correctly.



                        So IMHO your question is like "can I use a pocket calculator to find the shortest route when traveling from A to B"? Sure you can, when you take a map with distance information for individual roads and use the calculator to add them up, and then pick the route with the smallest sum. But you could also use the calculator for finding longer routes, or for calculating completely different things.






                        share|improve this answer




























                          5












                          5








                          5







                          You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, substracting or multiplying numbers. It is a multi-purpose tool, not just a tool for reducing the number of code lines in a program (as well as it is not a tool for just simplifying logic gates in hardware). However, it can sometimes be used for such cases (as well as for the opposite, or for completely different purposes).



                          For example, if your program contains an overly complicated boolean expression or sequence of conditionals, boolean algebra might help you to simplify the expression and the surrounding code. But that does not necessarily leads to less lines of code. In fact, sometimes complex one-line code snippets get more maintainable when you split them up into several lines of code, and boolean algebra can help you to do this correctly.



                          So IMHO your question is like "can I use a pocket calculator to find the shortest route when traveling from A to B"? Sure you can, when you take a map with distance information for individual roads and use the calculator to add them up, and then pick the route with the smallest sum. But you could also use the calculator for finding longer routes, or for calculating completely different things.






                          share|improve this answer















                          You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, substracting or multiplying numbers. It is a multi-purpose tool, not just a tool for reducing the number of code lines in a program (as well as it is not a tool for just simplifying logic gates in hardware). However, it can sometimes be used for such cases (as well as for the opposite, or for completely different purposes).



                          For example, if your program contains an overly complicated boolean expression or sequence of conditionals, boolean algebra might help you to simplify the expression and the surrounding code. But that does not necessarily leads to less lines of code. In fact, sometimes complex one-line code snippets get more maintainable when you split them up into several lines of code, and boolean algebra can help you to do this correctly.



                          So IMHO your question is like "can I use a pocket calculator to find the shortest route when traveling from A to B"? Sure you can, when you take a map with distance information for individual roads and use the calculator to add them up, and then pick the route with the smallest sum. But you could also use the calculator for finding longer routes, or for calculating completely different things.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 31 mins ago

























                          answered 2 hours ago









                          Doc BrownDoc Brown

                          131k23241380




                          131k23241380

























                              1














                              Boolean algebra is logic reduced to its most basic form. It maps nicely to a binary counting system, the second most basic imaginable counting system.




                              it seems that boolean algebra is used to simplify logic gates in hardware




                              Not quite. It makes it possible to implement any kind of logic in hardware in the first place. Once you have that, you can use software to build on that and create more complex logic again.



                              So no, you do not reduce the number of code lines using boolean algebra. You use it whenever there is a binary choice to be made. If your problem is complex, you are going to have a lot of choices in your software solution.



                              So it is the other way around: simple logic requires less boolean algebra, complex logic requires more. And more logic == more code.






                              share|improve this answer




























                                1














                                Boolean algebra is logic reduced to its most basic form. It maps nicely to a binary counting system, the second most basic imaginable counting system.




                                it seems that boolean algebra is used to simplify logic gates in hardware




                                Not quite. It makes it possible to implement any kind of logic in hardware in the first place. Once you have that, you can use software to build on that and create more complex logic again.



                                So no, you do not reduce the number of code lines using boolean algebra. You use it whenever there is a binary choice to be made. If your problem is complex, you are going to have a lot of choices in your software solution.



                                So it is the other way around: simple logic requires less boolean algebra, complex logic requires more. And more logic == more code.






                                share|improve this answer


























                                  1












                                  1








                                  1







                                  Boolean algebra is logic reduced to its most basic form. It maps nicely to a binary counting system, the second most basic imaginable counting system.




                                  it seems that boolean algebra is used to simplify logic gates in hardware




                                  Not quite. It makes it possible to implement any kind of logic in hardware in the first place. Once you have that, you can use software to build on that and create more complex logic again.



                                  So no, you do not reduce the number of code lines using boolean algebra. You use it whenever there is a binary choice to be made. If your problem is complex, you are going to have a lot of choices in your software solution.



                                  So it is the other way around: simple logic requires less boolean algebra, complex logic requires more. And more logic == more code.






                                  share|improve this answer













                                  Boolean algebra is logic reduced to its most basic form. It maps nicely to a binary counting system, the second most basic imaginable counting system.




                                  it seems that boolean algebra is used to simplify logic gates in hardware




                                  Not quite. It makes it possible to implement any kind of logic in hardware in the first place. Once you have that, you can use software to build on that and create more complex logic again.



                                  So no, you do not reduce the number of code lines using boolean algebra. You use it whenever there is a binary choice to be made. If your problem is complex, you are going to have a lot of choices in your software solution.



                                  So it is the other way around: simple logic requires less boolean algebra, complex logic requires more. And more logic == more code.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 1 hour ago









                                  Martin MaatMartin Maat

                                  8,16821131




                                  8,16821131























                                      1














                                      Well, OK. The reason electronics engineers want to reduce the number of logic gates in a circuit is probably a matter of construction cost and operational performance of their final product. They are building the very thing that will do the work in the end.



                                      When you are programming in any language other than assembly, the computer in the end will not execute your code, but a transformed version of it. This transformation from the programmer friendly language to the computer friendly language may be called «compilation». Boolean logic is indeed used by the compiler to achieve operational efficiency.



                                      You may code in assembly and do the compiler's job yourself, but on a day-to-day basis, it becomes less and less worth the hassle. Modern compilers know more and more of the tricks, and better than you.



                                      On the contrary, if you ask whether there exist a similar practice in higher-level languages, yes, it's called «refactoring», which means applying well-defined transformations to code chat change the structure and not the behaviour. In the 90s, Martin Fowler published a book that is a catalog of probably the most recurring basic refactorings in object languages.






                                      share|improve this answer




























                                        1














                                        Well, OK. The reason electronics engineers want to reduce the number of logic gates in a circuit is probably a matter of construction cost and operational performance of their final product. They are building the very thing that will do the work in the end.



                                        When you are programming in any language other than assembly, the computer in the end will not execute your code, but a transformed version of it. This transformation from the programmer friendly language to the computer friendly language may be called «compilation». Boolean logic is indeed used by the compiler to achieve operational efficiency.



                                        You may code in assembly and do the compiler's job yourself, but on a day-to-day basis, it becomes less and less worth the hassle. Modern compilers know more and more of the tricks, and better than you.



                                        On the contrary, if you ask whether there exist a similar practice in higher-level languages, yes, it's called «refactoring», which means applying well-defined transformations to code chat change the structure and not the behaviour. In the 90s, Martin Fowler published a book that is a catalog of probably the most recurring basic refactorings in object languages.






                                        share|improve this answer


























                                          1












                                          1








                                          1







                                          Well, OK. The reason electronics engineers want to reduce the number of logic gates in a circuit is probably a matter of construction cost and operational performance of their final product. They are building the very thing that will do the work in the end.



                                          When you are programming in any language other than assembly, the computer in the end will not execute your code, but a transformed version of it. This transformation from the programmer friendly language to the computer friendly language may be called «compilation». Boolean logic is indeed used by the compiler to achieve operational efficiency.



                                          You may code in assembly and do the compiler's job yourself, but on a day-to-day basis, it becomes less and less worth the hassle. Modern compilers know more and more of the tricks, and better than you.



                                          On the contrary, if you ask whether there exist a similar practice in higher-level languages, yes, it's called «refactoring», which means applying well-defined transformations to code chat change the structure and not the behaviour. In the 90s, Martin Fowler published a book that is a catalog of probably the most recurring basic refactorings in object languages.






                                          share|improve this answer













                                          Well, OK. The reason electronics engineers want to reduce the number of logic gates in a circuit is probably a matter of construction cost and operational performance of their final product. They are building the very thing that will do the work in the end.



                                          When you are programming in any language other than assembly, the computer in the end will not execute your code, but a transformed version of it. This transformation from the programmer friendly language to the computer friendly language may be called «compilation». Boolean logic is indeed used by the compiler to achieve operational efficiency.



                                          You may code in assembly and do the compiler's job yourself, but on a day-to-day basis, it becomes less and less worth the hassle. Modern compilers know more and more of the tricks, and better than you.



                                          On the contrary, if you ask whether there exist a similar practice in higher-level languages, yes, it's called «refactoring», which means applying well-defined transformations to code chat change the structure and not the behaviour. In the 90s, Martin Fowler published a book that is a catalog of probably the most recurring basic refactorings in object languages.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered 53 mins ago









                                          Laurent LA RIZZALaurent LA RIZZA

                                          43528




                                          43528























                                              1














                                              Of course you can use Boolean Algebra for different cases in your project. This technique allows to get the same output with less steps and components when designing a circuit.



                                              However in case of High-level languages it may result in more problems. HLL languages are designed for readability and ease of understanding. You can simplify your several lines of code into a single one, but it will become unreadable and complex, so much more time is required to grasp the idea of the code.
                                              Most of compilers perform such kind of optimizations by default.



                                              All in all, I suggest not to think a lot about reducing lines of code, but consider behavior of the program in runtime, memory usage and architectual design. Generally when you are applying Boolean Algebra to your circuits you are acting as a compiler for most of existing languages.






                                              share|improve this answer




























                                                1














                                                Of course you can use Boolean Algebra for different cases in your project. This technique allows to get the same output with less steps and components when designing a circuit.



                                                However in case of High-level languages it may result in more problems. HLL languages are designed for readability and ease of understanding. You can simplify your several lines of code into a single one, but it will become unreadable and complex, so much more time is required to grasp the idea of the code.
                                                Most of compilers perform such kind of optimizations by default.



                                                All in all, I suggest not to think a lot about reducing lines of code, but consider behavior of the program in runtime, memory usage and architectual design. Generally when you are applying Boolean Algebra to your circuits you are acting as a compiler for most of existing languages.






                                                share|improve this answer


























                                                  1












                                                  1








                                                  1







                                                  Of course you can use Boolean Algebra for different cases in your project. This technique allows to get the same output with less steps and components when designing a circuit.



                                                  However in case of High-level languages it may result in more problems. HLL languages are designed for readability and ease of understanding. You can simplify your several lines of code into a single one, but it will become unreadable and complex, so much more time is required to grasp the idea of the code.
                                                  Most of compilers perform such kind of optimizations by default.



                                                  All in all, I suggest not to think a lot about reducing lines of code, but consider behavior of the program in runtime, memory usage and architectual design. Generally when you are applying Boolean Algebra to your circuits you are acting as a compiler for most of existing languages.






                                                  share|improve this answer













                                                  Of course you can use Boolean Algebra for different cases in your project. This technique allows to get the same output with less steps and components when designing a circuit.



                                                  However in case of High-level languages it may result in more problems. HLL languages are designed for readability and ease of understanding. You can simplify your several lines of code into a single one, but it will become unreadable and complex, so much more time is required to grasp the idea of the code.
                                                  Most of compilers perform such kind of optimizations by default.



                                                  All in all, I suggest not to think a lot about reducing lines of code, but consider behavior of the program in runtime, memory usage and architectual design. Generally when you are applying Boolean Algebra to your circuits you are acting as a compiler for most of existing languages.







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered 48 mins ago









                                                  CROSPCROSP

                                                  6411614




                                                  6411614























                                                      1














                                                      Yes, you can. But should you?



                                                      Boolean algebra serves to reduce logical expressions to their minimal form, but whether this is good or bad is left to the programmer. Let's take this validation code:



                                                      ...
                                                      if (person.Money == 0) return;
                                                      if (person.Money != 0 && person.Age < 15) return;
                                                      if (person.Age > 90) return;
                                                      if (person.Children > 3) return;
                                                      if (person.HasWhiteShirt() && person.HasBlueSocks()) return;
                                                      ...


                                                      This is a list of checks on the object person, and will exit the function as soon as one of those is true. The meaning of that is pretty clear, and anyone that reads it, even a non-programmer, can understand what checks we're doing.



                                                      There are different ways to write that, though. Let's go for lines of code and simplify it, as you asked in your question, and see what happens:



                                                      if (... || person.Money == 0 || person.Age < 15 || person.Age > 90 || person.Children > 3 || (person.HasWhiteShirt() && person.HasBlueSocks()) || ...) return;


                                                      We now have a single line of unreadable conditionals, and by simplifying we also lost the info that that <15 age check is somehow related to money. Editing this code is gonna be way more difficult in the future, so reducing it was a mistake.



                                                      In conclusion: Always go for readability, regardless of the minimal form of a logical expression.






                                                      share|improve this answer




























                                                        1














                                                        Yes, you can. But should you?



                                                        Boolean algebra serves to reduce logical expressions to their minimal form, but whether this is good or bad is left to the programmer. Let's take this validation code:



                                                        ...
                                                        if (person.Money == 0) return;
                                                        if (person.Money != 0 && person.Age < 15) return;
                                                        if (person.Age > 90) return;
                                                        if (person.Children > 3) return;
                                                        if (person.HasWhiteShirt() && person.HasBlueSocks()) return;
                                                        ...


                                                        This is a list of checks on the object person, and will exit the function as soon as one of those is true. The meaning of that is pretty clear, and anyone that reads it, even a non-programmer, can understand what checks we're doing.



                                                        There are different ways to write that, though. Let's go for lines of code and simplify it, as you asked in your question, and see what happens:



                                                        if (... || person.Money == 0 || person.Age < 15 || person.Age > 90 || person.Children > 3 || (person.HasWhiteShirt() && person.HasBlueSocks()) || ...) return;


                                                        We now have a single line of unreadable conditionals, and by simplifying we also lost the info that that <15 age check is somehow related to money. Editing this code is gonna be way more difficult in the future, so reducing it was a mistake.



                                                        In conclusion: Always go for readability, regardless of the minimal form of a logical expression.






                                                        share|improve this answer


























                                                          1












                                                          1








                                                          1







                                                          Yes, you can. But should you?



                                                          Boolean algebra serves to reduce logical expressions to their minimal form, but whether this is good or bad is left to the programmer. Let's take this validation code:



                                                          ...
                                                          if (person.Money == 0) return;
                                                          if (person.Money != 0 && person.Age < 15) return;
                                                          if (person.Age > 90) return;
                                                          if (person.Children > 3) return;
                                                          if (person.HasWhiteShirt() && person.HasBlueSocks()) return;
                                                          ...


                                                          This is a list of checks on the object person, and will exit the function as soon as one of those is true. The meaning of that is pretty clear, and anyone that reads it, even a non-programmer, can understand what checks we're doing.



                                                          There are different ways to write that, though. Let's go for lines of code and simplify it, as you asked in your question, and see what happens:



                                                          if (... || person.Money == 0 || person.Age < 15 || person.Age > 90 || person.Children > 3 || (person.HasWhiteShirt() && person.HasBlueSocks()) || ...) return;


                                                          We now have a single line of unreadable conditionals, and by simplifying we also lost the info that that <15 age check is somehow related to money. Editing this code is gonna be way more difficult in the future, so reducing it was a mistake.



                                                          In conclusion: Always go for readability, regardless of the minimal form of a logical expression.






                                                          share|improve this answer













                                                          Yes, you can. But should you?



                                                          Boolean algebra serves to reduce logical expressions to their minimal form, but whether this is good or bad is left to the programmer. Let's take this validation code:



                                                          ...
                                                          if (person.Money == 0) return;
                                                          if (person.Money != 0 && person.Age < 15) return;
                                                          if (person.Age > 90) return;
                                                          if (person.Children > 3) return;
                                                          if (person.HasWhiteShirt() && person.HasBlueSocks()) return;
                                                          ...


                                                          This is a list of checks on the object person, and will exit the function as soon as one of those is true. The meaning of that is pretty clear, and anyone that reads it, even a non-programmer, can understand what checks we're doing.



                                                          There are different ways to write that, though. Let's go for lines of code and simplify it, as you asked in your question, and see what happens:



                                                          if (... || person.Money == 0 || person.Age < 15 || person.Age > 90 || person.Children > 3 || (person.HasWhiteShirt() && person.HasBlueSocks()) || ...) return;


                                                          We now have a single line of unreadable conditionals, and by simplifying we also lost the info that that <15 age check is somehow related to money. Editing this code is gonna be way more difficult in the future, so reducing it was a mistake.



                                                          In conclusion: Always go for readability, regardless of the minimal form of a logical expression.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered 32 mins ago









                                                          BgrWorkerBgrWorker

                                                          1,3641413




                                                          1,3641413























                                                              -5














                                                              Subject: Generating PDF after submitting the survey in Limesurvey ?
                                                              Dear all,
                                                              I would like to aks how can print out PDF after submitting the survey in Limesurvey ?
                                                              Could we use css code for this ?
                                                              Many thanks for your help!
                                                              Best regards.






                                                              share|improve this answer








                                                              New contributor




                                                              liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                              Check out our Code of Conduct.

























                                                                -5














                                                                Subject: Generating PDF after submitting the survey in Limesurvey ?
                                                                Dear all,
                                                                I would like to aks how can print out PDF after submitting the survey in Limesurvey ?
                                                                Could we use css code for this ?
                                                                Many thanks for your help!
                                                                Best regards.






                                                                share|improve this answer








                                                                New contributor




                                                                liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                Check out our Code of Conduct.























                                                                  -5












                                                                  -5








                                                                  -5







                                                                  Subject: Generating PDF after submitting the survey in Limesurvey ?
                                                                  Dear all,
                                                                  I would like to aks how can print out PDF after submitting the survey in Limesurvey ?
                                                                  Could we use css code for this ?
                                                                  Many thanks for your help!
                                                                  Best regards.






                                                                  share|improve this answer








                                                                  New contributor




                                                                  liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.










                                                                  Subject: Generating PDF after submitting the survey in Limesurvey ?
                                                                  Dear all,
                                                                  I would like to aks how can print out PDF after submitting the survey in Limesurvey ?
                                                                  Could we use css code for this ?
                                                                  Many thanks for your help!
                                                                  Best regards.







                                                                  share|improve this answer








                                                                  New contributor




                                                                  liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.









                                                                  share|improve this answer



                                                                  share|improve this answer






                                                                  New contributor




                                                                  liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.









                                                                  answered 1 hour ago









                                                                  litiliti

                                                                  1




                                                                  1




                                                                  New contributor




                                                                  liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.





                                                                  New contributor





                                                                  liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.






                                                                  liti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                                  Check out our Code of Conduct.






















                                                                      themis is a new contributor. Be nice, and check out our Code of Conduct.










                                                                      draft saved

                                                                      draft discarded


















                                                                      themis is a new contributor. Be nice, and check out our Code of Conduct.













                                                                      themis is a new contributor. Be nice, and check out our Code of Conduct.












                                                                      themis is a new contributor. Be nice, and check out our Code of Conduct.
















                                                                      Thanks for contributing an answer to Software Engineering Stack Exchange!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f385783%2fcan-i-use-boolean-algebra-to-reduce-the-number-of-lines-in-my-code%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown











                                                                      Popular posts from this blog

                                                                      Morgemoulin

                                                                      Scott Moir

                                                                      Souastre