can anyone recommend software (preferably for mac) or a web based tool, that can be used to evaluate logic expressions?
For example I would like to be able to quickly test whether two expressions like:
$a = 'foo';
$b = 'bar';
$c = 'foo';
( !(($a == $c) && ($b == $c)) )
// and
( ($a != $c) || ($b != c$) )
are interchangeable or not.
And also, is there generally an agreed upon best practice in relation to how to construct such expressions? For example to try to minimize the use of the negation, the order of the elements or something like that?
Sometimes I find myself struggling a bit with these things :)
You can also use Wolfram Alpha
https://www.wolframalpha.com/input/?i=P+%26%26+(Q+%7C%7C+R)&lk=3
or
https://www.dcode.fr/boolean-expressions-calculator
You can use something like http://www-cs-students.stanford.edu/~silver/truth/ and compare the generated truth tables.
I like this site which does exactly what you are looking for, but it only supports limited logical operators:
https://electronics-course.com/boolean-algebra
Related
What is the most readable way of writing a very simple function that is effectively executing one statement, if a condition is met?
What I find most readable is this:
function doSomething(myNumber){
if(myNumber !== null && myNumber > 5){
doTheThing();
}
}
However, my colleague insists that guard clauses make anything more readable, and would always write this function like this:
function doSomething(myNumber){
if(myNumber === null || myNumber <= 5)
return;
doTheThing();
}
I understand that guard clauses can be way more readable in bigger functions, especially if there is a need to check for multiple conditions, and/or if exceptions need to be thrown. But in a case like this, I always have to do a triple-take to understand in which case doTheThing() will be executed, which seems ridiculous for such a simple function.
This is not really a technical question, but rather a choice of style.
There are many ways you can write that function and the compiler will try to optimise it as best as possible. As for "readability" this is purely up to the programmer's choice. As long as you follow the language rules and standards, then any choice is fine. Of course if you work in a team it might be better to agree on a certain style, so that everyone can work on the code without getting confused.
Personally, if I really want to make it readable I would do this:
function doSomething(myNumber)
{
if(myNumber != null && myNumber > 5)
{
doTheThing();
}
}
On the other hand if I want less lines of code I would choose this:
function doSomething(myNumber) {
if(myNumber == null || myNumber <= 5) return;
doTheThing();
}
Also it is important to consider how the if statement should be. In this case you cover all possibilities, but just keep it in mind to avoid unexpected errors.
Is there a way to represent logical not, with && or || or some statement like if or ?:
I want to implement this in some other way:
boolean isRunning = true;
startButton.setEnabled(!isRunning); // <<== ???
Assuming that you are doing this as an exercise, ternary operator lets you replace ! in a simple and straightforward way:
startButton.setEnabled(isRunning ? false : true);
As far as using && and || by themselves goes, this pair of operators is not functionally complete, i.e. there are operations that cannot be implemented by using a sequence of &&s and ||s alone; not ! operation is among operations that cannot be implemented with ands and ors.
It's a bit confusing to me about what is the difference between these condition expressions below:
if( 1 == a) {
//something
}
and
if( a == 1 ) {
//something
}
I saw the above one in some scripts I have downloaded and I wonder what's the difference between them.
The former has been coined a Yoda Condition.
Using if(constant == variable) instead of if(variable == constant), like if(1 == a). Because it's like saying "if blue is the sky" or "if tall is the man".
The constant == variable syntax is often used to avoid mistyping == as =. It is, of course, often used without understanding also when you have constant == function_call_retuning_nothing_modifiable.
Other than that there's no difference, unless you have some weird operator override.
Many programming languages allow assignments like a = 1 to be used as expressions, making the following code syntactically valid (given that integers can be used in conditionals, such as in C or many scripting languages):
if (a = 1) {
// something
}
This is rarely desired, and can lead to unexpected behavior. If 1 == a is used, then this mistake cannot occur because 1 = a is not valid.
Well, I am not sure about the trick. Generally, we could say the equal sign is commutative. So, a = b implies b = a. However, when you have == or === this doesn't work in certain cases, for example when on the right side you have a range: 5 === (1..10) vs. (1..10) === 5.
I've never used the following kind of expressions in if statements or other control structures (example in PHP):
if ( ( $p = someFunction() ) !== false ) {
// Use $p
}
I usually take this kind of assignment expression out and have it evaluated before I check it in an if statement.
My questions are:
Is there a name for this technique?
After the assignment ($p = someFunction()) what types can be tested for? (For example only true/false or the actual type someFuntion returns)?
Is this reccomended and safe to use with most languages and to expect similar behaviour?
You could name it using return value of assignment expression?
Why do you ask this generally? You have to check if your language supports this kind of code. Most C like languages return the value that is assigned to the variable on the left so it can be any type.
oracle has a nice built in function for doing if null, however I want to do if = 0; is there a simple way to do this?
nvl(instr(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5),' '),
length(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5))))
This is going as a parameter to a substr function.
If instr(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5),' ') is != 0 then I want that value, otherwise I want the value of length(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5))
is there an easy way to do this?
You can use NVL(NULLIF(A,'0'), B)
I found both answers hard to read because of the extra verbiage from the original post. Summarizing Conrad and Craig's answers:
To replicate nvl(A,B) but for 0 instead of null, you can do:
WHEN A != 0 THEN
A
ELSE
B
END
or Craig's more compact (but harder for others to read):
NVL(NULLIF(A,0),B)
I think that the following would also work:
DECODE(A,0,B,A)
I think you'll need to use CASE
e.g.
WHEN instr(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5),' ') != 0 THEN
length(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5))
ELSE
Some Default
END as foo
You could technically do this with less typing as:
nvl(
nullif(instr(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5),' '),0),
length(substr(ovrflo_adrs_info,instr(ovrflo_adrs_info,'bldg')+5))
)
However, I would typically side with Conrad and advise you to use CASE so it is easier to tell what the intent of the code is for future maintenance.