INDAPlus21 / INDAPlus21/ogronman-sorting
Pass
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
**Very well done Oskar!**
Bläh! Java.
Never heard of goblin sort, but fair enough.
I think a readability discussion is in somewhat order.
#### If statements vs. switch expressions
_Your code_:
```java
public boolean dispatchKeyEvent(KeyEvent e) {
if (/*...*/) {
if (e.getKeyCode() == 82) {
//...
addColorListPanel();
multiPanel.repaint();
} else if (e.getKeyCode() == 73) {
//...
addColorListPanel();
multiPanel.repaint();
} else if (e.getKeyCode() == 83) {
//...
addColorListPanel();
multiPanel.repaint();
} else if (e.getKeyCode() == 77) {
//...
addColorListPanel();
multiPanel.repaint();
} else if (e.getKeyCode() == 65) {
//...
addColorListPanel();
multiPanel.repaint();
} else if (e.getKeyCode() == 78) {
//...
addColorListPanel();
multiPanel.repaint();
//...
} else if (e.getKeyCode() == 71) {
//...
addColorListPanel();
multiPanel.repaint();
//...
}
}
addColorListPanel();
multiPanel.repaint();
return false;
}
```
_With switch expression_:
```java
public boolean dispatchKeyEvent(KeyEvent e) {
if (/*...*/) {
switch (e.getKeyChar()) {
case 'R' -> //... restart
case 'I' -> //... insertion sort
case 'S' -> //... selection sort
case 'M' -> //... merge sort
case 'A' -> //... all colours
case 'N' -> {
//... side-by-side
}
case 'G' -> {
//... goblin sort
}
default -> null;
}
}
addColorListPanel();
multiPanel.repaint();
return false;
}
```
#### Douplication vs. anything else
_Your code_:
```java
import java.util.function.Function;
//...
public OgronmanSorting() throws HeadlessException {
//...
red = //...
red.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showColor = 0;
addColorListPanel();
multiPanel.repaint();
}
});
green = //...
green.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showColor = 1;
addColorListPanel();
multiPanel.repaint();
}
});
blue = //...
blue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showColor = 2;
addColorListPanel();
multiPanel.repaint();
}
});
all = //...
all.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showColor = 3;
addColorListPanel();
multiPanel.repaint();
}
});
//...
}
```
_With nested function_:
```java
import java.util.function.Function;
//...
public OgronmanSorting() throws HeadlessException {
//...
Function getActionListener = (colour) -> new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showColor = colour;
addColorListPanel();
multiPanel.repaint();
}
};
red = //...
red.addActionListener(getActionListener.apply(0));
green = //...
green.addActionListener(getActionListener.apply(1));
blue = //...
blue.addActionListener(getActionListener.apply(2));
all = //...
all.addActionListener(getActionListener.apply(3));
//...
}
```
_With stream_:
```java
import java.util.stream.IntStream;
//...
public OgronmanSorting() throws HeadlessException {
//...
JPanel buttonPanel = new JPanel();
String[] buttonTitles = {/*button titles in colour order*/};
IntStream
.range(0, buttonTitles.length)
.map((colourIndex) -> {
JButton button = new JButton(buttonTitles[colourIndex]);
button.addActionListener(ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showColor = colourIndex;
addColorListPanel();
multiPanel.repaint();
}
});
return button;
}
.forEach((button) -> buttonPanel.add(button));
//...
}
```
Keep up the good work!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The payload mentions OgronmanSorting(), dispatchKeyEvent(KeyEvent e), and the Swing color-button setup, but names no file or test. Start by locating those entry points and clarify which readability changes are actually wanted; completion criteria are not defined in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- desktop
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 15/100