Software Testing and Quality Assurance Chapter 03 Code Quality 1
Software Testing and Quality Assurance Chapter 03 Code Quality 1
Software Review
Software Review
• Software review is the second task in the software testing life cycle
• Its an activity mean to review/test the work and examined for feedback
• Review team helps in finding defects soon, to reduce the cost of fixing things in later stages.
• Review can be applied on all parts of the software
• Code
• Documents
• Producers
• Data
Software Review
SOFTWARE REVIEW
INFORMAL NON-FUNCTIONAL
Static Testing (Review)
• Code will not be executed in this type, it will be manually checked, line by line and evaluated.
• Also it will be a full review for all kind of documents in the software development cycle.
• Its important to find issues in the document, data and producers to avoid fixing error and
defect in the delivered application in later stages.
Informal Review
• Review is a written or verbal review requested by a developer of a work product to improve
that product
• Desk Check
• Peer Review
Desk Check
• Is an informal review where the author distributes a work product to peers for reviews and
comments.
• A desk check is the first line of defense against defects. You can speed up formal inspections by
taking care of simple defects in desk checks first.
• For many work products, desk checks suffice, and you might not need to go to a formal
inspection.
•
Peer Review
• is directed at reviewing short documents, chapters or parts of a report, a coded printout of a
software module, and the like.
• Peer reviews can take several forms and use many methods; usually, the reviewers are all peers,
not superiors, who provide professional assistance to colleagues.
• The main objective of inspections and walkthroughs is to detect as many design and
programming faults as possible.
•
Formal Review
• Review is a written review conducted by a team leader or a moderator for the purpose of
identifying, documenting, and fixing defects in a work product
• Formal Inspection
• Expert Opinion
• Audits
Formal Inspection
• An inspection is a formal review meeting where participants identify and document defects or
possible improvements in a work product
• Team may contain Moderator, Programmer, designer, QA Specialist.
• Some steps for a formal Inspections on: Preparation – Overview - Page-by-page Review -
Rework - Follow-up – Approval
Expert Opinion
• prepared by outside experts, support quality evaluation by introducing additional abilities to
the internal review staff.
• Outside experts transmit their expertise by either:
• Preparing an expert’s judgment about a document or a code section.
• Participating as a member of an internal formal inspection team.
•
Software Audits
• Is a type of software review in which one or more auditors who are not members of the software
development organization conduct "An independent examination of a software product,
software process, or set of software processes to assess compliance with specifications,
standards, contractual agreements, or other criteria.
• Software audit is a Certification, or third-party assessment (referred to as registration in some
countries), is carried out by an independent organization against a particular standard.
•
I C
Requirements Review E X T RA T OP
Requirements Review
• Requirements is a difficult process. An expected effect of complex work is the possibility of
introducing errors along the way. This is where the requirements review process comes in.
• A requirements review is a meeting where you gather all of your stakeholders together and
walk-through the requirements documentation, page-by-page, line-by-line, to ensure that the
document represents everyone’s complete understanding of what is to be accomplished in this
particular software.
• If software testing depends on good requirements, it is important to understand some of the
key elements of quality requirements
Requirements Review
• Verifiable
• Clear
• Complete
• Consistent
• Traceable
• Feasible
• Necessary
• Implementation Free
Verifiable
• Can be measured and tested
• The UI should be user friendly
• The user interface shall be menu driven. It shall provide dialog boxes, help screens, radio
buttons, dropdown list boxes, and spin buttons for user inputs.
Clear
• Requirements should be Simple sentences will most often suffice for a good requirement.
• Should be no more than 30-50 words in length
• Must be easily read and understood by non technical people.
• Must be unambiguous and not subject to multiple interpretations.
Clear
• The system shall be implemented using ASP.
• The system shall be implemented using Active Server Pages (ASP).
• Job poster enters job posting details and reviews them before saving.
• There are three requirements in that statement: There’s a requirement for entering the job
posting details. There’s a requirement for reviewing them. And there’s a requirement for saving
them.
• This requirement should be written in three requirements.
Complete
• All needed information about the requirement should be found.
• There should be no possibility for guessing.
• If the requirement represents a number or a quantity, a measurement unit should be defined
Example
• Professor user should login the system using username, password, and any other relative
information
• Professor user should login the system using username, password, and department code.
Consistant
• Does not conflict with other requirements
• Use the same terminology through all requirements
• Does not duplicate other requirements
Example
• Application should always start on home screen
• Users cannot use the application unless they login
• Conflict between these two requirements because the first one must start on home screen and
the second one tells that it should start on login screen
Traceable
• Requirements should have a unique identifier
• Requirement cannot be broken into smaller requirements
• Can be easily traced through design implementation and testing phase
Example
• Student information should be presented in details screen
• UR 4.1 | Student information should be presented in details screen
Feasible
• Must meet the exiting technology
• Can be achieved within the budget
• Can be achieved within the schedule
• Will be used by end-users
Code Quality
Code Quality
• Code Quality refers to the usefulness and maintainability of code in long term
• Code Quality can have a major impact on the software quality.
• To achieve high quality code there are several methods
• Code Review
• Coding Standards
• Refactoring
• Analysis
• Testing
Code Review
• Manual code reviews are important for verifying the intent of the code.
• When code reviews are done well, they improve overall software quality
Coding Standards
• A coding standard makes sure everyone uses the right style. It improves consistency and
readability of the codebase.
• This is key for lower complexity and higher quality
• Examples
• No more than one statement per line.
• Line length should not exceed 80 or 100 characters.
• More standards in Java https://ace.apache.org/docs/coding-standards.html * read only
Refactoring
• One way to improve the quality of an existing code is through refactoring.
• Refactoring code can help you clean up your code and lower its complexity.
Code Analysis
• The best way to improve quality is by analyzing code automatically.
• By running a static analyzer over code early and often, you’ll make sure the code that gets to
the code review phase is the highest quality possible.
Testing
• Testing has been considered as having a key role in building high quality code, and therefore it
has been widely used in practice.
Efficiency
• The efficiency of the code is directly related to the performance and speed of the software,
whereby the quality can be evaluated on its basis.
• The goal of such an exercise is to assess the quality of the software in question, not the quality
of the process used to develop the software which done in software review
• The objective of code review is to review the code, not to evaluate the author of the code. A
clash may occur between the author of the code and the reviewers, and this may make the
reviews unproductive.
Code Reviewer
• Reviewer checking:
• if the code is easy to understand
• Can be tested
• Contains Duplicated Code
• Contains Too Big Code
• Follow the Standards and Guidelines
Objectives of Code Review
• To achieve better code quality
• Finding Faults
• Knowledge transfer
• Increase the sense of mutual responsibility
• Finding better solutions
Code Review Process
REVIEW COMPLETE
SUBMIT FORM TO
DEVELOPMENT TEAM
Code Review
Over-the-shoulder Review
- Techniques
• An experienced team member walks through the new code, providing suggestions through a
conversation.
• It is the easiest approach to code reviews and does not require a pre-defined structure
• Such a code review may still be done informally today, along with a formal code review
process that may be in place
Code Review
Email Pass-Around
- Techniques
Code Review
Pair Programming
- Techniques
• Pair programming is a continuous code review process. Two developers sit at a workstation,
but only one of them actively codes whereas the other provides real-time feedback
Code Review - Tools
• There is a group of software tools can assist in code review process.
• Mainly Source Control / Git protocol used for code reviews.
Review Board
• is a web-based, open source tool for code review. To test this code review tool
w.reviewboard.org
o.reviewboard.org/r/
AHMED A. QAZZAZ OCT 2021
GitHub
• maintain your Git repositories on the cloud, you may have already used forks and pull requests
to review code
m/features/code-review/
AHMED A. QAZZAZ OCT 2021
Other Tools
Review Boar Visual Exper
Crucibl Gerri
GitHu Rhodecod
Phabricato Veracod
Collaborato Reviewabl
EXAMPLES
Code Review - Check for faults
• Logical faults: is a bug in a program that causes it to operate incorrectly
Division process will truncate the fraction since both inputs are integers
te Recursion
AHMED A. QAZZAZ OCT 2021
fi
fi
Code Review - GUI faults
• GUI Fault is used to identify the presence of defects is a software under test by using Graphical
user interface (GUI).
Code Review - Security Faults
• Is a type Testing that uncovers vulnerabilities, threats, risks in a software application and
prevents malicious attacks from intruders.
Performance Tools
• There are several tools can be used to profiling iOS and Android apps.
• Tools for Android profiling: https://developer.android.com/studio/profile/android-profiler
• Tools for iOS profiling: Xcode instruments (https://www.appcoda.com/xcode-instrument/)
Standards
• Standards aim to generate more consistent code on collaborative projects.
• Easier to understand
• Easier to develop
• Easier to maintain
• Reduce over all cost
Official Standards Sources
• Swift standards: https://engineering.vokal.io/iOS/CodingStandards/Swift.md.html
https://swift.org/documentation/
• Kotlin standards: https://kotlinlang.org/docs/reference/coding-conventions.html
• Java standards examples: https://android.jlelse.eu/java-coding-standards-ee1687a82ec2
Code Standards
Examples
• Solutions
• Passing variables around
• Store/Load locally
Code Standards
Examples
Code Standards
Examples
Code Standards
Examples
Code Standards
Examples
Code Style
• Code style refers to how a script formatted and written, it does not affect the functionality of
the code but it has a large impact on readability
• Indentation
• Parameter listing
• Naming
• Comments
• Coercions and Castings
Code Style
Examples (Swift)
Both of the following code will set a string in the variable pageLableText with the same
value
Code Style
Examples (Swift)
Code Style
Examples (swift)
Code Style
Examples (Java)
Code Style
Examples (Java)
Code Quality - Method
• Code Review
• Standereds
• Refactoring
• Code Analyzing
• Testing
Refactoring
• Is a technique of changing the code (to improve readability) without changing the code
functionality.
• Refactoring can be applied on code to make it match the standards.
• for example renaming a variable or class over the whole code.
Refactoring Examples
• Code on the left show a class for data model named Product with its constructor.
• but parameter n in the constructor might not be the best naming for this parameter
Refactoring Techniques
EXTRACTION MOVING RE ORGANIZING GENERALIZATION
SIMPLIFYING
Extract Method Example
Extract Subclasses
Bloaters
https://refactoring.guru/refactoring/smells/bloaters
Bloaters
Long Method
• Any method that is larger than 10 lines, or needs a scroll down
• The best treatment for this case, is to extract method
• if you need to write a comment about something inside the method
You should take this part to a sub method
AHMED A. QAZZAZ OCT 2021
AHMED A. QAZZAZ OCT 2021
Final Results
Large Class
• Class that contains tons for lines of code, that is over the
screen hight
• The best treatment for this case, is to
• Extract class
• Extract sub classes
• Extract interfaces
Large Class Example
• This class is 200 lines long
Final Results
Replace Parameters with Function call
Introduce Parameter Object
Switch Statement
• Using a complex switch operator, or a sequence of if statements
• When you see switch, you should think of polymorphism
• There are several refactoring methods to treat the switch problem
• Sub Classes
• State/Strategy Design Pattern
Use State / Strategy DP
Temporary Fields
• Fields in class that is not getting values all the time.
• The best treatment for this case, is to
• Extract class
OOP Abuse
Refused Bequest
• Subclass that uses only some of the parent class methods and fields
• The best treatment for this case, is to
• Extract Subclasses
Push Down Method
Divergent Change
• You need to change unrelated methods inside the class when you make
a different change
• for example, adding new product type, will make us change the search, display, and sorting
methods.
• The best treatment for this case, is to
• Extract classes
• Extract subclasses
AHMED A. QAZZAZ OCT 2021
Change Preventers
Shotgun Surgery
• Making a change will force us to make many small changes on
other classes
• The best treatment for this case, is to
• Move Methods
• Move Fields
• Inline Classes
AHMED A. QAZZAZ OCT 2021
Change Preventers
Parallel Inheritance
Hierarchies
Dispensables
https://refactoring.guru/refactoring/smells/dispensables
Dispensables
Lazy Class
• The class that is ridiculously small.
• for example class that holds one attribute
• Inline Class
• Collapse Structure
Dispensables
Duplicated Code
• Two code fragments doing the same function
• The best treatment for this case, is to
• Extract Methods
• Pull Up Methods
• Extract Subclasses
• …
Dispensables
Dead Code
• Code that is no longer used
• Remove These code segments
Couplers
https://refactoring.guru/refactoring/smells/couplers
Couplers
Feature Envy
• When a method in a class keep using an instance of another class,
it will be better to move it to the other class
• The best treatment for this case, is to Move Method
• if you need to write a comment about something inside the method
You should take this part to a sub method
Feature Envy
Messages Chain
• A message chain occurs when a client requests another object,
that object requests yet another one, and so on
• The best treatment for this case, is to
• Hide Delegates
• Extract Methods
• Move Methods
Couplers
Middle Man
• If a class performs only one action, delegating work to another class
• The best treatment for this case, is to
• Remove Middle Man Class
Other Smells
Other Smells
Magic Numbers
• When you use a constant number directly in the code, you should use symbolic (constant)
instead.
• What is applied for numbers can be applied for strings too
Example (Numbers)
Bad smell
Refactoring
Bad smell
Refactoring
Example
Refactoring
Bad smell
Code Analyzing
• software checks source code for compliance with a predefined set of rules or best practices
• The use of analytical methods to inspect and review source code to detect bugs has been a
standard development practice
Code Analyzing
• Following are the types of defects found by the tools during static analysis:
• A variable with an undefined value
• Inconsistent interface between modules and components
• Variables that are declared but never used
• Unreachable code (or) Dead Code
• Programming standards violations
• Security vulnerabilities
• Syntax violations
Code Analyzing
• Compiler Warnings and Errors
• Possibly uninitialized Variable
• Undocumented empty block
• Assignment has no effect
• Checkstyle
• Check for code guideline violations
• http://checkstyle.sourceforge.net
• FindBugs
• Check for code anomalies
• http://findbugs.sourceforge.net
Setup and Usage
https://medium.com/fantageek/how-to-setup-android-projects-8c306aaeb519
https://medium.com/developerinsider/how-to-use-swiftlint-with-xcode-to-enforce-swift-style-
and-conventions-368e49e910
SpotBug
• SpotBug: Java static analysis tool that focuses on Faults usage errors in code such as:
• null pointers
• useless/dead code
• unclosed I/O streams
• infinite loops
• infinite recursion
• Missing initialization
• Incorrect branching condition
• Missing test for null
Refectory
• Refactoring is a disciplined technique for improving the structure of existing code without
changing the observable behaviours
Testing
• Testing process will pass several phases, and it has its own documents.
• Test design is process to describe how test should be done.
• In the next chapter we will focus more on Testing
Dynamic Testing Types
• White Box testing
• Black Box testing
Test Cases and Test Scenarios
• You need to derive cases from your app for each functionality, and document these cases in standard
format
• for example, login service with username and email from mobile side perspective :
• {valid email, valid password}
• {invalid email, valid password}
• {valid email, invalid password}
• {invalid email, invalid password}
• {empty email, empty password}
• ….