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

CSC305 Lecture 03

CSC305 Lecture 03

Individual Software Design and Development
Code Clean II
(202409)

Javier Gonzalez-Sanchez

September 26, 2024
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info office: 14 -227 CSC 305

    Individual Software Design and Development Lecture 03. Clean Coding II
  2. Week 01 • SRP : Each class should have one,

    and only one, clear, well-defined job. • Break down your code into smaller, reusable functions. • Keep functions focused on a single task. • Use comments wisely (JavaDoc, multi-line, single-line) • DRY • KIS 5
  3. Review Lab 01: Tic Tac Toe 6 Main Or Driver

    Player GameEngine GameBoard
  4. Review Lab 01: Tic Tac Toe 7 Driver Player Game

    public class Driver { public static void main(String[] arg) { Player player = new Player(); GameEngine game = new GameEngine(); game.ready(); do { player.move(); game.move(); } while (!game.isOver()); game.bye(); } } GameEngine GameBoard Main Or Driver
  5. Dependency Injection (DI) • An object’s dependencies (other objects it

    relies on) are provided externally rather than created internally by the object itself. • Constructor injection: Dependencies are passed via the class constructor. • Setter injection: Dependencies are provided through setter methods. • Make components more accessible to swap or extend without modifying the dependent class. 10
  6. • Body Level One • Body Level Two • Body

    Level Three • Body Level Four • Body Level Five "
  7. Dependency Injection 13 public class Driver { public static void

    main(String[] arg) { View view = new View (); Player player = new Player(view); Game game = new Game(view); game.ready(); do { player.move(); game.move(); } while (!game.isOver()); game.bye(); } }
  8. Dependency Injection 14 public class Driver { public static void

    main(String[] arg) { View view = new View (); Player player = new Player(view); Game game = new Game(view); game.ready(); do { player.move(); game.move(); } while (!game.isOver()); game.bye(); } } public class View { public void print(String s) { System.out.println(s); } } public class Game { View myView; public Game(View v) { myView = v; } public void ready() { myView.print (“Welcome!”); } // more code … }
  9. 115,475 Towns and Cities in the United States July, 2012

    https://www.math.uwaterloo.ca/tsp/data/usa/index.html Weka Example 25
  10. CSC 305 Individual Software Design and Development Javier Gonzalez-Sanchez, Ph.D.

    [email protected] Winter 2025 Copyright. These slides can only be used as study material for the class CSC305 at Cal Poly. They cannot be distributed or used for another purpose.