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
490
How useful Kotlin/Native in Kotlin 1.3
yashims
0
410
UX design trend 2019
yashims
5
1.5k
Kotlin/MPP getting started and troubles
yashims
0
3.9k
ココがダメだよWebCamTexture
yashims
0
86
Introduction of MaterialDesign for engineer
yashims
0
94
Other Decks in Technology
See All in Technology
Japan AWS Jr. Championsがお届けするre:Invent2024のハイライト ~ラスベガスで見てきた景色~
fukuchiiinu
0
1.1k
20250125_Agent for Amazon Bedrock試してみた
riz3f7
2
110
GitLab SelfManagedをCodePipelineのソースに設定する/SetGitLabSelfManagedtoCodePipeline
norihiroishiyama
1
120
企業テックブログにおける執筆ネタの考え方・見つけ方・広げ方 / How to Think of, Find, and Expand Writing Topics for Corporate Tech Blogs
honyanya
0
820
現実的なCompose化戦略 ~既存リスト画面の置き換え~
sansantech
PRO
0
170
インシデントキーメトリクスによるインシデント対応の改善 / Improving Incident Response using Incident Key Metrics
nari_ex
0
4.2k
例外処理を理解して、設計段階からエラーを「見つけやすく」「起こりにくく」する
kajitack
12
3.8k
さいきょうのアーキテクチャを生み出すセンスメイキング
jgeem
0
270
20250129 Findy_テスト高活用化
dshirae
0
230
消し忘れリソースゼロへ!私のResource Explorer活用法
cuorain
0
140
AIエージェントについてまとめてみた
pharma_x_tech
12
8.6k
2025/1/29 BigData-JAWS 勉強会 #28 (re:Invent 2024 re:Cap)/new-feature-preview-q-in-quicksight-scenarios-tried-and-tested
emiki
0
310
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Done Done
chrislema
182
16k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
19k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
Producing Creativity
orderedlist
PRO
343
39k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
220
Music & Morning Musume
bryan
46
6.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
175
51k
Agile that works and the tools we love
rasmusluckow
328
21k
Become a Pro
speakerdeck
PRO
26
5.1k
Building an army of robots
kneath
302
45k
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を変える方法があった ら誰か教えてください
ありがとうございました