370

# CSC307 Lecture 06

Introduction to Software Engineering
Clean Coding
(202407)

July 04, 2024

## Transcript

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

CSC 307 Introduction to Software Engineering Lecture 06. Clean Coding
2. ### 1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

5. ### 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); } }

Diagrams in

32. ### 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
33. ### 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
34. ### Keep It Simple (KIS) • Aim for simplicity in your

design a nd implement a tion. • Avoid unnecess a ry complexity or over-engineering. 34
35. ### 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
36. ### 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
37. ### 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
38. ### 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
39. ### 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
40. ### 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
41. ### 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…
42. ### 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…

50. ### 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
51. ### 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
52. ### 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.
53. ### 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
54. ### Homework 4. KeyboardListener. For copy and paste, besides the menu

Edit, we need CTRL+C and CTRL+V :)
55. ### Homework 5. File Menu. We should be able to save

and Load our draws. Let’s also add the option New (for cleaning all).