A
A
Andrew2021-10-07 15:34:47
Programming languages
Andrew, 2021-10-07 15:34:47

Is there a PL with a similar syntax for logical operations?

There was a dispute with a friend that he lacks expressions in JS like:

if (status === 'complete' || 'deleted' || 'updated') { //... }

Instead of:

if (status === 'complete' || status === 'deleted' || нувыпонели) {}

I said it was unreadable and no one would do that kind of syntax. But then I thought - what if someone still would?

PS
Of course, as a short note, I suggested

['complete', 'deleted', 'updated'].includes(status)

But these are all far-fetched and artificial examples. In real life, conditions in JS can really reach nightmare sizes and become unreadable, even if they are placed in variables, or even entire functions.

Therefore, I would like to discuss such a question - in which language is the coolest syntax for logical operations? Or is it the same everywhere?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
V
Vasily Bannikov, 2021-10-07
@AndrewRusinas

There is something similar in C#:

if(status is "complete" or "deleted" or "updated") { /*...*/ }

T
taktik, 2021-10-07
@taktik

python:

if status in ['complete', 'deleted', 'updated']:
    # ...

C
ComodoHacker, 2021-10-07
@ComodoHacker

It's written like this in C.

T
tundramani, 2021-10-07
@tundramani

the syntax of the language should be as simple as possible
the simpler the better
do this:
let array_of_values ​​= ['complete', 'deleted', 'updated']
let if = function(key, array_of_values)
{
return true /false
}
if( if(status , array_of_values) )
{ }

J
Jacen11, 2021-10-07
@Jacen11

['complete', 'deleted', 'updated'].includes(status)

in kotlin it would be status in listOf('complete', 'deleted', 'updated')
if (status === 'complete' || 'deleted' || 'updated') { //... }

and this is just some kind of nonsense, these operations are applied only to the boolean, 'deleted' is not a boolean. It's like requiring "strring" * "string" to work.

D
dollar, 2021-10-07
@dollar

If you optimize speed , then you need to use the power of hash tables, the search for which takes approximately O (1):

const NEEDED_STATUS = { complete: true, deleted: true, updated: true}
if (NEEDED_STATUS[status]) { /*... */ }

However, two points should be kept in mind:
  1. This is savings on matches.
  2. The increase in performance will start approximately when the number of options is greater than 4. The real increase will be with a very large number of options.

Hash tables (or the ability to create) are in almost any PL.
If we optimize the beauty and clarity of the code, then I would suggest this:
let is_complete = (status === 'complete');
let is_deleted = (status === 'deleted');
let is_updated = (status === 'updated');
if (is_complete || is_deleted || is_updated) { /*... */ }

In this case, the total size of the code will grow, but the condition itself will become more compact and understandable. In addition, often these is_что-тоare needed in other places in the code that will also look neat, and in general, the total amount of code can sometimes even decrease.
This technique is possible in any PL.

Y
Yury Komar, 2021-10-08
@Yury_Komar

VB.NET:

If (New String(){"111",  "222", "333"}).Contains(status) Then
'...
End If

or like this:
Select Case status : Case "111", "222", "333"
'...
End Select

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question