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

CSC305 Lecture 03

CSC305 Lecture 03

Individual Software Design and Development
Code Clean II
(202409)

Javier Gonzalez-Sanchez

September 26, 2024
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 305 Individual Software Design and Development Lecture 03. Clean Coding II
  2. 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. • 3
  3. Dependency Injection 5 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 … }
  4. Readability • 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. 8
  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. 9
  6. Keep It Simple (KIS) • Aim for simplicity in your

    design a nd implement a tion. • Avoid unnecess a ry complexity or over-engineering. 10
  7. 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. 11
  8. 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. 12
  9. 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. 19
  10. Lab Working with your table. There are five versions (iterations)

    of my Paint App. Please review them Write a report (per version, per file): a) Readability (do you understand what is happening) b) DRY? KIS? SRP? c) What about names and comments Provide comments and feedback as needed. - what is good? What is bad?
  11. CSC 305 Individual Software Design and Development Javier Gonzalez-Sanchez, Ph.D.

    [email protected] Fall 2024 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.