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

CSC305 Lecture 02

CSC305 Lecture 02

Individual Software Design and Development
Clean Code
(202501)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

September 24, 2025
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 Code
  2. Clean Code • Write e ff icient code but prioritize

    re a d a bility a nd m a int a in a bility. • Optimize perform a nce-critic a l sections only when necess a ry. 7
  3. Clean Code :: Readability (Easy to read to others) •

    Single Responsibility Principle (SRP) • Style a nd Comments • Do not Repe a t Yourselves (DRY) • Keep It Simple (KIS) • Dependency Injection (DI) • Error H a ndling 8
  4. 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. • 10
  5. Some Ideas. It is NOT a Complete List 12 MOVE

    EAT SHOW DETECT COLLISION MOVE/ HUNT SHOW SHOW/ CREATE SCORE HANDLING WINNER DETECTOR MOVE/ HUNT
  6. Tic Tac Toe 15 Driver Player Game 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(); } }
  7. Consistent Formatting • Follow a consistent code style a nd

    form a tting guidelines. https://google.github.io/styleguide/javaguide.html • Use indent a tion, whitesp a ce, a nd comments e ff ectively to enh a nce re a d a bility. 17
  8. Meaningful Names • Use descriptive a nd un a mbiguous

    n a mes for v a ri a bles, functions, cl a sses, etc. • Avoid using misle a ding or cryptic n a mes. • Follow consistent n a ming conventions. 18
  9. Commenting and Documentation • Write me a ningful comments th

    a t expl a in why something is done, not wh a t is done. • Keep comments up to d a te with code ch a nges. • Document public APIs a nd complex logic thoroughly. • Single Line vs Multiple Line ( a nd J a v a Doc) comments 19
  10. Don't Repeat Yourself (DRY) • Avoid code duplic a tion

    by a bstr a cting common function a lity. • Use functions or cl a sses to enc a psul a te repe a ted jobs. 24
  11. Keep It Simple (KIS) • Aim for simplicity in your

    design a nd implement a tion. • Avoid unnecess a ry complexity or over-engineering. 26
  12. 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. 31
  13. Dependency Injection 33 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(); } }
  14. Dependency Injection 34 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 … }
  15. Lab

  16. Second Part | Programming Show me your best Java programming

    skills by developing the assigned code. I will describe the problem during lecture— attendance is expected and labs are not homework! Team of (max) 2. Individual work is OK
  17. 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.