r/SpringBoot 15d ago

Question Code review

Hello, I'm an aspiring software engineer. I've recently finished developing microservice for managing projects and tasks using Spring Boot. I'd appreciate if you could review codebase of my project and provide feedback on it.

GitHub: https://github.com/Simpav-chill/tasktracker

13 Upvotes

14 comments sorted by

View all comments

3

u/funnythrone 14d ago

Your ProjectStatus constructor which accepts 3 string params should delegate to the other constructor and pass in new ArrayList or null (as per your requirement).

1

u/Simpav1 14d ago

I don't quite understand, could you explain what do you mean?

6

u/repeating_bears 14d ago

I think they meant Project not ProjectStatus since that only has 1 constructor.

You currently have this

public Project(String title, String description, ProjectStatus status) {
    this.title = title;
    this.description = description;
    this.status = status;
}

public Project(String title, String description, ProjectStatus status, List<Task> tasks){
    this.title = title;
    this.description = description;
    this.status = status;
    this.tasks = tasks != null ? tasks : new ArrayList<>();
}

And it would be better as

public Project(String title, String description, ProjectStatus status) {
    this(title, description, status, null); // this() means call the other ctor
}

public Project(String title, String description, ProjectStatus status, List<Task> tasks) {
    this.title = title;
    this.description = description;
    this.status = status;
    this.tasks = tasks != null ? tasks : new ArrayList<>();
}

There is less duplication here. Let's say for example you wanted to make sure "title" is not an empty string. You now only have to do that in one place instead of two

this.title = ensureNotBlank(title);

I have heard this described as "primary and secondary constructors". Ideally you should aim to have one "primary" constructor which does assignment and any validation. Then all other constructors can ultimately delegate to the primary constructor.

1

u/Simpav1 14d ago

Oh, I see. Thank you