-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#38 create date time method #89
Conversation
…r-app into #38_create_date_time_method
@@ -6,4 +6,40 @@ extension DateTimeExtension on DateTime { | |||
|
|||
/// 「yyyy年MM月dd日 HH時mm分」形式の文字列を返す。 | |||
String formatDateTime() => DateFormat('yyyy年MM月dd日 HH時mm分').format(this); | |||
|
|||
/// 現在時刻と比較して、相対的な日付文字列を返す。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc string を書いているのはとても素晴らしいと思います!(たとえば例とかも書いちゃっていいかもですね、Chat GPT にこの実装をまるっと投げて、doc string 書いてください、とか言うと結構いい仕事してくれます)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!
GPT先生の力も借りながら、doc stringを加筆修正しました!
return formatDate(); | ||
} | ||
|
||
bool _isToday(DateTime now) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このメソッド内の処理を見ると「引数で渡された DateTime
が、this
と同じ日付か」という処理になっていますね。そうだとしたらメソッド名は _isToday
ではなく _isSameDay
のような名前になるべきだと思います。
_isToday
のメソッド名通り、「this
が今日かどうか」というメソッドにしたいのなら、DateTime now
は外から渡すのではなく、_isToday
の処理内部にあるべきだと思います!
意図しているのはどちらでしょうか?(実装するときに、doc comment に「「引数で渡された DateTime
が、this
と同じ日付かどうかの真偽値を返す」とか「「this
が今日かどうかの真偽値を返す」とかを、事前に書く癖をつけると、自分のやりたいこと・意図を明確にしたコードを書けるようなり、doc comment もついて一石二鳥だと思います!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにーーーーとなりました・・・
「this が今日かどうか」というメソッドにしたいという意図だったので、修正しました。
加えて、doc stringも追加しました。
実装するときに、doc comment に「「引数で渡された DateTime が、this と同じ日付かどうかの真偽値を返す」とか「「this が今日かどうかの真偽値を返す」とかを、事前に書く癖をつけると、自分のやりたいこと・意図を明確にしたコードを書けるようなり、doc comment もついて一石二鳥だと思います!
意識していきます!!!!
return year == now.year && month == now.month && day == now.day; | ||
} | ||
|
||
bool _isYesterday(DateTime now) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_isToday
と同様。
- 「引数で渡された DateTime が、this の 1 日前の日付か」
- 「this が昨日かどうか」
を考えて同様の修正をお願いします!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_isToday同様に、「this が昨日かどうか」が意図だったので修正しました。
} | ||
|
||
/// 現在と比較して2日〜7日前ならその数字を、そうでなければnullを返す | ||
int? _daysBefore(DateTime now) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_isToday
と同様。DateTime now
を外から渡すので意図どおりか確かめる。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_isToday
と同様に修正しました。
/// 現在と比較して2日〜7日前ならその数字を、そうでなければnullを返す | ||
int? _daysBefore(DateTime now) { | ||
final differece = now.difference(this).inDays; | ||
if (differece >= 2 && differece <= 7) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 と 7 がいわゆるマジックナンバーになっているので、次のようなリファクタを見当してください:
extension DateTimeExtension on DateTime {
/// 適切な変数名にする。適切な doc comment を書く。
static const _a = 2;
/// 適切な変数名にする。適切な doc comment を書く。
static const _b = 7;
// ... 省略
/// 現在と比較して [_a] 日 〜 [_b] 日前ならその数字を、そうでなければnullを返す
int? _daysBefore(DateTime now) {
final differece = now.difference(this).inDays;
if (differece >= _a && differece <= _b) {
return differece;
}
return null;
}
}
あと、differece
がタイポです。エディタのタイポの警告は出ていませんか?(必要ならタイポチェックの設定をすることを強く勧めます)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらもありがとうございます!
マジックナンバーにならないよう以後留意します!
定数名など問題ないかご確認お願いします🙇♂️
タイポの件、失礼しました(前回も同じタイポの指摘を受けたので反省してます)
先ほどタイポチェックの拡張機能を入れました・・・!
特に Firestore を使っているとその |
確かに、Firestoreはnull 許容の |
「 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正ありがとうございました!!Approved!
Issue
close #38
説明
UI
変更なし
その他
DateTime?は許容しない設定とした。
DateTime?になるケースとして、特定のデータの日付情報がオプショナルになっている場合などが想定されるが、そういったデータを使ってこのextensionを呼ぶことはないのでは?と考えた。
と考えたのですが、どうでしょうか・・・?
チェックリスト