Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it useful in C# to apply DeMorgan's theorem to manually optimize boolean expressions in conditional statements (e.g. if conditions)

Back in the day when I did most of my work in C and C++, as a matter of course, I would manually apply deMorgan's theorem to optimize any non-trivial boolean expressions.

Is it useful to do this in C# or does the optimizer render this unnecessary?

like image 395
Jeff Leonard Avatar asked Jun 11 '09 15:06

Jeff Leonard


People also ask

Is it still useful to learn C?

Learning C is worth it. It is hard to avoid C because it is used to write OS kernels, databases, compilers, and many other applications. Knowledge of C will be required to debug or improve them.

What is the useful of C?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C or C++ more useful?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.


2 Answers

On processors this fast, it's virtually impossible for rearranging boolean expressions to make any actual difference in speed. And the C# compiler is very smart, it will optimize it as well. Optimize for readability and clarity!

like image 98
Ana Betts Avatar answered Sep 20 '22 12:09

Ana Betts


Your first goal should be to optimize such statements for developer comprehension and ease of maintenance.

DeMorgan's theorem can be a useful tool for this.

like image 45
Richard Ev Avatar answered Sep 20 '22 12:09

Richard Ev