Object-Oriented Design Principles Static Code Analysis PMD (1) PMD

Object-Oriented Design Principles Static Code Analysis PMD (1) PMD

Object-Oriented Design Principles PMD (1) ● A static source code analyzer (written in: Java; license: BSD-style) https://pmd.github.io/ Péter Jeszenszky https://github.com/pmd/pmd Faculty of Informatics, University of Debrecen – Supported programming languages: ECMAScript [email protected] (JavaScript), Java, Scala, … Last modified: April 16, 2021 3 Static Code Analysis PMD (2) ● Static code analysis is the process of analyzing program ● Tool integration: code without executing it. – Apache Maven: ● Apache Maven PMD Plugin (license: Apache License 2.0) – Analysis can be aimed at: detecting errors and bugs, checking https://maven.apache.org/plugins/maven-pmd-plugin/ whether the code conforms to a coding standard, … – Gradle: ● The PMD Plugin (license: Apache License 2.0) ● Static code analyzer, static code analysis tool: an automated https://docs.gradle.org/current/userguide/pmd_plugin.html tool for performing static code analysis. – Eclipse IDE: ● pmd-eclipse-plugin (license: BSD-style) https://marketplace.eclipse.org/content/pmd-eclipse-plugin – Examples: https://github.com/pmd/pmd-eclipse-plugin ● PMD Plug-in (license: Eclipse Public License 2.0) https://acanda.github.io/eclipse-pmd/ ● Checkstyle https://checkstyle.org/ https://github.com/checkstyle/checkstyle https://github.com/acanda/eclipse-pmd/ ● PMD https://pmd.github.io/ https://github.com/pmd/pmd – IntelliJ IDEA: ● SpotBugs https://spotbugs.github.io/ https://github.com/spotbugs/spotbugs ● PMDPlugin (license: MIT License) https://plugins.jetbrains.com/plugin/1137-pmdplugin https://github.com/amitdev/PMD-Intellij ● QAPlug (license: proprietary) https://qaplug.com/ 2 4 DRY (1) DRY (3) ● Don't Repeat Yourself ● Categories of duplication: – – Imposed duplication: Developers feel they have no choice – “Every piece of knowledge must have a single, the environment seems to require duplication. unambiguous, authoritative representation within a – Inadvertent duplication: Developers don’t realize that they are system.” duplicating information. ● The opposite of DRY is WET: – Impatient duplication: Developers get lazy and duplicate because it seems easier. – “We enjoy typing”, “write everything twice”, “waste – Interdeveloper duplication: Multiple people on a team (or on everyone's time”, … different teams) duplicate a piece of information. ● Related concept: code duplication, duplicate code, copy- and-paste programming 5 7 DRY (2) DRY (4) ● Source: ● Duplicate code is identical (or very similar) – Andrew Hunt, David Thomas. The Pragmatic piece of source code that occurs more than Programmer: From Journeyman to Master. once in a program. Addison-Wesley, 1999. ● Not all code duplication is knowledge – David Thomas, Andrew Hunt. The Pragmatic duplication! Programmer: Your Journey to Mastery, 20th Anniversary Edition. Addison Wesley, 2019. https://pragprog.com/book/tpp20/the-pragmatic-pro grammer-20th-anniversary-edition ● Free chapter: DRY – The Evils of Duplication https://media.pragprog.com/titles/tpp20/dry.pdf 6 8 DRY (5) DRY (7) ● PMD support: Copy/Paste Detector (CPD) ● Example (continued): – Finding duplicated code with CPD – The violation can be eliminated by replacing the https://pmd.github.io/latest/pmd_userdocs_cpd.html length field with a method: – Supported programming languages: C/C++, C#, class Line { ECMAScript (JavaScript), Java, Kotlin, Python, Point start; Scala, … Point end; ● See: double length() { https://pmd.github.io/latest/pmd_userdocs_cpd.html#sup ported-languages return start.distanceTo(end); } } 9 11 DRY (6) DRY (8) ● DRY violations do not always take the form of ● Example (continued): duplicate code. – For performance reasons, one may choose to – DRY is about the duplication of knowledge. The violate the DRY principle. same piece of knowledge can be expressed in two ● In this case, the violation should be hidden from the totally different ways in two different places. outside world. – Example (Thomas & Hunt, 2019): class Line { Point start; Point end; double length; // DRY violation } 10 12 class Line { private Point start; private Point end; DRY (11) private double length; public Line(Point start, Point end) { this.start = start; ● Representational duplication (Thomas & Hunt, this.end = end; calculateLength(); 2019): } – public void setStart(Point p) { This duplication is inevitable. this.start = p; calculateLength(); – Tools that help to cope with this kind of duplication: } ● Tools that generate code from schemas (e.g., JAXB, JPA) public void setEnd(Point p) { ● this.end = p; OpenAPI calculateLength(); ● } … public Point getStart() { return start; } public Point getEnd() { return end; } public double getLength() { return length; } private void calculateLength() { this.length = start.distanceTo(end); }13 15 } DRY (10) KISS ● Representational duplication (Thomas & Hunt, ● “Keep it simple, stupid” 2019): – 1960s, U.S. Navy – Code often depends on the outside world: e.g., – The phrase is attributed to aeronautical engineer Kelly other libraries via APIs, data in external data Johnson (1910–1990). sources, that always introduces some kind of DRY ● The pursuit of simplicity: violation: code has to have knowledge that is also – Leonardo da Vinci (1452–1519): “Simplicity is the ultimate present in the external thing. sophistication.” ● It needs to know the API, or the schema, or the meaning – Ludwig Mies van der Rohe (1886–1969): “Less is more.” of error codes. – Albert Einstein (1879–1955): “Everything should be made as simple as possible, but not simpler.” 14 16 YAGNI (1) YAGNI (3) ● An acronym that stands for “You Aren't Gonna ● The costs of developing a feature that is not Need It”. need now (Martin Fowler): ● A principle of extreme programming (XP). 17 19 YAGNI (2) YAGNI (4) ● “Always implement things when you actually ● YAGNI only applies to capabilities built into the need them, never when you just foresee that software to support a presumptive feature, it you need them.” does not apply to effort to make the software – See: Ronald E. Jeffries. You’re NOT gonna need it! easier to modify. Apr 4, 1998. ● YAGNI is only a viable strategy if the code is https://ronjeffries.com/xprog/articles/practices/pracn otneed/ easy to change. ● See also: – Martin Fowler. Yagni. 26 May 2015. https://martinfowler.com/bliki/Yagni.html 18 20 Coupling (1) Coupling (3) ● Coupling: the degree to which a software ● Loose coupling: module depends on another software module. – It allows developers to write code that conforms to – In other words, coupling between software modules the open-closed principle (OCP), i.e., it makes code is a measure of how closely connected they are. extensible. – Coupling can either be loose or tight. – It makes code extensible, and extensibility makes it maintainable. ● Reference: – It allows parallel development. – Joseph Ingeno. Software Architect's Handbook. Packt Publishing, 2018. https://www.packtpub.com/application-development/ software-architects-handbook 21 23 Coupling (2) Law of Demeter (1) ● Tight coupling: ● The Law of Demeter was proposed by Ian Holland in 1987. – It increases complexity that makes modification of code more difficult, thus, it reduces maintainability. ● The law was named after the Demeter project in – It also reduces reusability. which Holland was working when he discovered it. ● Also known as: Don't Talk to Strangers – Craig Larman. Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development. 3rd ed. Prentice Hall, 2005. 22 24 Law of Demeter (2) Law of Demeter (4) ● References: ● Class form: – Karl J. Lieberherr, Ian M. Holland, Arthur Joseph Riel. – A method M of class C may use only members Object-Oriented Programming: An Objective Sense of Style. (methods and data) of the following classes and their Proceedings on Object-oriented programming systems, base classes: languages and applications (OOPSLA), pp. 323– 334, 1988. https://doi.org/10.1145/62084.62113 ● C ● data-member classes of C – Ian M. Holland, Karl J. Lieberherr. Assuring Good Style for Object-Oriented Programs. IEEE Software, vol. 6, no 5, pp. ● argument classes of M 38– 48, 1989. https://doi.org/10.1109/52.35588 ● classes whose constructors are called in M – Karl Lieberherr. Law of Demeter: Principle of Least ● the classes of global variables used in M Knowledge. http://www.ccs.neu.edu/home/lieber/LoD.html – Can be checked at compile time. 25 27 Law of Demeter (3) Law of Demeter (5) ● Restricts the message-sending structure of ● Object form: methods. – A method M of object O may use only members – Informally, the law says that each method can send (methods and data) of the following objects: messages to only a limited set of objects. ● O ● The goal of the Law of Demeter is to organize ● data members of O and reduce the dependencies between classes. ● argument objects of M ● objects that are created/instantiated directly in M ● objects in global variables – Can be checked only at runtime. 26 28 Law of Demeter (6) Law of Demeter (8) ● The law promotes maintainability and ● Related PMD ruleset: comprehensibility. – Design (Java) – It effectively reduces the methods that can can be https://pmd.github.io/latest/pmd_rules_java_design. called inside a given method and therefore limits html the coupling of methods. ● See the LawOfDemeter rule: https://pmd.github.io/latest/pmd_rules_java_design.html#l – It enforces one kind of information hiding (structure awofdemeter hiding): the internal structure of an object is known only by the object itself. 29 31 Law of Demeter (7) Law of Demeter (9) ●

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    24 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us