Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
naomod
Software Construction and Evolution (SCE)
Software Construction and Evolution Lectures
Commits
073d25ef
Commit
073d25ef
authored
Jan 06, 2022
by
sunye
Browse files
Improve coding presentation
parent
0303d8e1
Pipeline
#56577
passed with stages
in 2 minutes and 29 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/slides/asciidoc/coding.adoc
View file @
073d25ef
...
...
@@ -98,19 +98,20 @@ public abstract class Application {
----
== Improving Assertions with A
ssertJ
== Improving Assertions with A
tlanmod 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 l
ibrary]
https://
github.com/atlanmod/Commons[Commons - Coding and Testing Utility L
ibrary]
[.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 Comm
ons
== Guards
and Asserti
ons
[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
src/slides/asciidoc/index.adoc
View file @
073d25ef
...
...
@@ -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.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment