2017-2018-2 20165315 Experiment 3 "Agile Development and XP Practice" Experiment Report
1. Coding Standard
An important realization in writing code is that "programs are mostly meant for people to see", and programming standards make code easier to read and understand, and even guarantee fewer bugs in it. Programming standards include: declarative names, clear expressions, straightforward control flow, readable code and comments, and the importance of using certain rules and idioms consistently when pursuing these.
Typography in coding standards is a good example, although typography does not affect the functionality of a program, it can affect readability. The layout of the program pursues clarity and beauty, which is an important factor in the style of the program.
——Teacher Lou " Agile Development and XP Practice "
- Specified canonical indentation:
We commonly see code like this:
public class CodeStandard {
public static void main(String [] args){
StringBuffer buffer = new StringBuffer();
buffer.append('S');
buffer.append("tringBuffer");
System.out.println(buffer.charAt(1));
System.out.println(buffer.capacity());
System.out.println(buffer.indexOf("tring"));
System.out.println("buffer = " + buffer.toString());
if(buffer.capacity()<20)
buffer.append("1234567");
for(int i=0; i<buffer.length();i++)
System.out.println(buffer.charAt(i));
}
}
The program does not have the most basic indentation, which makes it difficult to read, but it can be easily solved by using plugins in IDEA:
- open
Settings
->Plugins
->Browse repositories...
- Type in the search box
alibaba
to see theAlibaba Java Code Guidelines
plugin, clickInstall
to install it, and restart the IDE to take effect:
- Right-click on the project name, and select Coding Protocol Scan from the pop-up menu to scan out irregular codes:
A very important part of the code standard is how to name identifiers such as packages, classes, variables, methods, etc. A good name can make your code go up a notch immediately. The general naming rules in Java are:
- to reflect their meaning
- nouns for packages, classes, variables
- method name with verb object
- Package names are all lowercase, such as:
io
,awt
- The first letter of the class name should be capitalized, such as:
HelloWorldApp
- The first letter of the variable name should be lowercase, for example:
userName
- The first letter of the method name should be lowercase:
setName
- ...
Task 1: Use the tool (Code->Reformate Code) in IDEA to format the code and learn the functions of the Code menu
- Open the options in the toolbar
Code
, select theReformate Code
function, you can format the code
- By learning
Code
the menu, the basic functions are summarized as follows:Override Methods(Ctrl+O)
: Overload the method of the base class;Implement Methods(Ctrl+I)
: Complete the method of the current class interface or the interface of the abstract base class;Generate(Alt+Insert)
: Create getter and setter methods for any field in the class;Surround With(Ctrl+Alt+T)
: wrap the snippet withif-else
,try-catch
, etc.;do-while
Insert Live Template(Ctrl+J)
: execute some unrememberedLive Template
abbreviations ;Comment with Line Comment(Ctrl+斜杠)
: Comment (or uncomment) the current line or selected code block;Comment with Block Comment(Ctrl+Shift+斜杠)
: surround the selected block with block comment markers ("/* */")Reformat Code(Ctrl+Alt+L)
: Indent the code according to the standard format;Move Statement Up/Down
: move code statements up/down;Move Line Up/Down
: Move up/down the line where the cursor is located- ...
2. Agile development and XP
Pair programming is an important practice in XP. In the pair programming model, a pair of programmers develop work side by side, equally and complementary. They sit side by side at a computer, face the same monitor, use the same keyboard, and work together with the same mouse. They analyze together, design together, write test cases together, code together, do unit testing together, do integration testing together, write documentation together, etc.
There are two roles in pair programming:
- The driver is the person who controls the keyboard input.
- Navigator (Navigator) plays the role of navigating and reminding.
How to pair programming, why pair programming, let's refer to pair programming and cooperation between two people, the key points are:
- Driver: Write design documents, perform XP development processes such as coding and unit testing;
- Navigator: Review the driver's documentation, the driver's execution of development processes such as coding; consider the coverage of unit tests; think about whether and how to refactor; help the driver to solve specific technical problems;
- The pilot and navigator constantly rotate roles, do not work more than an hour continuously, and take a 15-minute break for every hour of work. The navigator has to control the time;
- Actively participate. Any task is first and foremost the responsibility of two people, but also the responsibility of all. no "my code", "your code" or "his/her code", only "our code";
- There is only a difference in level, there is no difference in level. The two are paired. Although everyone may have different levels of qualifications, both parties have equal decision-making rights in analysis, design or coding.
——Teacher Lou " Agile Development and XP Practice "
Task 2: Add your learning partner to your own project on the code cloud. After confirming that your partner's project has been added to your own, download the Complex code of partner experiment 2 and add no less than three JUnit unit test cases.
- After logging in to the code cloud, enter your own project, click
管理
, select after entering,项目成员管理
and add a partner to your project
- The code to find the partner is
Complex
as follows:
- Select the code of the connection partner in the IDEA
Git
option. After downloading,Complex
write no less than three Junit test classes against the code.
- The classes that need to be tested are:
getRealPart()
,getImagePart()
,Add()
,Sub()
,Multi()
,Div()
, upload the test code to the partner code cloud
3. Refactoring
Refactoring is to change the internal structure of the software to make it easier to read, maintain and change without changing the external behavior of the software.
——Teacher Lou " Agile Development and XP Practice "
A very key premise in refactoring is "not to change the external behavior of the software", which ensures that we will not bring new bugs to the original system while refactoring the original system, so as to ensure the safety of refactoring.
There are four motivations for us to modify the software without changing anything.
- Add new functions;
- The original function has BUG;
- Improve the structure of the original program;
- Optimize the performance of the original system.
What code needs to be refactored?
The refactoring functions provided by IDEA are in the Refactor
options:
A complete refactoring process includes:
- from the version control system codebase
Check out code
- Read and understand code (including test code)
- Discover
bad smell
Refactoring
- run all
Unit Tests
- into the codebase
Check in code
Task 3: Download your partner's code and do at least three refactorings
- select partner
Rational
code - Find four problems with this code
- The variable name does not conform to the naming rules
- The class name does not conform to the naming rules
- The variables in the class are all friendly variables, it is better to change them to private variables
- Redundant variables are defined
- According to the above problem, refactor the partner code
4. Java Cryptography
The Java security architecture is divided into 4 parts:
- JCA (Java Cryptography Architecture, Java Cryptography Architecture): JCA provides basic encryption framework, such as certificate, digital signature, message digest and key pair generator;
- JCE (Java Cryptography Extension, Java Encryption Extension Package): JCE has been extended on the basis of JCA, providing various encryption algorithms, message digest algorithms and key management functions. The implementation of JCE is mainly in the javax.crypto package (and its subpackages);
- JSSE (Java Secure Sockets Extension, Java Secure Sockets Extension): JSSE provides encryption based on SSL (Secure Sockets Layer, Secure Sockets Layer). In the process of network transmission, information will pass through multiple hosts (it is very likely that one of them will be eavesdropped), and finally transmitted to the receiver, which is not safe. This service to ensure network communication security is provided by JSSE;
- JAAS (Java Authentication and Authentication Service, Java Authentication and Security Service): JAAS provides the function of user authentication on the Java platform.
Java cryptography algorithm:
- Experience encryption and decryption - Caesar cipher
- Java Symmetric Encryption - DES Algorithm
- Java Asymmetric Encryption - RSA Algorithm
- Create a shared secret using a key agreement
- Java Digest Algorithm - MD5
- Java Hybrid Cryptosystem
Task 4: Complete the learning of Java cryptography related content in pairs, combined with refactoring, git, and code standards
After studying and studying various algorithms with our partner, we chose the MD5
algorithm:
Use Java to calculate the message digest of the specified string, java.security
the classes in the package MessageDigest
provide methods to calculate the message digest
First generate an object, execute its
update()
methods to pass raw data to the object, and then execute itsdigest( )
methods to get message digests. Specific steps are as follows:- Generate
MessageDigest
object:MessageDigest m=MessageDigest.getInstance("MD5");
- Pass in the string to be calculated:
m.update(x.getBytes("UTF8" ));
- Calculate the message digest:
byte s[ ]=m.digest( );
Process the calculation result (if necessary, you can use the following code to convert the calculation result s to a string)
String result=""; for (int i=0; i<s.length; i++){ result+=Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6); }
- Generate
Write the code as follows:
- Format and refactor source code
Product Code:
import java.io.UnsupportedEncodingException;
import java.security.*;
public class MD5Encrypt {
public static void main(String args[]) throws Exception {
String originalStr = args[0];
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
m.update(originalStr.getBytes("UTF8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte byteStr[] = m.digest();
GetResult getResult = new GetResult();
for (int i = 0; i < byteStr.length; i++) {
getResult.result += Integer.toHexString((0x000000ff & byteStr[i]) |
0xffffff00).substring(6);
}
}
public static class GetResult {
String result = "";
}
}
- Write test class code
/*测试输入输出*/
String str = new String(originalStr);
System.out.println("明文:" + str);
System.out.println("密文:" + getResult.result);
Problems encountered during the experiment and their solutions
- At first, I didn't know how to add a partner to my project. I thought it was building a project. The project members were only me and my partner:
Solution process:
After asking students, I know that I should invite a partner to become a developer of the project member in the management of my own project:
- When refactoring the partner's code, I spent a lot of thought, because the partner writes the code very carefully, and there are few places that need to be refactored
Solution process:
After careful study of refactoring and careful inspection of partner code, I still found several places that need to be refactored:
Experimental experience and summary
Through this experiment, I learned how to standardize and format code, how to add partners to my code cloud project and cooperate to write code, how to refactor and Java cryptography related knowledge.
In the process of hands-on practice, I have deepened my understanding of the knowledge points that are usually unclear, and have also mastered new knowledge points such as reconstruction. This experiment is the first real "pair programming", which made me deeply realize the truth of "everyone gathers firewood and the flame is high". Real-time observation of the code of the partner greatly improves the programming efficiency.
PSP (Personal Software Process) time
step | time consuming | percentage |
---|---|---|
step | time consuming | percentage |
demand analysis | 30min | 19% |
design | 30min | 19% |
Code | 60min | 38% |
test | 10min | 6% |
analysis Summary | 30min | 18% |
Code cloud link
My code cloud link:
https://gitee.com/BESTI-IS-JAVA-2018/ch1/tree/master/20165315exp2Partner's code cloud link:
https://gitee.com/BESTI-IS-JAVA-2018/20165310exp