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
550
How useful Kotlin/Native in Kotlin 1.3
yashims
0
440
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
98
Other Decks in Technology
See All in Technology
データアナリストからアナリティクスエンジニアになった話
hiyokko_data
2
440
KotlinConf 2025_イベントレポート
sony
1
120
ChatGPTとPlantUML/Mermaidによるソフトウェア設計
gowhich501
1
130
Platform開発が先行する Platform Engineeringの違和感
kintotechdev
4
550
2025年夏 コーディングエージェントを統べる者
nwiizo
0
140
ガチな登山用デバイスからこんにちは
halka
1
240
Practical Agentic AI in Software Engineering
uzyn
0
100
Autonomous Database - Dedicated 技術詳細 / adb-d_technical_detail_jp
oracle4engineer
PRO
4
10k
250905 大吉祥寺.pm 2025 前夜祭 「プログラミングに出会って20年、『今』が1番楽しい」
msykd
PRO
1
710
【実演版】カンファレンス登壇者・スタッフにこそ知ってほしいマイクの使い方 / 大吉祥寺.pm 2025
arthur1
1
810
Aurora DSQLはサーバーレスアーキテクチャの常識を変えるのか
iwatatomoya
1
890
Evolución del razonamiento matemático de GPT-4.1 a GPT-5 - Data Aventura Summit 2025 & VSCode DevDays
lauchacarro
0
170
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
112
20k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
580
Documentation Writing (for coders)
carmenintech
74
5k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Designing Experiences People Love
moore
142
24k
Git: the NoSQL Database
bkeepers
PRO
431
66k
Fireside Chat
paigeccino
39
3.6k
Making the Leap to Tech Lead
cromwellryan
135
9.5k
Gamification - CAS2011
davidbonilla
81
5.4k
Designing for Performance
lara
610
69k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
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を変える方法があった ら誰か教えてください
ありがとうございました