L
L
Luis Ignascio2017-04-26 19:17:48
Programming
Luis Ignascio, 2017-04-26 19:17:48

How to learn to quickly count logical expressions?

As a programmer, I often come across logical expressions. As soon as you need to write something like A || B & (C || D) , I start to calculate for a long time what will happen at the output. How to learn to solve such problems as efficiently as possible?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2017-04-26
@EvilWays

Break the complex into simple parts. A || B & (C || D) come up with a more practical example (equivalent to yours).

if (!file.exist || (file.type != DIRECTORY)  & (file.creationDate < lastGoodDate || file.size > maxSize))
{
   printErrorMessage();
}

This is hard to write and read. A longer, but more understandable version:
bool isBadFile(file)
{
   if (!file.exist)
      return true; 

   if (file.type == DIRECTORY)
      return fasle; //skip directories

   bool isTooOld = file.creationDate < lastGoodDate;
   bool isTooBig = file.size > maxSize;

   return isTooBig || isTooOld
}

if (isBadFile(file))
{
   printErrorMessage();
}

S
Sergey, 2017-04-26
@stweet

Avoid such decisions, rewrite, write clear, transparent code, shoot those who write like a Hindu.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question