// This should be a method on Article, really
// Either use int/long for the smallest unit of your currency (cent, etc.)
// or use an actual currency type. Never use floats for money.
public String formatPrice(int price) {
return BigDecimal.valueOf(price, 2).toString() + "€";
}
// It might be worth creating a type to represent an Article in
// a cart and its quantity so you can maintain the invariant
// (assumed here) that here must be at least one of each thing
// in the cart. This can then just be a method on that type.
public StringBuilder description(Article a, int quantity) {
var multiplier =
quantity > 1
? new StringBuilder(Integer.toString(quantity)).append(" x ")
: new StringBuilder();
return multiplier
.append(a.getName())
.append("\nPrice: ")
.append(formatPrice(a.getUnitPrice() * quantity));
}
// If we wrote the type suggested above, we could just carry around a List
// of CartArticles and this stream solution would read a bit nicer
List<String> cartDescriptions(Cart cart) {
return cart.outerMap().entrySet().stream().map(e ->
description(e.getKey(), e.getValue()).toString()
).collect(Collectors.toList());
// Could be
// return cart
// .getItems()
// .stream()
// .map(item -> item.description)
// .collect(Collectors.toList())
}
public void UpdateArticlesInCart() {
ItemList.setItems(FXCollections.observableList(cartDescriptions(cart)))
}
Sketch of the final code:
class CartArticle {
private Article article; // Or just extend Article, but I can't think of a context where that hierarchy would be useful
private int quantity;
...
private StringBuilder description() {
var preface =
quantity > 1
? new StringBuilder(Integer.toString(quantity)).append(" x ")
: new StringBuilder();
return preface
.append(article.getName())
.append("\nPrice: ")
.append(article.formatPrice(quantity));
}
public String description = description().toString();
...
}
class Article {
private int unitPrice;
private String name;
...
public String formatPrice(int multiplier) {
return BigDecimal.valueOf(getUnitPrice() * multiplier, 2) + "€";
}
public String formatPrice() { return formatPrice(1); }
...
}
class Cart {
private List<CartArticle> items;
public List<CartArticle> getItems() {
return items;
}
...
}
class BusinessLogic {
...
public List<String> cartDescriptions(Cart cart) {
return cart
.getItems()
.stream()
.map(e -> e.description)
.collect(Collectors.toList());
}
public void UpdateArticlesInCart() {
ItemList.setItems(FXCollections.observableList(cartDescriptions(cart)))
}
...
}
Eh, not completely horrible compared to some legacy BS I've seen. Only needs some line breaks in the longer lines and it's a lot better in terms of readability.
Readability? Readability is not the issue. They are iterating over the key-value pairs of a hashmap. How do they do this?
They build a list of the keys, and a list of the values. Then they iterate over the list of keys. Reasonable enough so far.
Then, to get the value that corresponds to a key, they iterate over the list of keys again to find the current key's index, and get the value at that index. They do this every time they want to use the value, so the key list is scanned for no reason 2-3 extra times for each key. What could have been a simple iteration over the map now takes exponential quadratic time.
Yeah, I looked at this and went "too much to read for a lame complaint". Then I did read it, and was like, "oh, what's this?" And then "Oh, no, again?" "Oh, shit, is he getting paid by the keystroke or the CPU cycles?"
Honestly this reads like one of those hero opportunities. "Great job on that new feature. Any chance we can squeeze a tad more speed out of it?"
Oh, yeah, boss. I think I can get a 10% speedup if I spend the next few weeks on just this task.
Release Notes: Handling of data has been drastically optimized and algorithms have been improved to fully use the available computer resources. We estimate this to nearly 3 seconds quicker calculations and generating the results!
Aren't they also making the assumption that the indices in both lists correspond to each other? Given that both the keyset and valueset are unordered, it'd surprise me if that works
I bet it'll be in that excellent category of "things that work most of the time until they don't", like it suddenly stops working with a certain size of map.
The long line could easily have been fixed by an auto-formatter so that's just sloppy, but what I find truly horrible here is: "tf is a carts outer map!?!".
i was already annoyed that they were using string concatenation instead of String.format and then I saw that they were using String.format, just only for part of it
Both branches of the if branch start with the same string. That can be moved out so it’s not repeated. The call to k.get(whatever) is done twice instead of assigning the result to a variable.
And it’s hard to say without knowing the bigger usage context, but this presentation logic looks quite brittle in terms of formatting.
Reduce iterations, wrap long lines for readability. Iterate over content.keys rather than defining two new list vars that really aren’t necessary for what the code does. To name a few
Using an hash map for small collection is actually less efficient than using an array. The actual turnover size of the collection is dependant on the value size and hash map implemantation so benchmarks are requiered. If the list can fit in L1 cach it will be faster even if you need x20 (sometimes more) the operations to get by a key
LOL.. it's not. That's my point. The guy that wrote this is just learning JAVA and used structures similar to the STL, as one would if one was normally a C++ programmer.
That’s because your brain is undeveloped and you shouldn’t be on Reddit. Your parents should be spanking you but I have a feeling they don’t pay much attention.
165
u/BambusleitungTV Jun 25 '22
fun fact: it is the OPs code
source: his friend (he couldnt remember he wrote it lol)
74
u/[deleted] Jun 25 '22
Thanks i hate it
39
u/TinyBreadBigMouth Jun 25 '22 edited Jun 25 '22
52
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 25 '22 edited Jun 25 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
46
u/[deleted] Jun 25 '22
I've seen worse :)
12
u/itemluminouswadison Jun 25 '22
That first fuckin var name content...
And "k" oh god
Scroll up... Whats in the docblock? WHATS IN THE DOCBLOCK????
35
u/Furry_69 Jun 25 '22
Eh, not completely horrible compared to some legacy BS I've seen. Only needs some line breaks in the longer lines and it's a lot better in terms of readability.
7
u/youngbull Jun 25 '22
The long line could easily have been fixed by an auto-formatter so that's just sloppy, but what I find truly horrible here is: "tf is a carts outer map!?!".
4
u/ledalmatiennoir Jun 25 '22
i was already annoyed that they were using string concatenation instead of String.format and then I saw that they were using String.format, just only for part of it
4
u/ErdenArt Jun 25 '22
I have no idea how to improve this code
2
u/I_Like_Existing Jun 25 '22 edited Jun 26 '22
O(n5)? Considering the worst case lol
EDIT: i'm wrong!!
2
u/IceQub3 Jun 25 '22
Using an hash map for small collection is actually less efficient than using an array. The actual turnover size of the collection is dependant on the value size and hash map implemantation so benchmarks are requiered. If the list can fit in L1 cach it will be faster even if you need x20 (sometimes more) the operations to get by a key
2
u/analogic-microwave @SpringDoSomeBlackMagicStuffPls Jul 02 '22
Horizontal programming. Best design pattern ever.
1
u/[deleted] Jun 26 '22 edited Jun 26 '22
recursion and string formatting moment
1
u/AmyMialee Jun 26 '22
God this colour scheme for java throws me off so hard
1
u/scooptyy Jun 26 '22
Now this is true garbage.
1
u/NoFixedName Jun 26 '22
This is C# right? I've never seen that type of for loop before. Is it just different syntax for a foreach loop?
1
u/AllenKll Jun 26 '22
Tell me you're a C++ programmer without telling me you're a C++ programmer...
1
u/[deleted] Jun 30 '22
I don't get it?
1
u/bravopapa99 Oct 30 '22
Is he in rehab now ? Or was that part of the rehab process??