[Android-Kotlin] Convert m-prefix and s-prefix, which are problems with java to kotlin, are removed by regular expression replacement.

Introduction

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.)

◯ -What is prefix?

It is to add a fixed character (◯ part) to the prefix (beginning) in Hungarian notation according to the variable naming convention.

Example

int strHoge;
static textHoge;

android-java coding convention

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.

Android-Kotlin coding conventions

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.

Remove m-prefix and s-prefix

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

One-shot replacement execution

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.

kotlin.png

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.

be careful

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.

Supervision

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!

Digression

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

[Android-Kotlin] Convert m-prefix and s-prefix, which are problems with java to kotlin, are removed by regular expression replacement.
Extract elements by doing regular expression replacement from a lot of HTML with java
I want to transition screens with kotlin and java!
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to implement various functions with kotlin and java!
I want to return to the previous screen with kotlin and java!
Replace with a value according to the match with a Java regular expression
[Android] Convert Map to JSON using GSON in Kotlin and Java
[Android] Convert Android Java code to Kotlin
<java> Split the address before and after the street address with a regular expression