470 likes | 636 Views
Class Analysis with Concept Lattices. Uri Dekel Department of Computer Science Technion, Haifa, Israel. Outline. Introduction Formal Concept Analysis Stage I – Interface Analysis Stage II – Implementation Analysis Stage III – Code Inspection Version Comparison
E N D
Class Analysis with Concept Lattices Uri Dekel Department of Computer Science Technion, Haifa, Israel
Outline • Introduction • Formal Concept Analysis • Stage I – Interface Analysis • Stage II – Implementation Analysis • Stage III – Code Inspection • Version Comparison • Overview of other FCA applications
Domain • Understanding and analyzing individual Java classes • Interface (black-box) analysis • Reducing the learning curve • Discovering interface problems • Implementation (white-box) analysis • Understanding class structure and role of fields • Discovering implementation problems • Code review and inspection • Understanding the purpose of each method from its code. • Ensuring style, quality, and correctness • Discovering code reuse opportunities • Version Comparison
Problems • Classes can be very large and complex • OOP practices promote use of many methods • Meyer’s “shopping list approach” advocates completing the interface with “syntactic-sugar” methods • “Rules of software evolution”: The entropy of software artifacts increases with time • Delocalisation • Definition order not meaningful Fact: A quarter of all public methods are found in classes with more than 100 methods !
Research Question • Can Formal Concept Analysis (FCA) help alleviate some of these problems? • FCA is a mathematical classification technique • Helps discover meaningful data in binary relations • Can be visualized with Concept Lattices • FCA has been applied to many CS and SW problems • Automatic modularization • Automatic construction and refinement of class hierarchies • Reverse engineering complex systems • Smart component repositories
Formal Concept Analysis • Input: A context <O,A,R> • O is a set of objects • A is a set of attributes • R is a binary relation between O and A • Mapping: Galois Connection • Common attributes of a set of objects: • Common objects of a set of attributes: • Output: Concepts s.t.
FCA Example • Field-accesses context of a class • Objects are fields, attributes are methods, relation specifies which methods access each field Context: Concepts:
Concept Lattices • Partial order: • Defines domination between concepts • Visualized as a concept lattice
Interpreting Class Lattices • We use only sparse lattices • Economical but equivalent representation • Each object introduced in lowest concept • Each attribute introduced in highest concept • Interpretation: • Each method uses all fields introducedin the same concept or below • Reveals: • Possible restructuring • Asymmetry between coordinates
Field-Accesses Context • Field usage is critical for understanding a class • All implementations of an operation use the same fields • Representation changes are rare • Methods that use the same combination are related • Can be calculated directly from the .class file • Allows some reverse engineering without source code • Calculated using standard static analysis • Currently restricted to accesses inside the class
Lattices vs. Tables • The lattice and the accesses table contain exactly the same information! • Advantages of the table: • It is immediately clear what fields are accessed by each method. • Advantages of the lattice: • Related methods appear together. Makes it easier to: • Discover what exactly each method does. • Discover duplicate methods. • Find inconsistencies. • Determine level of abstraction.
Graph example • Accesses tables (only a part is visible)
Class Assignment • Try to find as many problems as possible in the Molecule class. • Examples: • Duplicate methods. • Different methods that do the same thing (not composites!). • Inconsistencies in types and names between methods. • Asymmetries in the interface. • Invariants that are violated. • Methods which do not access the fields you expect them to. • Assume that: • All methods are documented. • Some methods declare and throw exceptions.
Zoom-in Zoom-out approach • Problems: • Concept lattices can be very large • Number of concepts is bound by • Polynomial for most real-life contexts • Linear for 99.5% of classes! • Elaborate member details are cumbersome • Solution: • Provide (semi-) automatic zoom in/out tools
Running Example • The Molecule class from CDK • CDK: Chemistry Development Kit • Open source library of chemistry related classes • Developed at the Max Plank institute in Germany • Used in chemistry visualization applications • Why the Molecule class? • Has a large interface (nearly 75 public members) • The represented entity is familiar to most people • Methodology was successfully applied to other classes as well Our methodology revealed several new bugs and issues !
Stage I: Interface Analysis “Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning…” --Rich Cook “There are only two industries that refer to theircustomers as ‘users’…” -- Edward Tufte
Interface Analysis • Purpose: • Understand the functionality provided by the class • Map expectations into interface members • The “concept assignment” or “feature mapping” problems • Discover problems • e.g. missing or superfluous functionality, exposed implementation details, inconsistent naming • Methodology: • Methods are partitioned into concepts • Heuristic for automatic feature categorization • Zoom-out and reason about overall structure • Zoom-in and examine specific functionalities
Preliminaries • Mapping features to interface members requires knowing what the features are • Tasks: • Surmising abstraction, purpose and role • Determining vocabulary • Predicting mandatory- and non-mandatory functionality • Information sources: • Domain-specific knowledge • Class environment • E.g. hierarchy, dependencies, etc. • This step is not unique to concept analysis
Context Selection • Only client-visible methods should be used • Public methods by default, protected if client is subclass, default if client is in the same package • All fields are kept to ensure a correct partitioning • Will be removed after the lattice is constructed • Context parameters: (boldface indicates selection) (bold indicates our selection, Φ represents”don’t care” )
Constructing the Lattice • The lattice is too cluttered to grasp immediately • We start zooming-out • Layers correspond to levels of abstraction
Simplifying concepts • We summarize the responsibilities of each concept in a quick skim over method signatures • This process cannot be fully-automated at present • Still too cluttered !
Naming Concepts • Name concepts based on summary • Use symbolic representations for common responsibilities
Horizontal Decomposition • Remove top- and bottom- concepts • Connected components are orthogonal • Problem with title (on the right) becomes obvious • Abundance of trivial components implies record-like behavior • Cohesive component requires further analysis
Abstraction Lattice • Heuristic for clustering concepts • Concepts dominated by the same top-layer concepts belong in the same cluster
Match services against expectations • Functionality search order: • Expected mandatory features • Expected non-mandatory features • Unexpected features • For each functionality: • Mark relevant clusters • Mark relevant concepts • Examine each concept • Example: • Bond management
Stage II – Implementation Analysis "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies.” C. A. R. Hoare
Implementation Analysis • Purpose: • Understand implementation and structure. • Discover problems • e.g. redundant fields, bad naming conventions, wrongly-implemented operations • Methodology: • Code is not inspected at this stage! • All information derived from lattice • Zoom-in: • Including private fields and methods • Listing full signatures and introducing classes • Embedded call-graph
Embedded Call Graph • Superposition of call-graph on concept lattice • A semantics-based CG layout heuristic • Keeps related methods together while reducing crossings • Helps investigate relations between methods • e.g. surmise level of abstraction or discover wrappers • Used later for selecting an order for code inspection • Example: ECG of Pnt3D
Investigate Fields • Examine unused fields • Might indicate unimplemented stubs or dead structure • Discover the roles of fields • Easy for trivialcomponents • Harder for thecohesive one • Investigateinterdependency • Naming quality
Investigate Special Methods • Methods that (should) use the entire state should be in the top concept • Exceptions can indicate problems • Zoom-in by adding declaring class details • Examine methodsthat do not use fields • e.g. discoverundeclared statics
Investigate Other Methods • Ensure symmetry where expected • e.g. C11 and C13, C10 and C14, C16 and C17 • Ensure methods use expected access patterns • Add non-publicmethods to lattice
Stage III – Code Inspection “Real programmers don't document. If it was hard to write, it should be hard to understand…” --Anonymous “Real programmers can write assembly code in any language…” --Larry Wall
Code Inspection • Purpose: • Understand functionality which is unclear after the previous stages. • Ensure quality of code and style • Methodology: • Select an order for effective reading • Maximizing reading throughput • Maximizing discovered defects • Minimizing repetitions
Code Inspection Problem • Original source code order not effective • Co-definitions. • No incremental order • All class members are defined simultaneously • Perturbations to intended order • Evolution and maintenance • Language issues (e.g. inheritance) • Style issues (e.g. public before private)
Reading Strategy • Organize methods into groups of related functionality and order these groups (global order) • Order the methods inside each group (local order) • Each concept is a group • Same-concept methods are similar in purpose, semantics and implementation • Increased prospects of understanding differences between methods and discovering redundancies and replications • Less infrastructure (e.g. external libraries) to memorize
Reading Strategy • Global order (by importance) • Read each HD component separately • Each represents an independent functionality • Read concepts in ascending order of layers • Exploit similar level of abstraction • Read concepts of the same cluster together • Local order (by importance) • Read methods in topological order • Use restricted ECG • Read methods in same ECG component together • Resolve equivalencies with “simplest-first” rule
Inspection Tasks • Inspection tasks customized for our reading order • Finding duplicate services inside a concept • e.g. getDegree and getBondCount • Identifying code-sharing opportunities • e.g. overloads of addBond • Verify that low-level methods are not bypassed • e.g. getBondCount, getBondAt • An addition to “standard”inspection tasks
Version Comparison “Zero defects: The result of shutting down a production line…”--Kelvin Throop III, "The Management Dictionary"
Version Comparison • Examine an outline of the differences before the actual details • Example: • Also useful for subclass/superclass comparisons Differences between the original version of the “Graph” class of VGJ (Visualizing Graphs with Java) and the Technion adaptation of that class. Originals appear in bold font, Modifications appear in plain font
Hierarchy Construction • Godin and Mili (93) classified Smalltalk classes: • Objects: Names of concrete collection classes • Attributes: Names of messages that these classes accept.
Hierarchy Construction (cont.) • Output: Multiple inheritance class hierarchy
Hierarchy Construction (cont.) • There are four types of concepts: • Concrete concepts • Introduce both attributes and objects • Intersect concepts • Introduce objects but no attributes • Abstract concepts • Introduce attributes but no objects • Connector (“empty”) concepts • Do not introduce objects or attributes • Can be removed!
Hierarchy Construction (cont.) • Hierarchy after removing connectors and naming abstract concepts:
Other Applications • Modularizing legacy code • Objects: Global variables. • Attributes: Functions. • Lattice is horizontally decomposed, resulting in modules. • Managing component repositories: • Objects: Software components. • Attributes: Text-based properties or features. • Lattice includes all search paths.
The End “Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programming combines theory and practice: Nothing works and you don't know why…” -- Anonymous