  As the complexity and size of software projects increases in real-world
environments, maintaining and creating maintainable and dependable code becomes
harder and more costly. Refactoring is considered as a method for enhancing the
internal structure of code for improving many software properties such as
maintainability.
  In this thesis, the subject of refactoring software packages using community
detection algorithms is discussed, with a focus on the notion of package
stability. The proposed algorithm starts by extracting a package dependency
network from Java byte code and a community detection algorithm is used to find
possible changes in package structures. In this work, the reasons for the
importance of considering dependency directions while modeling package
dependencies with graphs are also discussed, and a proof for the relationship
between package stability and the modularity of package dependency graphs is
presented that shows how modularity is in favor of package stability.
  For evaluating the proposed algorithm, a tool for live analysis of software
packages is implemented, and two software systems are tested. Results show that
modeling package dependencies with directed graphs and applying the presented
refactoring method, leads to a higher increase in package stability than
undirected graph modeling approaches that have been studied in the literature.
