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

CSC307 Lecture 06

CSC307 Lecture 06

Introduction to Software Engineering
Clean Coding
(202407)

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 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
  3. 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); } }
  4. 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
  5. 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
  6. Keep It Simple (KIS) • Aim for simplicity in your

    design a nd implement a tion. • Avoid unnecess a ry complexity or over-engineering. 34
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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…
  14. 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…
  15. 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
  16. 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
  17. 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.
  18. 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
  19. Homework 5. File Menu. We should be able to save

    and Load our draws. Let’s also add the option New (for cleaning all).
  20. Homework 8. Last but not least, let’s fix the problem

    of drawing right-to-left or bottom-up
  21. Homework 9. Do not forget the Class Diagram. Share it,

    i.e., include it in your submission
  22. 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.