SE3070
Case Studies in Software
Engineering
Lecture 04
Best Practices & Refactoring
Techniques
Mr.Udara Samaratunge
Faculty of
Computing
Case Studies in Software Engineering (SE3070) 1
Department
SE3070| Case Studies in Software Engineering| UML for Case studies of
Design| Udara Samaratunge
Add Comments for Code
CSSE (SLIIT) by Udara Samaratunge 2
Usage of Loggers
CSSE (SLIIT) by Udara Samaratunge 3
Exception Handling
CSSE (SLIIT) by Udara Samaratunge 4
Imports and packaging
CSSE (SLIIT) by Udara Samaratunge 5
Usage of
Constants
CSSE (SLIIT) by Udara Samaratunge 6
Usage of Properties
• .properties is a file extension for files mainly used in Java related technologies
• This is used to store configurable parameters of an application.
• They can also be used for storing strings for Internationalization and localization.
• Known as Property Resource Bundles.
• Properties are configuration values managed as key/value pairs.
• In each pair, the key and value are both String values.
CSSE (SLIIT) by Udara Samaratunge 7
Usage of Properties
• Properties extends java.util.Hashtable. Some of the
methods inherited from Hash Table support the
following actions:
• Testing to see if a particular key or value is in the
Properties object
• Getting the current number of key/value pairs
• Removing a key and its value
• Adding a key/value pair to the Properties list
• Enumerating over the values or the keys
• Retrieving a value by its key
• Finding out if the Properties object is empty.
CSSE (SLIIT) by Udara Samaratunge 8
Read property file
Out put
CSSE (SLIIT) by Udara Samaratunge 9
Best Practices
File Name
Use InputStream and access
through class path
CSSE (SLIIT) by Udara Samaratunge 10
Remove deprecated methods
Don’t use Deprecated methods.
Instead search the most suitable
method
Instead use store()
method
CSSE (SLIIT) by Udara Samaratunge 11
Best Practice
CSSE (SLIIT) by Udara Samaratunge 12
Property to XML
File
Properties.xml
CSSE (SLIIT) by Udara Samaratunge 13
Pass parameters for Property file
• Use java.text.MessageFormat
• MessageFormat provides a means to produce concatenated messages in a language-
neutral way. Use this to construct messages displayed for end users.
• MessageFormat takes a set of objects, formats them, then inserts the formatted strings
into the pattern at the appropriate places.
• String java.text.MessageFormat.format(String pattern, Object... arguments)
• You can pass any number of arguments for the property file using MessageFormat and
you have to pass the String Pattern with specifying indexes in curly braces to display
passed parameters. Eg:- {0},{1}, {2}…etc
CSSE (SLIIT) by Udara Samaratunge 14
Create
Configuration.properties
Load
Configuration.properties
CSSE (SLIIT) by Udara Samaratunge 15
Response
CSSE (SLIIT) by Udara Samaratunge 16
XML Manipulations
CSSE (SLIIT) by Udara Samaratunge 17
Embed Queries into XML
CSSE (SLIIT) by Udara Samaratunge 18
HQL Queries in XML
CSSE (SLIIT) By Udara Samaratunge 19
Read Elements in XML query
CSSE (SLIIT) by Udara Samaratunge 20
Employee ID will be assessed as a parameter for xml
element
CSSE (SLIIT) by Udara Samaratunge 21
Inject Parameter to XML file
CSSE (SLIIT) by Udara Samaratunge 22
Response
CSSE (SLIIT) by Udara Samaratunge 23
Generate XML file
Response
CSSE (SLIIT) by Udara Samaratunge 24
Separation of Logic and refactor
considering cohesion and coupling
CSSE (SLIIT) by Udara Samaratunge 25
What is the Problem ???
Logics were not separated
Cohesiveness of the method?
CSSE (SLIIT) by Udara Samaratunge 26
Identify Logics to be
separated
1. Create XML document builder
2. Create Element
3. Add name to element
4. Create Attributes
5. Add attributes to Element
6. Transform response to XML
CSSE (SLIIT) by Udara Samaratunge 27
Separation of Logics
1. Create XML instance
2. Create Elements
3. Create Attributes
CSSE (SLIIT) by Udara Samaratunge 28
4. Add name to element
5. Add attributes to Element
6. Transform response to XML
MTIT (SLIIT) by Udara Samaratunge CSSE (SLIIT) 29
Build XML file invoking implemented methods and
append child elements accordingly
Response
CSSE (SLIIT) by Udara Samaratunge 30
Usage of Generics
• Implementation of Generic templates is
good practice in implementation
CSSE (SLIIT) By Udara Samaratunge 31
Bad Practice
• Generic template NOT USED properly
CSSE (SLIIT) By Udara Samaratunge 32
Required Code Refactoring
• Understood the generic design concept.
• But code was not refactored.
CSSE (SLIIT) By Udara Samaratunge 33
Generic Implementation
Outputs
CSSE (SLIIT) by Udara Samaratunge 34
Generic Implementation
CSSE (SLIIT) by Udara Samaratunge 35
Tips for development
• DataHashing secure class should be a utility. (Move to Util
Package)
• Don’t use “lib” as package name here.
• You should have proper packaging and naming conventions.
• All naming conventions should be documented and
everybody should follow it.
• Classes, methods, Comments and other implementation
should follow common template.
• Template should be documented & shared in the
repository.
• Learn how to use code repository.
• Use merge tools to merge large source files.
CSSE (SLIIT) By Udara Samaratunge 36
Implement Utilities
CSSE (SLIIT) By Udara Samaratunge 37
The End
CSSE (SLIIT) by Udara Samaratunge 38