Clean your condition ?

Always positive

It’s need extra effort to understanding logic in negative condition, avoid it as you can

// ❌ Don’t

function isUserNotVerified(){

}

if(!userVerified){

}

// ✅ Do

function isUserVerified(){

}

if(userVerified){

}


This content originally appeared on DEV Community and was authored by Heru Hartanto

Always positive

It's need extra effort to understanding logic in negative condition, avoid it as you can

// ❌ Don't 

function isUserNotVerified(){

}

if(!userVerified){

}

// ✅ Do

function isUserVerified(){

}

if(userVerified){

}

Use Shorthands if possible

Shorthands make your code use less line and easier to read

// ❌ Don't

if(isActive ==null){

}

if(firstname !== null || firstname !=='' || firstname !== undefined){

}

const isUserValid = user.isVerified() && user.isActive() ? true : false;

// ✅ Do

if(isActive) {

}

if(!!firstname){

}

const isUserValid = user.isVerified() && user.isActive()

Object literals over Switch statements

// ❌ Don't

const getStatus = (status) => {
  switch (status) {
    case "success":
      return "green";
    case "failure":
      return "red";
    case "warning":
      return "yellow";
    case "loading":
    default:
      return "blue";
  }
};

// ✅ Do
const statusColors = {
  success: "green",
  failure: "red",
  warning: "yellow",
  loading: "blue",
};

const getStatus = (status) => statusColors[status] || statusColors.loading;

Use optional chaining

Remember that optional chaining is not working with IE browser yet, see here

const alice = {
    name:'Alice',
    cat:{
        name:'Nala'
    }
}
// ❌ Don't

const cat = (alice && alice.cat && alice.cat.name) || 'N/A';

// ✅ Do

const cat = alice?.cat?.name ?? 'N/A';


This content originally appeared on DEV Community and was authored by Heru Hartanto


Print Share Comment Cite Upload Translate Updates
APA

Heru Hartanto | Sciencx (2021-09-16T00:27:58+00:00) Clean your condition ?. Retrieved from https://www.scien.cx/2021/09/16/clean-your-condition-%f0%9f%a7%bc/

MLA
" » Clean your condition ?." Heru Hartanto | Sciencx - Thursday September 16, 2021, https://www.scien.cx/2021/09/16/clean-your-condition-%f0%9f%a7%bc/
HARVARD
Heru Hartanto | Sciencx Thursday September 16, 2021 » Clean your condition ?., viewed ,<https://www.scien.cx/2021/09/16/clean-your-condition-%f0%9f%a7%bc/>
VANCOUVER
Heru Hartanto | Sciencx - » Clean your condition ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/16/clean-your-condition-%f0%9f%a7%bc/
CHICAGO
" » Clean your condition ?." Heru Hartanto | Sciencx - Accessed . https://www.scien.cx/2021/09/16/clean-your-condition-%f0%9f%a7%bc/
IEEE
" » Clean your condition ?." Heru Hartanto | Sciencx [Online]. Available: https://www.scien.cx/2021/09/16/clean-your-condition-%f0%9f%a7%bc/. [Accessed: ]
rf:citation
» Clean your condition ? | Heru Hartanto | Sciencx | https://www.scien.cx/2021/09/16/clean-your-condition-%f0%9f%a7%bc/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.