Language/Dart
[Exercism] Gigasecond #Duration #Named parameter
paran21
2022. 9. 8. 16:54
https://exercism.org/tracks/dart/exercises/gigasecond/iterations?idx=2
문제 자체는 정말 단순하게 gigasecond를 더하면 되는데, Duration을 처음 써볼 수 있었다.
Duration은 말 그대로 기간을 나타내는 클래스인데, DateTime 클래스와 같이 많이 쓰는 것 같다.
Dart에서 마음에 드는 것 중 하나는 named parameter인데, 이렇게 arguments가 어떤 값인지 바로 알 수 있어서 좋다.
(자바는 named parameter가 없고, 코틀린은 있다!)
메서드명에서 바로 매개변수를 유추할 수 있는 경우(ex. findBy~~)에는 굳이 named parameter를 사용하지 않아도 된다고 생각하지만, 여러 매개변수가 들어가는 경우, 메서드명만으로는 알 수 없는 경우에는 named parameter를 더 선호하는 편이다.
DateTime add(final DateTime birthDate) {
final int gigaseconds = 1000000000;
return birthDate.add(Duration(seconds: gigaseconds));
}