We have summarized the contents of LT announced in the past at weekend engineer.
It's been two years since I started programming, so I've summarized what I usually pay attention to when programming. I may update it from time to time.
If used in a mold, you lose. It can be said that the power of statically typed languages can be utilized only by mastering the power of typing.
Static typing means that in a program written in a programming language, the types of variables, subroutine arguments, return values, etc. are pre-determined before the program is executed, such as at compile time. It is the nature of the type system.
Dynamically typed languages such as JavaScript check the type when you run the program.
const str = 10;
const length = (str) => {
return str.length;
}
length(str);
// =>undefined is returned.
In comparison, statically typed languages check types before they run. At the code level, it is different from dynamically typed languages in that it declares the types of variables, function parameters, and return values.
Integer str = 10;
Integer length(String str) {
return str.length();
}
System.out.println(length(str));
// =>At compile time,In other words, an error occurs before execution.
Recently, editors sometimes give warnings even in dynamically typed languages such as JavaScript, but I think that is largely due to static parsing technology.
There are disadvantages to static typing. It is troublesome to specify the variable type in advance. It is also troublesome to cast (convert the variable type).
I know everything. I did too. But I recently realized that it indicates that ** I'm used for typing **. Statically typed languages make typing more aggressive. Define, ** I realize that the benefits come only when I use types **.
Below, I will write specifically how to use the type.
You can express more in your program by using your own defined types than by using primitive types such as String
and ʻInt`.
String phoneNumber = "000-0000-0000";
PhoneNumber phoneNumber = new PhoneNumber("000-0000-0000");
If you just use the String
type, you only know that the variable is a string.
From the variable of type PhoneNumber as in the second example, you can predict what kind of string is stored, and you can also imagine what kind of method can be called. In this case, I don't think I can call anything, but lol
The details of contract programming are more detailed in the following articles.
What I want to say here is that we can guarantee that variables are "not just strings, but strings in the form of telephone numbers, so they can behave without raising exceptions for certain instructions." It means that there are merits.
In the following class, when creating an instance of the PhoneNumber class, it checks whether it is a "phone number format string with a hyphen" and raises an exception. Paradoxically, a variable of type PhoneNumber always " The type can guarantee that the data has a "phone number format string with a hyphen".
/**
*Class that represents a phone number.
* String
*/
class PhoneNumber {
private static final Pattern pattern =
Pattern.compile("^0\\d{2,3}-\\d{1,4}-\\d{4}$");
private final String value;
/**
*Returns an instance of a new phone number type based on the hyphenated phone number specified in the argument.
* @param value phone number
* @return Phone number type instance
* @throws IllegalArgumentException If the argument is something other than a hyphenated phone number
*/
PhoneNumber(String value){
Objects.requireNonNull(value);
if (!value.matches(pattern)) {
throw new IllegalArgumentException();
}
this.value = value;
}
String getValue() {
return this.value;
}
}
Various methods and functions are defined for types provided by languages such as the String type. Do you really need all of them?
Is it appropriate to be able to get the character code or reverse the order of the strings for the phone number? Dare to define the type for the phone number by limiting the operations on the variables and preventing mistakes. It is also for.
On the contrary, it may increase the behavior. It is assumed that you get the local code of the telephone number. When substituting a telephone number for a String type variable and using it, the program will be as follows.
String phoneNumber = "000-0000-0000";
String cityCode = phoneNumber.split("-")[1];
If you define the behavior of returning the local code to the PhoneNumber type, the program will be as follows.
PhoneNumber phoneNumber = new PhoneNumber("000-0000-0000");
String cityCode = phoneNumber.getCityCode();
class PhoneNumber {
private final String value;
//Abbreviation
String getCityCode() {
return value.split("-")[1];
}
}
I've written the source code quite redundantly so far, but in reality, the code is coded using the annotations of Lombok and Bean Validation. Can be simplified.
/**
*Class that represents a phone number.
* String
*/
@Value
class PhoneNumber {
@NotNull
@Pattern(regexp="^0\\d{2,3}-\\d{1,4}-\\d{4}$");
private final String value;
}
So far I've written about micro units (phone numbers), but I don't think it's necessary to make molds in these units. Making molds in units that require integrity is a big advantage. In this article, all the input values from the screen are defined as a type and used as an argument of the calculation logic. Restrictions and guarantees by type and constructor I hope I can continue to utilize this method.
Recommended Posts