Slide 1

Slide 1 text

Compare Java's 'var' With C#'s 'var' 2018.05.14 @Oracle Dev Tour Japan in Osaka by masanori_msl

Slide 2

Slide 2 text

• 'Local-Variable Type Inference' (JEP286). http://openjdk.java.net/jeps/286 • It infers the type of the local variable from the right side value. About Java's 'var' 1

Slide 3

Slide 3 text

About Java's 'var' 2 • It must be inferred the type from the right side value. // Compile error. var nullSample = null; // Compile error. var lambdaSample = () => { // Do something. }; // Compile error. var arraySample = {0, 2};

Slide 4

Slide 4 text

About Java's 'var' 3 • It shouldn’t use with the diamond operators. var sampleList = new ArrayList<>(); ↓ var sampleList = new ArrayList();

Slide 5

Slide 5 text

About Java's 'var' 4 • The type is inferred as implemented class. // List. List texts = new ArrayList(); // ArrayList. var texts2 = new ArrayList();

Slide 6

Slide 6 text

About Java's 'var' 5 • The variable of anonymous class can be declared with it. var person = new Object() { privateString name; public String getName(){ return name; } public void setName(String name){ this.name = name; } }; person.setName("masanori"); System.out.println(person.getName());

Slide 7

Slide 7 text

• C#'s 'var' was implemented in version 3.0. • Most of the features are as same as Java's. About C#'s 'var'

Slide 8

Slide 8 text

I have felt C# programmers are more actively seeking to use 'var' than Java programmers. Where does the difference come from? and should we use 'var'? Question

Slide 9

Slide 9 text

• It improve readability. • It induces better naming. • It makes me be able to treat the type ambiguous. Merits of using 'var' 1

Slide 10

Slide 10 text

Merits of using 'var' 2 public static void main( String[] args ){ // Even if the return value type is changed, // this code doesn't need modifications. for (var name: getNames()) { System.out.println(name); } } private static List getNames(){ return new ArrayList(); }

Slide 11

Slide 11 text

Demerits of using 'var' 1 • It reduces readability. // There is no infomations(´・ω・`). var v = c.m(); But you should not name the variable with the type name. // Don’t do this. var stringNames = c.m();

Slide 12

Slide 12 text

Demerits of using 'var' 2 • It risks type mismatch. // if the return value type is changed from float to int …? var point = getPoint(); So if the variable needs the precise type, you should write type explicitly.

Slide 13

Slide 13 text

• These situations are not much different between Java and C#. • C#'s 'var' has been implemented since over 10 years ago. • I don’t know if the situation of Java also will become as same as C#'s. Where does the difference come from?

Slide 14

Slide 14 text

I think we should use ‘var’ if there is no special reason. Because keeping code simple makes us to think other important parts of code. And the most important reason of my opinion is JetBrains ReSharper and Rider suggest using 'var' : ). So when I write C# code, I want to follow them. Should we use ‘var’ ?

Slide 15

Slide 15 text

• Because using ‘var’ has some merits and demerits, so please use it properly. • If the variable needs the precise type, please write the type explicitly. • If your team has coding rules, please follow them or update them first. Summary

Slide 16

Slide 16 text

Reference 1 ■Java • Style Guidelines for Local Variable Type Inference in Java http://openjdk.java.net/projects/amber/LVTIstyle.html • [Java] Style Guidelines for Local Variable Type Inference in Java - Oracle Blogs 日本語のまとめ https://orablogs-jp.blogspot.jp/2018/03/style-guidelines-for-local-variable.html • JEP 286: Local-Variable Type Inference http://openjdk.java.net/jeps/286 • Episode 72. A very deep dive on Var, and unmodifiable collections with Stuart Marks (@stuartmarks) himself! http://www.javapubhouse.com/2018/04/episode-72-very-deep-dive-on-var-and.html • Java10のvarをどう使うか - きしだのはてな http://d.hatena.ne.jp/nowokay/20180326 • 数カ月後の自分へ。僕がどういうときにJava10のvarを使いたいと思っているか。 - Mitsuyuki.Shiiba http://bufferings.hatenablog.com/entry/2018/04/03/004050 • Java varメモ - Hishidama's Java var Memo http://www.ne.jp/asahi/hishidama/home/tech/java/var.html • 【JDK 10】varを使った型推論 - Qiita https://qiita.com/chooyan_eng/items/f01c336359bc08cfa4b0

Slide 17

Slide 17 text

Reference 2 ■C# • C# Coding Conventions (C# Programming Guide) - Microsoft Docs https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions • Effective C# https://play.google.com/store/books/details/Bill_Wagner_Effective_C_Covers_C_6_0_includes_Cont?id=pz2cDQAAQBAJ • [雑記] 型推論の是非 - C# によるプログラミング入門 - ++C++; // 未確認飛行 C http://ufcpp.net/study/csharp/sp3_var.html • C# Debate: When Should You Use var? - InfoQ https://www.infoq.com/news/2008/05/CSharp-var • 第3回 varによる変数宣言とコレクション初期化子(1/4) - @IT http://www.atmarkit.co.jp/fdotnet/csharp30/csharp30_03/csharp30_03_01.html • c# - Why does ReSharper want to use 'var' for everything? - Stack Overflow https://stackoverflow.com/questions/1873873/why-does-resharper-want-to-use-var-for-everything • Misuse of the 'var' keyword in C# - Brad Smith's Coding Blog https://www.brad-smith.info/blog/archives/336

Slide 18

Slide 18 text

About me • Name:Masui Masanori • Work:Creating applications with Unity etc. • twitter:https://twitter.com/masanori_msl • blog:http://mslgt.hatenablog.com/