KISS

Agenda

  1. Definition of KISS
  2. KISS 1: less lines of code
  3. KISS 2: less complexity
  4. Summary
Definition
KISS - "Keep It Simple Stupid"

Story of Kelly Johnson

Simple things are easier to:
  • understand
  • maintain
  • change

So, try to keep your code and system structure simple.

No PIE & framework magic in the code unless you can justify their need. Because, they always requires you to add more classes and add complexity to your code.

KISS 1: less lines of code
1. Short-circuit Evaluation
						
							   if (var1 === null || var1 === undefined || var1 === '') {
									console.log("VARIABLE NOT FOUND!");
							   } else {
								   console.log(var1);
							   }
						
					
						
								console.log( var1 || "VARIABLE NOT FOUND!" );
						
					
2. Arrow Functions () =>
						
							function sayHello(name) {
								console.log('Hello', name);
							}
						
					

can be replaced with arrow function

						
							const sayHello = name => console.log("Hello", name);
						
					

different scoping methodology.

3. forEach
						
							const list = [2, 5, 7, 2, 6, 2, 3, 5];
							for (let i = 0; i < list.length; i++) {
								console.log(list[i]);
							}
						
					
						
							list.forEach(number => console.log(number));
						
					
4. Default Parameters
						
							function volume(l, w, h) {
								if (w === undefined)
									w = 1;
								if (h === undefined)
									h = 1;
								return l * w * h;
								}
						
					
						
							const volume = ( l, w = 1, h = 1) => l * w * h
						
					
5. Destructuring

								const person = {
									first: 'Wes',
									last: 'Bos',
									country: 'Canada',
									city: 'Hamilton',
									twitter: '@wesbos'
								  };
								  const first = person.first;
								  const last = person.last;
						

vs


								let { first, last } = person;
						
6. Spread Operator

							const odd = [1, 2, 3];
							const nums = [4, 5, 6].concat(odd);
					

vs


							const nums = [4, 5, 6, ...odd];
					
KISS 2: less complexity
simple == !complex
Reason 1: make code more flexible to accommodate further requirements
Reason 2: optimization
Example

							function weekday1(dayOfWeek){
								switch(dayOfWeek){
									case 1: return "Monday";
									case 2: return "Tuesday";
									case 3: return "Wednesday";
									case 4: return "Thursday";
									case 5: return "Friday";
									case 6: return "Saturday";
									case 7: return "Sunday";
									default: 
									  throw new Error("dayOfWeek - in range 1..7");
								}
							}									
					

							function weekday2(dayOfWeek){
								if((dayOfWeek < 1) || (dayOfWeek > 7))
									throw new Error("dayOfWeek must be in range 1..7");
								
									let weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
					
									return weekdays[dayOfWeek-1];
							}
					
Summary
  1. KISS -
  2. less lines of code
  3. code with less complexity
References
Thank you😘