Given the description of the problem, unless I have not understood something, there is nothing better that can be done.
If you have some detail not explained, eventually you can find an optimization but there is no miracle. Only if there is a criterion that makes it easier to take some shortcut.
One thing you can do is have a single list that has all the data, but it’s hardly a good idea, it makes it easier on the one hand, it makes it harder on the other.
Even with this solution, you may have a for
. So what? Why is this a better code? Just because it has fewer ties? That doesn’t mean that the code is better written. Anyway I’d probably have to have three if
to identify the cluster break, then perhaps it would be even worse. And another, changes the data structure because of the algorithm. Then it would be a beautiful bad practice in most situations.
And understand that good or bad practice refers to common cases, to a large number of cases. They cannot be followed in all cases.
What you can do is separate it into methods for each interaction, so you would only have one for
in each method. But there would still be 4 for
. Does this make the code better? In some chaos, yes. But not necessarily, it depends on the goal. Separating too much can bring as many problems as piling too much. It would be something like this (roughly speaking):
void getGeneros(ArrayList biblioteca) {
for (Genero genero : biblioteca.getLista()) {
getArtistas(genero);
}
}
void getArtistas(ArrayList genero) {
for (Artista artistas : genero.getLista()) {
getAlbuns(artistas);
}
}
void getAlbuns(ArrayList artista) {
for (Album album : artista.getLista()) {
getMusicas(album);
}
}
void getMusicas(ArrayList album) {
for (Musica musica : album.getLista()) {
GravaMusica(musica);
}
}
I put in the Github for future reference.
It’s just an example. I have doubts if it’s better, I think it got worse, got repetitive depending on each GetLista()
makes. Anyway I think the names of variables and types are better defined there. Since you like good practices, try to name everything in your program, this helps a lot. Could be a better example but I don’t know the whole context.
If the problem is performance it may be possible to break the operation and put the processors to work in parallel, but then the problem would be different from what was described. And it might not even be worth the effort.
It doesn’t seem like there’s much to improve on. Unless you give more details of what you’re doing it looks like this way is ok.
– Maniero
I just asked to know if it has an optimized shape, this way works but using 4 "for" goes against good programming practices.
– daniel12345smith
Where did you see this? You need to review your concepts or stop reading things written by those who do not understand the subject. If you need to scan 4 nested lists and grab each of the elements from each of them, there is no different way. You can write otherwise but in the background the result will be the same. There is a case that can even get worse. There may even be a way to improve me but it would depend on circumstances that don’t seem to be your case.
– Maniero
Maybe if you post other parts, explain better, give p/ think of something better. An important detail is that if you will record in bank, the
for
is the least.– Maniero
You from a given genre want to know all your songs?
– Goldbones
@daniel12345smith Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Need something to be improved?
– Maniero