Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 101 characters in body
Source Link
Grijesh Chauhan
  • 58.6k
  • 20
  • 145
  • 214

Is my understanding correct?

Yes, your interpretation is correct.

Is using this better or multiple if/else checks?

As you know the code:

if ( a ? b ? c : d : false) { "Code-1" } else { "Code-2" } 

could be written like:

if(a){ if(b){ if (c){ "code-1" } else{ "code-2" } } else{ if(d){ "code-1" } else{ "code-2" } } } else{ //then false; "Code-2" } 

Now which you will prefer in above two. Second is long and includes many nested level code (hard to understand and bug). Additionally first code that can be improved in readability as:

if ( a ? (b ? c : d) : false) 

as @ManikandanSigamani answered.

As I noted @WhozCraig also give an another technique to write your code better if (a && (b ? c : d)) using && that supports Short-Circuit logic.

A good programmer is one who know one than one technique to solve a problem and also know which is better. A short and linear codding is preferable in general. In small writing small code chances of making mistakes are less (looks like functional programming better then imperative programming). And small code easily can improved as in your case suggested by @ManikandanSigamani and @WhozCraig.

I will prefer: if (a && (b ? c : d)) form!

Is my understanding correct?

Yes, your interpretation is correct.

Is using this better or multiple if/else checks?

As you know the code:

if ( a ? b ? c : d : false) { "Code-1" } else { "Code-2" } 

could be written like:

if(a){ if(b){ if (c){ "code-1" } else{ "code-2" } } else{ if(d){ "code-1" } else{ "code-2" } } } else{ //then false; "Code-2" } 

Now which you will prefer in above two. Second is long and includes many nested level code (hard to understand and bug). Additionally first code that can be improved in readability as:

if ( a ? (b ? c : d) : false) 

as @ManikandanSigamani answered.

As I noted @WhozCraig also give an another technique to write your code better if (a && (b ? c : d)) using && that supports Short-Circuit logic.

A good programmer is one who know one than one technique to solve a problem and also know which is better. A short and linear codding is preferable in general. In small writing small code chances of making mistakes are less (looks like functional programming better then imperative programming).

I will prefer: if (a && (b ? c : d)) form!

Is my understanding correct?

Yes, your interpretation is correct.

Is using this better or multiple if/else checks?

As you know the code:

if ( a ? b ? c : d : false) { "Code-1" } else { "Code-2" } 

could be written like:

if(a){ if(b){ if (c){ "code-1" } else{ "code-2" } } else{ if(d){ "code-1" } else{ "code-2" } } } else{ //then false; "Code-2" } 

Now which you will prefer in above two. Second is long and includes many nested level code (hard to understand and bug). Additionally first code that can be improved in readability as:

if ( a ? (b ? c : d) : false) 

as @ManikandanSigamani answered.

As I noted @WhozCraig also give an another technique to write your code better if (a && (b ? c : d)) using && that supports Short-Circuit logic.

A good programmer is one who know one than one technique to solve a problem and also know which is better. A short and linear codding is preferable in general. In small writing small code chances of making mistakes are less (looks like functional programming better then imperative programming). And small code easily can improved as in your case suggested by @ManikandanSigamani and @WhozCraig.

I will prefer: if (a && (b ? c : d)) form!

added 135 characters in body
Source Link
Grijesh Chauhan
  • 58.6k
  • 20
  • 145
  • 214

Is my understanding correct?

Yes, your interpretation is correct. And as

Is using this better or multiple if/else checks?

As you know followingthe code:

if ( a ? b ? c : d : false) { "Code-1" } else { "Code-2" } 

could be written aslike:

if(a){ if(b){ if (c){ "code-1" } else{ "code-2" } } else{ if(d){ "code-1" } else{ "code-2" } } } else{ //then false; "Code-2" } 

Now which you will prefer in above two. Second is long and includes many nested level code. first (hard to understand and bug). Additionally first code that can be improved in readability as:

if ( a ? (b ? c : d) : false) 

as @ManikandanSigamani answered.

As I noted @WhozCraig also give an another technique to write your code better if (a && (b ? c : d)) using && that supports Short-Circuit logic.

A good programmer is one who know one than one technique to solve a problem and also know which is better. A short and linear codding is preferable in general. In small writing small code chances of making mistakes are less (looks like functional programming better then imperative programming).

I will prefer: if (a && (b ? c : d)) form!

Yes, your interpretation is correct. And as you know following

if ( a ? b ? c : d : false) { "Code-1" } else { "Code-2" } 

could be written as:

if(a){ if(b){ if (c){ "code-1" } else{ "code-2" } } else{ if(d){ "code-1" } else{ "code-2" } } } else{ //then false; "Code-2" } 

Now which you will prefer in above two. Second is long and includes many nested level code. first. that can be improved in readability as:

if ( a ? (b ? c : d) : false) 

as @ManikandanSigamani answered.

As I noted @WhozCraig also give an another technique to write your code better if (a && (b ? c : d)) using && that supports Short-Circuit logic.

A good programmer is one who know one than one technique to solve a problem and also know which is better. A short and linear codding is preferable in general.

Is my understanding correct?

Yes, your interpretation is correct.

Is using this better or multiple if/else checks?

As you know the code:

if ( a ? b ? c : d : false) { "Code-1" } else { "Code-2" } 

could be written like:

if(a){ if(b){ if (c){ "code-1" } else{ "code-2" } } else{ if(d){ "code-1" } else{ "code-2" } } } else{ //then false; "Code-2" } 

Now which you will prefer in above two. Second is long and includes many nested level code (hard to understand and bug). Additionally first code that can be improved in readability as:

if ( a ? (b ? c : d) : false) 

as @ManikandanSigamani answered.

As I noted @WhozCraig also give an another technique to write your code better if (a && (b ? c : d)) using && that supports Short-Circuit logic.

A good programmer is one who know one than one technique to solve a problem and also know which is better. A short and linear codding is preferable in general. In small writing small code chances of making mistakes are less (looks like functional programming better then imperative programming).

I will prefer: if (a && (b ? c : d)) form!

Source Link
Grijesh Chauhan
  • 58.6k
  • 20
  • 145
  • 214

Yes, your interpretation is correct. And as you know following

if ( a ? b ? c : d : false) { "Code-1" } else { "Code-2" } 

could be written as:

if(a){ if(b){ if (c){ "code-1" } else{ "code-2" } } else{ if(d){ "code-1" } else{ "code-2" } } } else{ //then false; "Code-2" } 

Now which you will prefer in above two. Second is long and includes many nested level code. first. that can be improved in readability as:

if ( a ? (b ? c : d) : false) 

as @ManikandanSigamani answered.

As I noted @WhozCraig also give an another technique to write your code better if (a && (b ? c : d)) using && that supports Short-Circuit logic.

A good programmer is one who know one than one technique to solve a problem and also know which is better. A short and linear codding is preferable in general.