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












2















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.

























    2















    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.























      2












      2








      2








      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 6 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 8 hours ago









      themisthemis

      1164




      1164




      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.






















          5 Answers
          5






          active

          oldest

          votes


















          6














          You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, subtracting 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 lead 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

































            4














            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



















            • 4





              A good compiler will "use Boolean algebra" internally to generate optimised code. Unless you are writing in assembler for an embedded system, make the source code easy to read and let the computer do the "clever stuff" for itself.

              – alephzero
              2 hours ago






            • 2





              Honestly I would write it with ||s as in your second example, but split it so that each term is on a separate line.

              – Rotem
              1 hour ago



















            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























                  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: true,
                  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

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  6














                  You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, subtracting 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 lead 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






























                    6














                    You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, subtracting 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 lead 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




























                      6












                      6








                      6







                      You can use boolean algebra for many things in programming, it is a basic calculation technique like adding, subtracting 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 lead 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, subtracting 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 lead 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 3 hours ago

























                      answered 6 hours ago









                      Doc BrownDoc Brown

                      131k23241380




                      131k23241380

























                          4














                          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



















                          • 4





                            A good compiler will "use Boolean algebra" internally to generate optimised code. Unless you are writing in assembler for an embedded system, make the source code easy to read and let the computer do the "clever stuff" for itself.

                            – alephzero
                            2 hours ago






                          • 2





                            Honestly I would write it with ||s as in your second example, but split it so that each term is on a separate line.

                            – Rotem
                            1 hour ago
















                          4














                          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



















                          • 4





                            A good compiler will "use Boolean algebra" internally to generate optimised code. Unless you are writing in assembler for an embedded system, make the source code easy to read and let the computer do the "clever stuff" for itself.

                            – alephzero
                            2 hours ago






                          • 2





                            Honestly I would write it with ||s as in your second example, but split it so that each term is on a separate line.

                            – Rotem
                            1 hour ago














                          4












                          4








                          4







                          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 4 hours ago









                          BgrWorkerBgrWorker

                          1,3941413




                          1,3941413








                          • 4





                            A good compiler will "use Boolean algebra" internally to generate optimised code. Unless you are writing in assembler for an embedded system, make the source code easy to read and let the computer do the "clever stuff" for itself.

                            – alephzero
                            2 hours ago






                          • 2





                            Honestly I would write it with ||s as in your second example, but split it so that each term is on a separate line.

                            – Rotem
                            1 hour ago














                          • 4





                            A good compiler will "use Boolean algebra" internally to generate optimised code. Unless you are writing in assembler for an embedded system, make the source code easy to read and let the computer do the "clever stuff" for itself.

                            – alephzero
                            2 hours ago






                          • 2





                            Honestly I would write it with ||s as in your second example, but split it so that each term is on a separate line.

                            – Rotem
                            1 hour ago








                          4




                          4





                          A good compiler will "use Boolean algebra" internally to generate optimised code. Unless you are writing in assembler for an embedded system, make the source code easy to read and let the computer do the "clever stuff" for itself.

                          – alephzero
                          2 hours ago





                          A good compiler will "use Boolean algebra" internally to generate optimised code. Unless you are writing in assembler for an embedded system, make the source code easy to read and let the computer do the "clever stuff" for itself.

                          – alephzero
                          2 hours ago




                          2




                          2





                          Honestly I would write it with ||s as in your second example, but split it so that each term is on a separate line.

                          – Rotem
                          1 hour ago





                          Honestly I would write it with ||s as in your second example, but split it so that each term is on a separate line.

                          – Rotem
                          1 hour ago











                          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 5 hours 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 4 hours 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 4 hours ago









                                              CROSPCROSP

                                              6421614




                                              6421614






















                                                  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

                                                  Ponta tanko

                                                  Tantalo (mitologio)

                                                  Erzsébet Schaár