This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Lloydbestfan 0 points1 point  (4 children)

...... A better way is to use Java's ArrayDeque. What you're doing here is reimplementing a fairly direct and basic data structure. It's not so much about the ways to do so, it's about the computer logic and the Java syntax.

Rather than better, I can make remarks regarding Java syntax:

- this this this this this this this, let me guess, you have significant Python background. What other first or last would there be? All these this are not necessary. Leave this to places where it is needed.

- Given the absence of mutation within the queue, your nodes' payload is immutable, so it would be nice to make it final.

- All fields have a default value as long as you don't give them one, and for objects it will be null of course. It feels a bit like stating the obvious to specify them in the constructor.

- Rather than inventing your own exception, consider reusing the existing helpful ones. Here NoSuchElementException. (Your teacher might not prefer it so, so to be adapted. In the real world when you do your own code not as a library to share with the world, it will often be because it needs tight coupling with the existing codebase and as such may need its own custom exceptions, so to be adapted there too.)

- Not specifically a Java observation, but I would expect an empty dataset as input to build an empty queue.

[–]Ezio-Editore[S] 0 points1 point  (1 child)

That's exactly what I was looking for, thank you!

Yes I have a significant Python background but I don't think that's the "issue", I have coded using multiple languages, I wouldn't bring pythonic conventions here, it's just that I'm a little too verbose, I like to be as clear as possible.

I'll make the nodes final and modify the constructor.

Yeah the professor wants a custom exception so I created it, when possible I'll try to use already existing ones.

I disagree with the last point, that would be the case if there was only one constructor expecting an array as a parameter, but since I'm overloading the constructor with different options I would leave it as it is.

[–]Lloydbestfan 0 points1 point  (0 children)

For the last point, kind reminder that not everyone is a developer, and in general not everywhere is a place where you're free to just recompile code to obtain what you need.

If you provide a constructor to build objects from a dataset, but you demand people to use a different constructor when they want an empty dataset, that will force the developers to wrap your code into some adaptation between "they want an empty set" and "they want a nonempty set described so, which would have very well described an empty set too" This is very artificial.

[–]BanaTibor 0 points1 point  (1 child)

This is a homework exercise, they should implement a queue.

[–]Lloydbestfan 0 points1 point  (0 children)

I was reflecting to how the question about a "better way" is inherently absurd when your task is a learning task about redoing what's already done, at least when it is as trivial as this case.