Commit 073d25ef authored by sunye's avatar sunye
Browse files

Improve coding presentation

parent 0303d8e1
Pipeline #56577 passed with stages
in 2 minutes and 29 seconds
......@@ -98,19 +98,20 @@ public abstract class Application {
----
== Improving Assertions with AssertJ
== Improving Assertions with Atlanmod Commons
[source,java]
----
class Assertion {
public double calculate(int x, int y) {
assertThat(x).isNotZero();
assertThat(y).isLessThanOrEqualTo(10);
class Example {
// (...)
private double calculate(int x, int y) {
Preconditions.ensureThat(x).isNotZero();
Preconditions.ensureThat(y).isLessThanOrEqualTo(10);
// (...)
}
----
https://assertj.github.io/doc/[AssertJ - fluent assertions java library]
https://github.com/atlanmod/Commons[Commons - Coding and Testing Utility Library]
[.impact]
......@@ -130,7 +131,6 @@ image::defensive.png[]
== Guard Checking
* Most spread technique form of *Defensive Programming*.
* Guarantee that a method can be executed only when some requirements are met.
......@@ -151,8 +151,20 @@ public static void foo(String name, int start, int end) {
}
----
== Guards in Apache Commons
== Improving Guards with Atlanmod Commons
[source,java]
----
public static void foo(String name, int start, int end) {
// Guards
Guards.checkNotNull(name, "Name must not be null");
Guards.checkArgument(start < end, "Start (%s) must be smaller than end (%s)", start, end);
// Method body ...
}
----
== Guards in Apache Commons
[source,java]
----
......@@ -185,21 +197,14 @@ public static void foo(String name, int start, int end) {
TIP: https://github.com/google/guava/wiki/PreconditionsExplained[Google Guava]
== Guards in Atlanmod Commons
== Guards and Assertions
[source,java]
----
public static void foo(String name, int start, int end) {
.Different goals:
* Assertions are for situations that should never happen
* Guards are for expected errors
// Guards
Preconditions.checkNotNull(name, "Name must not be null");
Preconditions.checkArgument(start < end, "Start (%s) must be smaller than end (%s)", start, end);
// Do something here ...
}
----
TIP: Use Guards for *public* and Assertions for *private* methods!
TIP: https://github.com/atlanmod/Commons[Atlanmod Commons]
[.impact]
== Logging
......@@ -209,7 +214,10 @@ TIP: https://github.com/atlanmod/Commons[Atlanmod Commons]
.Technique for keeping a log of events that occur during the execution of a software:
* Events are logged according to a severity level hierarchy.
* For instance: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST
* For instance:
** JDK levels: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST
** Log4j levels: FATAL, ERROR, WARN, INFO, DEBUG, TRACE
== Logging in Java
......@@ -236,6 +244,20 @@ public MyClass {
}
----
== Using Log Levels
Lowest level (FINEST, TRACE):: very detailed info, e.g. the steps of an algorithm
Low level (FINER, DEBUG):: coarse-grained level info, e.g., entering and leaving method, throwing an exception
Medium level (INFO):: information that shows the progress of the application, e.g., the arrival of a request
Highest level (SEVERE, FATAL):: errors that force the application to stop (assertions)
== Remarks
TIP: Logs can be filtered by class and level!
WARNING: Logging impact performance!
[.impact]
== Automatic Code Generation
......@@ -401,3 +423,14 @@ def main():
sum_ = client.add(1, 1)
----
== Conclusion
* Defensive and Assertive programming
** Similar syntax but different goals
** Use Assertions for private and Guards for public methods
* Logging must be planned
** Too much information is counter-productive
** Use levels to simplify log analysis
** Use a different logger instance for each class
......@@ -64,5 +64,5 @@ Icons made by http://www.freepik.com[Freepik] from http://www.flaticon.com[Flati
* https://www.uml-diagrams.org[The Unified Modeling Language]
* http://www.agilemodeling.com/essays/umlDiagrams.htm[Introduction to the Diagrams of UML 2.X]
* https://junit.org[JUnit]
* «http://www.agilealliance.org/system/article/file/1423/file.pdf[Test-Driven Development]». Christoph Steindl.
* «http://www.slideshare.net/Skud/test-driven-development-tutorial[Test Driven Development Tutorial]». Kirrily Robert.
* http://www.agilealliance.org/system/article/file/1423/file.pdf[Test-Driven Development]. Christoph Steindl.
* http://www.slideshare.net/Skud/test-driven-development-tutorial[Test Driven Development Tutorial]. Kirrily Robert.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment