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

CSC305 Summer Lecture 02

CSC305 Summer Lecture 02

Individual Software Design and Development
Clean Code
(202507)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info office: 14 -227 CSC 305

    Individual Software Design and Development | Summer Version Lecture 02. Clean Code
  2. (

  3. Topics covered in CSC 307, 308, and 309 5 Deployment

    Software Testing Coding, Programming, Developing Software Design Requirement Engineering [+]
  4. Not CSC 305, but Highly Relevant • Can you create

    the Product Backlog for the project described in Lab 1 • How many stories (user stories)? • Do you remember INVEST? • Priority? • Estimation? 6
  5. )

  6. Consistent Formatting • Follow a consistent code style and formatting

    guidelines. https://google.github.io/styleguide/javaguide.html • Use indentation, whitespace, and comments effectively to enhance readability. 9
  7. Meaningful Names • Use descriptive and unambiguous names for variables,

    functions, classes, etc. • Identifiers (name of variables and classes) are Nouns • Methods are Verbs • Avoid using misleading or cryptic names. • Follow consistent naming conventions. 10
  8. Commenting and Documentation • Write meaningful comments that explain why

    something is done, not what is done. • Keep comments up to date with code changes. • Document public APIs and complex logic thoroughly. • Single Line vs Multiple Line (and JavaDoc) comments 11
  9. Single Responsibility • Each class or function should have one,

    and only one, job. • Keep functions focused on a single task. • Break down your code into smaller, reusable modules or functions. • Ensure each module or function has a clear, well-defined purpose. • 14
  10. DRY

  11. Don't Repeat Yourself (DRY) • Avoid code duplication by abstracting

    common functionality. • Use functions or classes to encapsulate repeated jobs. 16
  12. KIS

  13. Keep It Simple (KIS) • Aim for simplicity in your

    design and implementation. • Avoid unnecessary complexity or over-engineering. 18
  14. Dependency Injection • An object’s dependencies (other objects it relies

    on) are provided externally rather than created internally by the object itself. • Constructor injection: Dependencies are passed via the class constructor. • Setter injection: Dependencies are provided through setter methods. • Make components more accessible to swap or extend without modifying the dependent class. 23
  15. Dependency Injection 24 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(); } }
  16. Dependency Injection 25 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 … }
  17. What to do Next? 27 Size of the File (#

    lines) GUI ! Color (Square)
  18. Lab

  19. CSC 305 Individual Software Design and Development Javier Gonzalez-Sanchez, Ph.D.

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