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
文字列操作の達人になる ~ Kotlinの文字列の便利な世界 ~ - Kotlin fest 2025
tomorrowkey
2
390
データとAIで明らかになる、私たちの課題 ~Snowflake MCP,Salesforce MCPに触れて~ / Data and AI Insights
kaonavi
0
230
ストレージエンジニアの仕事と、近年の計算機について / 第58回 情報科学若手の会
pfn
PRO
4
950
今のコンピュータ、AI にも Web にも 向いていないので 作り直そう!!
piacerex
0
360
組織全員で向き合うAI Readyなデータ利活用
gappy50
5
2k
ゼロコード計装導入後のカスタム計装でさらに可観測性を高めよう
sansantech
PRO
1
650
プロダクト開発と社内データ活用での、BI×AIの現在地 / Data_Findy
sansan_randd
1
760
サブドメインテイクオーバー事例紹介と対策について
mikit
11
3.3k
次世代のメールプロトコルの斜め読み
hirachan
3
270
GCASアップデート(202508-202510)
techniczna
0
240
Kotlinで型安全にバイテンポラルデータを扱いたい! ReladomoラッパーをAIと実装してみた話
itohiro73
3
140
AWSが好きすぎて、41歳でエンジニアになり、AAIを経由してAWSパートナー企業に入った話
yama3133
2
220
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
246
12k
Git: the NoSQL Database
bkeepers
PRO
431
66k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.7k
The World Runs on Bad Software
bkeepers
PRO
72
11k
Embracing the Ebb and Flow
colly
88
4.9k
GraphQLとの向き合い方2022年版
quramy
49
14k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.8k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.7k
Context Engineering - Making Every Token Count
addyosmani
8
330
Unsuck your backbone
ammeep
671
58k
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を変える方法があった ら誰か教えてください
ありがとうございました