Slide 1

Slide 1 text

Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info o ffi ce: 14 -227 CSC 307 Introduction to Software Engineering Lecture 06. Clean Coding

Slide 2

Slide 2 text

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Slide 3

Slide 3 text

Homework 1. Due Monday: Quiz 2

Slide 4

Slide 4 text

Previously

Slide 5

Slide 5 text

Problem public class B implements E { public B() { C c1 = new C(); } public void method() { B b = new B(); b.sleep(); } } public class Y { A [] a = new A[5]; } public class A extends B { C c1, c2; public A() { c1 = new C(); } public void method() { D d = new D(); d.working(); } } public class X { public void m() { B var = new A(); double x – Math.sqrt(5); } }

Slide 6

Slide 6 text

Solution 6

Slide 7

Slide 7 text

CSC3100/Cloud-Services 7

Slide 8

Slide 8 text

More Tools

Slide 9

Slide 9 text

Tools We need a Tool

Slide 10

Slide 10 text

astah.net 10 https://astah.net/products/students/

Slide 11

Slide 11 text

File | New (projet) 11

Slide 12

Slide 12 text

Create Diagram | Class Diagram 12

Slide 13

Slide 13 text

Tools For the rest of this course Do your Class Diagrams in

Slide 14

Slide 14 text

Feedback A Paint App (version 1.0)

Slide 15

Slide 15 text

Do not forget User Experience 15

Slide 16

Slide 16 text

Clean unused lines 16

Slide 17

Slide 17 text

Clean unused lines 17

Slide 18

Slide 18 text

Caution using inheritance 18

Slide 19

Slide 19 text

Caution using inheritance 19

Slide 20

Slide 20 text

JavaDoc 20

Slide 21

Slide 21 text

JavaDoc 21

Slide 22

Slide 22 text

Recommended: Put a class in its own File 22

Slide 23

Slide 23 text

Feedback B Game of Life

Slide 24

Slide 24 text

How many objects in action 24

Slide 25

Slide 25 text

How many objects in action 25

Slide 26

Slide 26 text

How many objects in action 26

Slide 27

Slide 27 text

How many objects in action 27

Slide 28

Slide 28 text

How many objects in action 28

Slide 29

Slide 29 text

Constants, Parameters, Both 29

Slide 30

Slide 30 text

Thread, Runnable, and Timer 30 https:/ /github.com/CSC3100/Pacman

Slide 31

Slide 31 text

Clean Coding

Slide 32

Slide 32 text

Single Responsibility + Modularity • 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. 32

Slide 33

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Refactoring • Regul a rly ref a ctor code to improve its structure a nd re a d a bility without ch a nging its function a lity. • Simplify complex code a nd elimin a te code smells. 35

Slide 36

Slide 36 text

Error Handling • Implement robust error h a ndling a nd logging mech a nisms. • Use exceptions a ppropri a tely a nd a void silent f a ilures - keep the user informed! 36

Slide 37

Slide 37 text

Performance Considerations • 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. 37

Slide 38

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

Slide 39

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

Slide 40

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

Slide 41

Slide 41 text

Testing • Write a utom a ted tests to cover critic a l p a ths a nd edge c a ses. • M a int a in a comprehensive test suite a nd run it regul a rly. 41 To be continued…

Slide 42

Slide 42 text

Minimize Dependencies • Avoid unnecess a ry dependencies a nd keep extern a l libr a ries to a minimum. • Ensure dependencies a re up-to-d a te a nd well-m a n a ged. 42 To be continued…

Slide 43

Slide 43 text

Questions 43

Slide 44

Slide 44 text

Remember

Slide 45

Slide 45 text

Code is meant for human consumption 45

Slide 46

Slide 46 text

Manufacturing vs. Crafting

Slide 47

Slide 47 text

Together but not Scrambled

Slide 48

Slide 48 text

Lab 06. Clean Coding

Slide 49

Slide 49 text

Homework Due Monday: Paint-App 2.0

Slide 50

Slide 50 text

Homework 1. Clicking on a Shape allows you to select it. Then we can change its color by selecting a new one or Drag&Drop to change its location

Slide 51

Slide 51 text

Advice For selecting a line, consider something like this, then if the distance is "small" you can consider the line clicked (selected) // calculates the distance from P3(x3,y3) // to the line defined by P1(x1,y1) and P2(x2,y2) public static double distance (int x1, int y1, int x2, int y2, int x3, int y3) { double A = y2 - y1; double B = x1 - x2; double C = x2 * y1 - x1 * y2; double distance = Math.abs(A * x3 + B * y3 + C) / Math.sqrt(A * A + B * B); return distance; } 51

Slide 52

Slide 52 text

Homework 2. Drag&Drop. So, if the first click is on an empty space, we are drawing, but if the click is over an existing shape, we will drag it.

Slide 53

Slide 53 text

Homework 3. Add a menu Edit with the option Copy and the option Paste. By selecting a Shape and then Copy and then Paste a new Shape, to copied one appears 10px right and below the original one

Slide 54

Slide 54 text

Homework 4. KeyboardListener. For copy and paste, besides the menu Edit, we need CTRL+C and CTRL+V :)

Slide 55

Slide 55 text

Homework 5. File Menu. We should be able to save and Load our draws. Let’s also add the option New (for cleaning all).

Slide 56

Slide 56 text

Homework 6. We need Lines besides rectangle, Oval, and Arc.

Slide 57

Slide 57 text

Homework 7. Let’s add the About menu option and DialogBox

Slide 58

Slide 58 text

Homework 8. Last but not least, let’s fix the problem of drawing right-to-left or bottom-up

Slide 59

Slide 59 text

Homework 9. Do not forget the Class Diagram. Share it, i.e., include it in your submission

Slide 60

Slide 60 text

GUI 60

Slide 61

Slide 61 text

CSC 307 Introduction to Software Engineering Javier Gonzalez-Sanchez, Ph.D. [email protected] Summer 2024 Copyright. These slides can only be used as study material for the class CSC307 at Cal Poly. They cannot be distributed or used for another purpose.