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 Code

Slide 2

Slide 2 text

Homework

Slide 3

Slide 3 text

Homework: Review Java Read The Java Tutorials (Links available on Canvas) As needed

Slide 4

Slide 4 text

Homework: Style Guide Read Java Style Guide https://google.github.io/styleguide/javaguide.html Apply it Always!

Slide 5

Slide 5 text

Clean Code

Slide 6

Slide 6 text

Summary 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Single Responsibility

Slide 10

Slide 10 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. • 10

Slide 11

Slide 11 text

Example 11

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Tic Tac Toe 13 Main Player Game

Slide 14

Slide 14 text

Tic Tac Toe 14 Main Player GameEngine GameBoard

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Style and Comments

Slide 17

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

Slide 18

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

Slide 19

Slide 19 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 19

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Do not Repeat Yourselves (DRY)

Slide 24

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

Slide 25

Slide 25 text

Keep It Simple (KIS)

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Dependency Injection

Slide 30

Slide 30 text

Dependency Injection 30

Slide 31

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

Slide 32

Slide 32 text

Tic Tac Toe 32 Main Player Game View

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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 … }

Slide 35

Slide 35 text

Questions 35

Slide 36

Slide 36 text

Lab

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Second Part | Programming Do not forget: https://google.github.io/styleguide/javaguide.html This is an important part of what I will be grading!

Slide 39

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