cs5351-code-with-quality-v2-20191015.pptx - Code with Quality Dr W.K Chan Department of Computer Science Email [email protected] Website

cs5351-code-with-quality-v2-20191015.pptx - Code with...

This preview shows page 1 - 7 out of 63 pages.

1 Code with Quality Dr W.K. Chan Department of Computer Science Email: [email protected] Website:
Image of page 1
Outline Motivation Basic Design Principles at Code Level Code Smells Code Refactoring Homework Read the smells catalog: Read challenges in refactoring: see Section 3.2 of Ref [11] Refactor the code example on slide 45 2
Image of page 2
Coding for Present and for Future Following the popularity of Agile methods to software development, coding and testing are intermixed, and software designs are made incrementally rather than following a complete plan in a bird’s eye view. When we write code for one task or for one user story, our code is tied to that task or user story. The problem is that our code may be evolved (i.e., changed) to cater for changes in understanding on the task/user story or for support of other tasks/user stories. Our code should be clean, minimal, readable, maintainable. But How? Most of them you should be practicing. 3
Image of page 3
Basic Code Design Principles There are a few basic design principles recommended to developers when they write code. We look at the core set of these design principles. Abstraction Information Hiding Encapsulation 4 S - Single-responsibility principle O - Open-closed principle L - Liskov substitution principle I - Interface segregation principle D - Dependency Inversion Principle
Image of page 4
Example of Abstraction Principle 5 // balls[] is an integer array int n = balls.length; int temp = 0; for ( int i=0; i < n; i++){ for ( int j=1; j < (n-i); j++){ if (balls[j-1] > balls [j]){ //swap elements temp = balls [j-1]; balls [j-1] = balls [j]; balls [j] = temp; } } } A piece of code Abstraction int[] sort(int[] balls) Function signature Function body { // balls[] is an integer array int n = balls.length; int temp = 0; for ( int i=0; i < n; i++){ for ( int j=1; j < (n-i); j++){ if (balls[j-1] > balls [j]){ //swap elements temp = balls [j-1]; balls [j-1] = balls [j]; balls [j] = temp; } } } }
Image of page 5
Abstraction Principle Abstraction Principle is a fundamental concept in programming theory. Its aim is to avoid duplication of information and/or human effort. Let C be a piece of code with a well-defined purpose. e.g., C is for sorting an array of integers. Why not putting C into the body of a function having a function signature F ? e.g., the function signature F is int[] sort(int[] balls) We say that F is an abstraction of C . [More precisely, F is a functional abstraction of C ] A program P can include C once in the body of F and include one or more statements to call F instead of including multiple copies of C . Developers of the client code U of F need not to comprehend the code C ; rather they can rely on the abstraction F and its functional purpose to complete their own tasks (i.e.., write the code for their purpose).
Image of page 6
Image of page 7

You've reached the end of your free preview.

Want to read all 63 pages?

  • Fall '17
  • Ms MONG, Yu

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors