Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Good Code

barisdemiroz
November 21, 2013

Good Code

The presentation I made for software engineering course @{Bogazici University}

barisdemiroz

November 21, 2013
Tweet

Other Decks in Programming

Transcript

  1. • Function/Variable naming • Code style – Java conventions (Eclipse

    CTRL+ALT+F) – Google conventions coming soon!
  2. private void testNoCookie( Cookie[] cookies ) { when( request.getCookies() ).thenReturn(

    cookies ); CookieHandler handler = new CookieHandler( request ); assertFalse( handler.hasCookie() ); handler.createOrRenew( response ); assertTrue( handler.hasCookie() ); assertTrue( handler.getUUID().length() > 0 ); }
  3. @Test public void testNoCookie1() { testNoCookie( null ); } @Test

    public void testNoCookie2() { testNoCookie( new Cookie[] {} ); }
  4. public class Calculator { private AdderFactory adderFactory; public Calculator(AdderFactor adderFactory)

    { this.adderFactory = adderFactory; } public int add(int a, int b) { Adder adder = adderFactory.createAdder(); ReturnValue returnValue = adder.compute(new Number(a), new Number(b)); return returnValue.convertToInteger(); } }
  5. 1

  6. double calculate(int foo) { if (foo < 0) return 0;

    // rest of the computation… }
  7. public class LongDivision { private static final long MILLIS_PER_DAY =

    24 * 60 * 60 * 1000; private static final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; public static void main(String[] args) { System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); } }
  8. public class LongDivision { private static final long MILLIS_PER_DAY =

    24 * 60 * 60 * 1000; private static final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; public static void main(String[] args) { System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); } }
  9. public class LongDivision { private static final long MILLIS_PER_DAY =

    24 * 60 * 60 * 1000; private static final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; public static void main(String[] args) { System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); } } overflows
  10. public class LongDivision { private static final long MILLIS_PER_DAY =

    24L * 60 * 60 * 1000; private static final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000; public static void main(String[] args) { System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); } }