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

CSC305 Lecture 02

CSC305 Lecture 02

Individual Software Design and Development
Clean Coding
(202409)

Javier Gonzalez-Sanchez

September 24, 2024
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info o ffi ce: 14 -227

    CSC 305 Individual Software Design and Development Lecture 02. Clean Coding
  2. Some Ideas. It is NOT a Complete List 5 MOVE

    EAT SHOW DETECT COLLISION MOVE/ HUNT SHOW SHOW/ CREATE SCORE HANDLING WINNER DETECTOR MOVE/ HUNT
  3. Single Responsibility • E a ch cl a ss or

    function should h a ve one, a nd only one, job. • Keep functions focused on a single t a sk. • Bre a k down your code into sm a ller, reus a ble modules or functions. • Ensure e a ch module or function h a s a cle a r, well-de f ined purpose. • 9
  4. Tic Tac Toe 12 Driver Player public class Driver {

    public static void main(String[] arg) { Player player = new Player(); Game game = new Game(); do { player.move(); game.move(); } while (!game.isOver()); } } Game
  5. Tic Tac Toe 13 public class Driver { public static

    void main(String[] arg) { Player player = new Player(); Game game = new Game(); game.ready(); do { player.move(); game.move(); } while (!game.isOver()); game.bye(); } }
  6. Dependency Injection • An object’s dependencies (other objects it relies

    on) a re provided extern a lly r a ther th a n cre a ted intern a lly by the object itself. • Constructor injection: Dependencies a re p a ssed vi a the cl a ss constructor. • Setter injection: Dependencies a re provided through setter methods. • M a ke components more a ccessible to sw a p or extend without modifying the dependent cl a ss. 14
  7. Dependency Injection 17 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 18 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. CSC 305 Individual Software Design and Development Javier Gonzalez-Sanchez, Ph.D.

    [email protected] Fall 2024 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.