简体   繁体   中英

Alternate to nested FOR Loop

I was looking an old project and i saw one code there that I am explaning here:

The code was loading some records(domain objects) from database then iterate over it then for each iteration they are getting on object through which they are loading again a list. They are doing this activity by putting nested for-loops in the code.

ParentList = getFromDatabse;
for(....){ // over ParentList
     SubChild1List = fromParentObejct;
     for(... ) { // over SubChild1List 
       subChild2List = fromSubChild1Objectl
        .. so on 

The code is having 7 nested loops.

My question is to suggest me some good design-pattern to avoid such a messy code.

Use the visitor pattern and recursion. It's hard to say more since your code does not tell much.

You can try to optimize the way you are getting data from database. Maybe then you won't need do processing by nesting for loops again n again (Pls provide more details).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM