ASHdevelopment / standards

ASH Development Standards
28 stars 13 forks source link

Block or blockless for if conditions with one statement #136

Closed ASH-Michael closed 6 years ago

ASH-Michael commented 6 years ago

I thought we already had a standard for this, but I am not able to find it in our standards repo.

if (condition)
    statement;

or

if (condition) {
    statement;
}
ASH-Bryan commented 6 years ago

I think that blockless is fine, if it is on one line:

if (iceCream > vegetables) return true;

This keeps it terse but also is a signal to anyone adding more statements that they will need to add a block.

ASH-Michael commented 6 years ago

Like the ternary operator for conditionals? I would agree with that. I could buy off on blockless if it is written in a single line.

Seeing a blockless pattern on 2 lines is a visual speedbump for me.

ASH-Michael commented 6 years ago

Agreed upon standard: If you have a condition with only 1 statement, it can either be written with curly braces or written as a single line.

//good
if (condition) {
    statement;
}

if (condition) statement;

//bad
if (condition)
    statement;
ashjackguo commented 6 years ago

are we allowed to do something like this?

condition ? true : false
ASH-Anthony commented 6 years ago

@ashjackguo yes, but it's been unwritten. do you want to open a separate issue so we can confirm/educate in our meeting?

ashjackguo commented 6 years ago

@ASH-Anthony Sure, I'll open a new issue for that!