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.8k
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
97
Introduction of MaterialDesign for engineer
yashims
0
96
Other Decks in Technology
See All in Technology
HiMoR: Monocular Deformable Gaussian Reconstruction with Hierarchical Motion Representation
spatial_ai_network
0
110
Prox Industries株式会社 会社紹介資料
proxindustries
0
290
Github Copilot エージェントモードで試してみた
ochtum
0
100
Tech-Verse 2025 Keynote
lycorptech_jp
PRO
0
100
解析の定理証明実践@Lean 4
dec9ue
0
180
SalesforceArchitectGroupOsaka#20_CNX'25_Report
atomica7sei
0
170
mrubyと micro-ROSが繋ぐロボットの世界
kishima
2
260
Clineを含めたAIエージェントを 大規模組織に導入し、投資対効果を考える / Introducing AI agents into your organization
i35_267
4
1.6k
Javaで作る RAGを活用した Q&Aアプリケーション
recruitengineers
PRO
1
110
フィンテック養成勉強会#54
finengine
0
180
第9回情シス転職ミートアップ_テックタッチ株式会社
forester3003
0
230
あなたの声を届けよう! 女性エンジニア登壇の意義とアウトプット実践ガイド #wttjp / Call for Your Voice
kondoyuko
4
440
Featured
See All Featured
A better future with KSS
kneath
239
17k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
670
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Six Lessons from altMBA
skipperchong
28
3.8k
How GitHub (no longer) Works
holman
314
140k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
Building an army of robots
kneath
306
45k
Optimizing for Happiness
mojombo
379
70k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
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を変える方法があった ら誰か教えてください
ありがとうございました