Cześć, refaktoryzuję teraz projekt i mam pełno if'ów w kodzie. Zacząłem refaktoryzację i chciałbym zapytać czy w dobrym kierunku idę. Również zapytać czy macie lepsze pomysły, czy są jakieś źródła na ten temat, z których mógłbym się uczyć?
Zacząłem z przykładem 1 i na przykład 2 jest kod po refaktoryzacji.
Przykład 1 (przed):
if (userSession.isInvalid()) {
throw new Error(`...`);
}
if (userSession.isEmpty()) {
throw new Error('...');
}
if (userSession.isExpired()) {
throw new Error('...');
}
...
export default class UserSessionResult {
userSessions: Array<UserSession>;
isInvalid(): boolean {
return this.userSessions.length > 1;
}
isEmpty(): boolean {
return this.userSessions.length === 0;
}
...
}
Przykład 2 (po):
userSession.isInvalid()
.or()
.isEmpty()
.or()
.isExpired()
...
export default class UserSessionResult {
userSessions: Array<UserSession>;
...
isInvalid(): UserSessionResult {
if (this.userSessions.length > 1) {
throw new Error('Invalid user session');
}
return this;
}
isEmpty(): UserSessionResult {
if (this.userSessions.length === 0) {
throw new Error('Invalid user session');
}
return this;
}
or(): UserSessionResult {
return this;
}
}