Programming - SeasStarsRoses/IT GitHub Wiki

Table of Contents


1. KISS

  • KISS means Keep it small and simple
  • Make it as simple as possible, but not simpler (Albert Einstein)

2. DRY

  • DRY means Don't repeat yourself
  • The opposite is WET:
    • Write everyting twice
    • We enjoy typing
    • Waste everyone's time

3. Readability

3.1 Parameters

  • What's the problem with the following code?
  Rook rook = new Rook(true);
  • Problem: The code is hard to read. What is the meaning of the boolean parameter value true?
  • Solution: Use enumerations instead of booleans as parameters:
enum COLOR { BLACK, WHITE }
Rook rook = new Rook(COLOR.WHITE}

3.2 Conditions

Question: What is the output of the following java code?

   int i = 0; int j = 1;
   if (i == 1)
      if (j==1)
         System.out.println("j == 1");
   else
      System.out.println("i != 1");

Answer: Nothing. Reason: The else belongs to the inner if. Solution: Always write parentheses in if statements. Format your code correctly

   int i = 0; int j = 1;
   if (i == 1) {
      if (j==1) {
         System.out.println("j == 1");
      }
      else
      {
         System.out.println("i != 1");
      }
   }

4. Lines of code

  • Hypotheses: Mimimize your lines of code. Except you get payed for it. Less code are less errors.
  • Question: How would you write code to validate an E-Mail address?
  • Answer: Use a regular expression.
  • Example: '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,}\b'