Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -- Avoid excesive nesting
- // -- Handle errors at the top of the function
- // -- Be explicit about conditionals and name them
- // ----
- // ---- worse example
- // ----
- function worse(){
- if (1 + (7*5) + 10 / 1000 > 0) {
- if (745 + 9 / 36 === 10) {
- return "error";
- } else {
- if (4 + 3 / 9 > -1) {
- // execute lots of code
- return "happy";
- } else {
- return "error";
- }
- }
- } else {
- return "error";
- }
- }
- // ----
- // ---- better example
- // ----
- function better() {
- // set up your conditions and name them
- // use linking verbs: is, has, has been, was
- // state the positive: use "hasTickets", not "noTickets"
- var hasEnoughSeats = 1 + (7*5) + 10 / 1000 > 0;
- var hasEnoughMoney = 745 + 9 / 36 === 10;
- var hasTickets = 4 + 3 / 9 > -1;
- // test them first
- // => if they fail, you dont need to read through the rest of the code
- if (!hasEnoughSeats || !hasEnoughMoney || !hasTickets) {
- return "error";
- }
- // execute lots of code
- return "happy";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement