[Moscow.pm] refactoring
Михаил Шогин
mshogin на gmail.com
Чт Окт 15 03:51:54 PDT 2015
Это конечно не Perl, но все тоже самое можно написать и на Perl-e
class NonePerson(BasePerson):
def get_id():
return None
class Person(BasePerson):
def get_father():
return self.father or NonePerson()
def get_mother():
return self.mother or NonePerson()
def get_date_of_birth():
return self.birthday # datetime object
def get_date_of_birth_as_a_very_custom_string():
date = self.birthday.day or ""
if self.birthday.month:
date . = "/" + self.birthday.month
if self.birthday.year:
date . = "/" + self.birthday.year
return date
class PersonRowBuilder():
def build(self, person):
row = ()
row.append(person.get_father().get_id())
row.append(person.get_mather().get_id())
row.append(person.get_email())
row.append(person.get_homepage())
return row
##################################
def info():
person = Person()
birthday = person.get_date_of_birth_as_a_very_custom_string()
def method_related_to_db_usage():
builder = PersonRowBuilder()
row = builder.build(Person())
15 октября 2015 г., 11:58 пользователь Vladimir Timofeev <vovkasm на gmail.com>
написал:
> Гм...
>
> push @person_row, $person->get_father_id, $person->get_mother_id,
> $person->get_email, $person->get_homepage;
>
> my $date = $person->get_display_date_of_birth;
>
> package Person;
>
> sub get_father_id {
> my $self = shift;
> if (my $self->get_father) {
> return $self->get_father->get_id;
> }
> return;
> }
>
> ... и т.д.
>
> Это раз.
>
> Можно пойти дальше и объединить построение @person_row в один метод:
>
> push @person_row, $person->get_person_fields
>
> Хотя использование массива здесь выглядит очень подозрительно, но это
> уже к вопросу не относится.
>
> С датами, помимо выноса в отдельный метод кода форматирования, можно
> потом вынести код форматирования в класс даты. Потом можно переписать
> код форматирования даты, так, чтоб работал корректно во всех случаях.
> А то сейчас можно сделать "3/" к примеру (задан только месяц).
>
>
>
>
> 2015-10-15 5:43 GMT+03:00 Nikolay Mishin <mi на ya.ru>:
> > Hi Moscow.PM!
> >
> > Существует ли какой-либо сбособ сделать более читаемым такой код?:
> >
> > push @person_row, (defined $person->get_father()) ?
> $person->get_father()->get_id() : undef;
> > push @person_row, (defined $person->get_mother()) ?
> $person->get_mother()->get_id() : undef;
> > push @person_row, ($person->get_email(), $person->get_homepage());
> >
> > my $date = "";
> > if(defined $person->get_date_of_birth()) {
> > my $date_of_birth = $person->get_date_of_birth();
> > $date .= defined $date_of_birth->day ? $date_of_birth->day."/" :
> "";
> > $date .= defined $date_of_birth->month ? $date_of_birth->month."/"
> : "";
> > $date .= defined $date_of_birth->year ? $date_of_birth->year : "";
> > }
> >
> > источник
> https://github.com/mishin/Ftree-cpan/blob/master/lib/Ftree/Exporters/ExcelExporter.pm#L53
> >
> > --
> > С уважением
> > Николай Мишин
> >
> > --
> > Moscow.pm mailing list
> > moscow-pm на pm.org | http://moscow.pm.org
>
>
>
> --
> Vladimir Timofeev <vovkasm на gmail.com>
> --
> Moscow.pm mailing list
> moscow-pm на pm.org | http://moscow.pm.org
>
--
С уважением
Михаил Шогин.
----------- следущая часть -----------
Вложение в формате HTML было извлечено…
URL: <http://mail.pm.org/pipermail/moscow-pm/attachments/20151015/aac86709/attachment-0001.html>
Подробная информация о списке рассылки Moscow-pm