patternjavaMinor
Determine which level of the text that it is currently parsing
Viewed 0 times
theleveltextthatparsingdeterminewhichcurrently
Problem
So I have this code in my program, and there are multiple variations of it. Just looking at it, I feel like there must be a way to make it more efficient, but I can't think of anything. Just looking at it, does anyone have any ideas?
Essentially, what the code does is it determines which level of the text that it is currently parsing. If it it is in level 0, it adds that body of text to the
If it it in level 1, it adds that body of text to
etc.
Notes:
Code
```
// Adds the passed in heading text to the appropriate section
public static void addSectionText(String text)
{
if (currentLevel == 0)
{
aMainSection.get(levelCount[0] - 1)
.addSection(text);
}
else if (currentLevel == 1)
{
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.addSection(text);
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.getSection());
}
else if (currentLevel == 2)
{
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.addSection(text);
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.getSection());
a
Essentially, what the code does is it determines which level of the text that it is currently parsing. If it it is in level 0, it adds that body of text to the
aMainSection ArrayList.If it it in level 1, it adds that body of text to
aChildSection of aMainSection (An ArrayList within the aMainSection ArrayList). Then it sets the aMainSection text to 'parent text' within a new ArrayList.etc.
Notes:
aMainSection&aChildSectionare arrayLists of typeSection(A custom class)
levelCountis just an array of typeint, size 10 (abstract number)
Code
```
// Adds the passed in heading text to the appropriate section
public static void addSectionText(String text)
{
if (currentLevel == 0)
{
aMainSection.get(levelCount[0] - 1)
.addSection(text);
}
else if (currentLevel == 1)
{
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.addSection(text);
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.getSection());
}
else if (currentLevel == 2)
{
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.addSection(text);
aMainSection.get(levelCount[0] - 1)
.aChildSection.get(levelCount[1] - 1)
.aChildSection.get(levelCount[2] - 1)
.addParentSection(
aMainSection.get(levelCount[0] - 1)
.getSection());
a
Solution
That much repetition does have a smell to it. Can you simplify it by saving and reusing intermediate values similar to what I've shown below?
else if (currentLevel == 3)
{
Section level0Section = aMainSection.get(levelCount[0] - 1);
Section level1Section = level0Section.aChildSection.get(levelCount[1] - 1);
Section level2Section = level1Section.aChildSection.get(levelCount[2] - 1);
Section level3Section = level2Section.aChildSection.get(levelCount[3] - 1);
level3Section.addSection(text);
level3Section.addParentSection(level0Section.getSection());
level3Section.addParentSection(level1Section.getSection());
level3Section.addParentSection(level2Section.getSection());
}Code Snippets
else if (currentLevel == 3)
{
Section level0Section = aMainSection.get(levelCount[0] - 1);
Section level1Section = level0Section.aChildSection.get(levelCount[1] - 1);
Section level2Section = level1Section.aChildSection.get(levelCount[2] - 1);
Section level3Section = level2Section.aChildSection.get(levelCount[3] - 1);
level3Section.addSection(text);
level3Section.addParentSection(level0Section.getSection());
level3Section.addParentSection(level1Section.getSection());
level3Section.addParentSection(level2Section.getSection());
}Context
StackExchange Code Review Q#3581, answer score: 4
Revisions (0)
No revisions yet.