Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
C# code refactoring with Scope Functions
Search
Masaya Yashiro
September 26, 2018
Technology
0
2.9k
C# code refactoring with Scope Functions
Kotlin で標準実装されているScope FunctionのうちのいくつかをC#(Unity) 向けに移植した話。
Masaya Yashiro
September 26, 2018
Tweet
Share
More Decks by Masaya Yashiro
See All by Masaya Yashiro
拡大期を迎えたプロダクトに起きたこと - Android編
yashims
0
530
How useful Kotlin/Native in Kotlin 1.3
yashims
0
430
UX design trend 2019
yashims
5
1.6k
Kotlin/MPP getting started and troubles
yashims
0
4k
ココがダメだよWebCamTexture
yashims
0
100
Introduction of MaterialDesign for engineer
yashims
0
97
Other Decks in Technology
See All in Technology
Delta airlines Customer®️ USA Contact Numbers: Complete 2025 Support Guide
deltahelp
0
680
無意味な開発生産性の議論から抜け出すための予兆検知とお金とAI
i35_267
4
13k
Zephyr RTOSを使った開発コンペに参加した件
iotengineer22
1
220
Flutter向けPDFビューア、pdfrxのpdfium WASM対応について
espresso3389
0
130
KubeCon + CloudNativeCon Japan 2025 Recap Opening & Choose Your Own Adventureシリーズまとめ
mmmatsuda
0
270
SaaS型なのに自由度の高い本格CMSでサイト構築と運用のコスパ&タイパUP! MovableType.net の便利機能とユーザー事例のご紹介
masakah
0
110
マネジメントって難しい、けどおもしろい / Management is tough, but fun! #em_findy
ar_tama
7
1.1k
CRE Camp #1 エンジニアリングを民主化するCREチームでありたい話
mntsq
1
120
american airlines®️ USA Contact Numbers: Complete 2025 Support Guide
supportflight
1
110
タイミーのデータモデリング事例と今後のチャレンジ
ttccddtoki
6
2.4k
Geminiとv0による高速プロトタイピング
shinya337
1
270
Reach American Airlines®️ Instantly: 19 Calling Methods for Fast Support in the USA
flyamerican
1
160
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
Site-Speed That Sticks
csswizardry
10
690
We Have a Design System, Now What?
morganepeng
53
7.7k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Testing 201, or: Great Expectations
jmmastey
43
7.6k
Making the Leap to Tech Lead
cromwellryan
134
9.4k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
950
Adopting Sorbet at Scale
ufuk
77
9.5k
How to train your dragon (web standard)
notwaldorf
95
6.1k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
970
Transcript
スコープファンクションでコード の見通しを改善する 2018/09/26 Gotanda.unity #8 @yashims85
誰 • Twitter: @yashims85 • 会社: モバイルファクトリー@ 五反田 • Unity:
趣味でさわってる • 最近気になる言語: Rust • 緩募: 継続するダイエット
質問です 皆さんはイケてないな。。。 と思いながら、そんなもんだろうと諦めて コードを書いていることはありませんか?
• PBXProjectのプロパティ • 関数に外出しするほどではないちょっとし た計算 • Builder, Factoryパターンを採用するほど でもないちょっとした手順 •
大量のnullチェックもしくは ?. 地獄 私がイケてなぁと思いながらコードを書 いているとき
スコープファンクション もしかしたらスコープ ファンクションがそれ らの悩みを一助にな るかもしれません。
スコープファンクション • Kotlinの文脈で登場する • (基本的に)引数にLambdaを1つとって、そ の中で自己参照をする • C#のExtensionで作った関数のように全部 のObjectに生えてる
なるほど
いくつか移植してみた CSharpScopeExtensions: https://github.com/yashims/CSharpScop eExtensions ここでスター欲しいって言えば 優しい人がしてくれるって聞きました
導入方法 • releaseにunitypackageがおいてあるので ご自由にお使い下さい • もしくは簡単なのでコピペ
実際の実装 今回は諸般の事情により2つだけ紹介
Also
Also public static T Also<T>(this T self, Action<T> action) {
action(self); return self; }
Also public static T Also<T>(this T self, Action<T> action) {
action(self); return self; } ジェネリクスのExtensionなので、どの Objectからも呼び出せる
Also public static T Also<T>(this T self, Action<T> action) {
action(self); return self; } 自分自身を引数に取る Actionに自分 自身を渡す
Also public static T Also<T>(this T self, Action<T> action) {
action(self); return self; } 戻り値として自分を返す。
Let
Let public static R Let<T, R>(this T self, Func<T, R>
action) { return action(self); }
Let public static R Let<T, R>(this T self, Func<T, R>
action) { return action(self); } ジェネリクスのExtensionなので、どの Objectからも呼び出せる
Let public static R Let<T, R>(this T self, Func<T, R>
action) { return action(self); } 自分自身をFuncに渡し、Funcの戻り 値がLetの戻り値となる。
それがどしたの
実際の使用感をみてみる
Alsoの使い所 例えばインスタンスのプロパティ設 定
Alsoを使わない void Start() { Transform t1 = GetComponent<Transform>(); Quaternion q1
= new Quaternion(); q1.x = 0; q1.y = 0; q1.x = 0; t1.rotation = q1; }
Alsoを使う void Start() { Transform t2 = GetComponent<Transform>().Also((it) => {
it.rotation = new Quaternion().Also((that) => { that.x = 0; that.y = 0; that.z = 0; }); }); }
Alsoを使う • Start()のスコープを一時変数で汚さない • いくつかの処理が記述されてるようならコレはメリット • 引数の変数名を`it`等の代名詞にしておけば、何に対して の処理かが明確になり、それ以外に言及するコードを避 けられる •
コレを使えばPBXProjectの設定が超絶スッキリする
Letの使い所 例えばLINQのSelectの様に使う
Letを使わない Transform t = GetComponent<Transform>(); float distanceX = t.parent.transform.localPosition.x +
t.localPosition.x;
Letを使う float distanceX = GetComponent<Transform>().Let((it) => { return it.parent.transform.localPosition.x +
it.localPosition.x; });
Letを使う • LINQやRxのSelectと同じイメージで、Lambda内で変形を 行える • Alsoと同じく、一時変数でスコープを汚さない
Letの使い所 例えばC#6.xのnullableの unwrapping
Letを使わない string say(string comment) { return comment != null ?
$"I said: {comment}" : ""; }
Letを使う string say(string comment) { return comment?.Let(it => $"I said:
{it}") ?? ""; }
Letを使う • C#でのnull条件演算子「?.」は、nullチェックして、非null であればローカル変数に格納するのと同義 • それをLetの引数で受け取っているので、Letの中では Null安全な変数を自由に使える
できなかったこと
できなかったこと • Lambdaの中でthisコンテキストを変える • KotlinではLambdaの中ではthisがそれ自身になるス コープファンクションがある(例: Apply)
Apply(実現できたら) public static T Apply<T>(this T self, Action action) {
self.action(); return self; }
Apply(実現できたら) public static T Apply<T>(this T self, Action action) {
self.action(); return self; } ジェネリクスのExtensionなので、どの Objectからも呼び出せる
Apply(実現できたら) public static T Apply<T>(this T self, Action action) {
self.action(); return self; } TをthisとしてActionを呼び出す
Apply(実現できたら) public static T Apply<T>(this T self, Action action) {
self.action(); return self; } 自分自身を返す
Applyを使えたら void Start() { Transform t2 = GetComponent<Transform>().Apply(() => {
this.rotation = new Quaternion().Apply(() => { x = 0; y = 0; z = 0; }); }); } this以外の事について言及する余地 が無くなる
まとめ
まとめ • ScopeFunctionを使うと一時変数が減ら せ、コードの見通しが良くなる • ブロックで区切られることで、何について述 べられているコードかが簡潔になる • Lambdaの中でthisを変える方法があった ら誰か教えてください
ありがとうございました