I usually write technical material on my personal blog, but I will post it for the first time on Qiita according to the Advent calendar specifications! What I want to say in this article is that it is convenient to search or replace with regular expressions. (Actually, kotlin is awkward and doesn't really matter.)
It is to add a fixed character (◯ part) to the prefix (beginning) in Hungarian notation according to the variable naming convention.
int strHoge;
static textHoge;
The android-java style guide has a private field prefix of "m" and a static field prefix of "s" in the style guide, which is customarily used.
android-java style guide
public class Book {
private int mId;
private String mTitle;
private int mPrice;
public Book(final int id,final String title, final int price){
mId = id;
mTitle = title;
mPrice = price;
}
}
Don't use m-prefix and s-prefix Since the time of android-java, I've been thinking that it doesn't make much sense. That said, I personally use Hungarian notation a lot. There are various reasons why m-prefix and s-prefix are not included, but when using the standard Butter Knife in the binding library, the one to bind is private, so you will want to add m, but due to the binding specifications of Butter Knife, it becomes public and inconsistent. To do. Please refer to other sites for other reasons. Hungarian notation can be found in various ways by searching for negative / affirmative groups (vim vs emacs style), so I think it's interesting just to look at it. In short, it is better to use it properly depending on the time and the case.
There is no mention of adding m-prefix or s-prefix. In the case of android-java, I just used it customarily. So, when I make a new android application with kotlin from now on, I don't add a prefix in the first place. (Maybe some people make a mistake)
convert java to kotlin For those who have prefixed with java, m-prefix and s-prefix will remain if you use the function to automatically convert to kotlin in Android Studio environment.
before java
public class Book {
private int mId;
private String mTitle;
private int mPrice;
public Book(final int id,final String title, final int price){
mId = id;
mTitle = title;
mPrice = price;
}
}
after convert java to kotlin
class Book(private val mId: Int, private val mTitle: String, private val mPrice: Int){
}
It's a shame.
We will replace it with a regular expression that everyone is good at. You can also use regular expressions in Android Studio. The description of regular expressions is almost the same in all languages. Personally, I often use it with ruby.
Android Studio Regular Expression Syntax Reference (IntelliJ IDEA but same) https://www.jetbrains.com/help/idea/2016.2/regular-expression-syntax-reference.html#d1540635e834
Replace m-prefix in bulk. (For s-prefix, change (m) to (s))
Search character:([\s|\t]+)(m)([A-Z])([a-z]+)
Substitute character:$1\L$3$4
As shown in the image, Match case (case sensitive) and Regex (use regular expression) are required and should be checked. Since only the basic kotlin file is supported, it is recommended to put * .kt in the File mask.
I don't think you need a detailed explanation, so I will explain only the capture function. If you use () as a search character, you can capture (reuse) that character. The first () is $ 1 and looks like ... There are quite a few people who know regular expressions but don't know the capture function, so if you don't know it, it's convenient to try it.
If id such as mId → id overlaps with other local variables or reserved words, it will not work well, so even if you replace all at once, it is better to perform a regression test after conversion. It is safer to check the replacement target individually. It compiles, but may cause problems or errors at runtime.
It can be used not only when converting to kotlin but also when you want to delete the prefix from java, so please try it once. Regular expressions that aren't often used or used in Android Studio. In some cases, development efficiency will increase, so please use it positively!
All the code of the currently assigned project has been converted from java to kotlin. At first, I didn't know if I had a goal, so I continued to respond, and in fact I managed to respond in about 12 business days. (There is a history of failing once in the release and returning to java. Trauma) However, the automatic conversion function alone cannot do it at all, and the larger the project (the larger the amount of code), the more errors will occur. There were about 8000 errors, but as with non-null errors unique to kotlin, there were various code corrections and it was difficult. However, I'm glad that kotlin's functional specification was changed because it was very easy to use. Have a good kotlin life!
Recommended Posts