Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Clean Coding • 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. 2

Slide 3

Slide 3 text

Manufacturing vs. Crafting

Slide 4

Slide 4 text

Clean Coding Single Responsibility Principle (SRP) Do not Repeat Yourselves (DRY) Keep It Simple (KIS) Error Handling Readability (Easy to read to others) Style and Comments Dependency Injection (DI)

Slide 5

Slide 5 text

Style and Comments

Slide 6

Slide 6 text

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. 6

Slide 7

Slide 7 text

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. 7

Slide 8

Slide 8 text

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 8

Slide 9

Slide 9 text

Code 9 https://github.com/CSC3100/App-Paint/

Slide 10

Slide 10 text

Code 10 https://github.com/CSC3100/App-Paint/

Slide 11

Slide 11 text

DRY

Slide 12

Slide 12 text

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. 12

Slide 13

Slide 13 text

KIS

Slide 14

Slide 14 text

Keep It Simple (KIS) • Aim for simplicity in your design a nd implement a tion. • Avoid unnecess a ry complexity or over-engineering. 14

Slide 15

Slide 15 text

Code 15 https://github.com/CSC3100/App-Paint/

Slide 16

Slide 16 text

Code 16 https://github.com/CSC3100/App-Paint/

Slide 17

Slide 17 text

Single Responsibility

Slide 18

Slide 18 text

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. • 18

Slide 19

Slide 19 text

Example 19

Slide 20

Slide 20 text

Some Ideas. It is NOT a Complete List 20 MOVE EAT SHOW DETECT COLLISION MOVE/ HUNT SHOW SHOW/ CREATE SCORE HANDLING WINNER DETECTOR MOVE/ HUNT

Slide 21

Slide 21 text

Tic Tac Toe 21 Main Player Game

Slide 22

Slide 22 text

Tic Tac Toe 22 Main Player GameEngine GameBoard

Slide 23

Slide 23 text

Tic Tac Toe 23 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(); } }

Slide 24

Slide 24 text

Dependency Injection

Slide 25

Slide 25 text

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. 25

Slide 26

Slide 26 text

Dependency Injection What about text-based vs. GUI?

Slide 27

Slide 27 text

Tic Tac Toe 27 Main Player Game View

Slide 28

Slide 28 text

Dependency Injection 28 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(); } }

Slide 29

Slide 29 text

Dependency Injection 29 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 … }

Slide 30

Slide 30 text

Questions 30

Slide 31

Slide 31 text

Lab 01

Slide 32

Slide 32 text

Apply these Single Responsibility Principle (SRP) Do not Repeat Yourselves (DRY) Keep It Simple (KIS) Error Handling Readability (Easy to read to others) Style and Comments Dependency Injection (DI)

Slide 33

Slide 33 text

Lab 02 (Quiz)

Slide 34

Slide 34 text

Quiz (45 minutes - 15 questions) Let’s Test Your Java Programming

Slide 35

Slide 35 text

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.