C
C#2y ago
kaziux

❔ How optimize this code without breaking it and taking DRY into account?

Hello. I have some issue. This code runs fine but some places repeats to combat null ref exception. This class issues invoice and it's payed by legal person or individual so that's why payedIndividual or payedLegalPerson variable is null and I do WET. How to optimize code with DRY but without breaking it? https://github.com/Nemes1sX/InvoiceApp/blob/master/InvoiceApp/Services/InvoiceService.cs code link, because it's too much post the whole code snippet here
GitHub
InvoiceApp/InvoiceService.cs at master · Nemes1sX/InvoiceApp
Contribute to Nemes1sX/InvoiceApp development by creating an account on GitHub.
38 Replies
ero
ero2y ago
isn't there so much more that you need to tackle first? not using nullable reference types not using WhereAsync using AddAsync (ouch!) not using is (not) null (not using file-scoped namespaces, but that's presonal preference)
Jimmacle
Jimmacle2y ago
i see some rules that could be described better by changing the InvoiceRequest model it looks like only one entity can "pay" so that shouldn't be mulitiple bools, it should be an enum
ero
ero2y ago
typos, lines being longer than 140 columns, non-descriptive variable names...
Jimmacle
Jimmacle2y ago
generally extracting business rules from the service and putting them somewhere else
basically, i am little cat
You can do that?
Jimmacle
Jimmacle2y ago
yes
ero
ero2y ago
you should do that
basically, i am little cat
No wait I meant like
ero
ero2y ago
no
basically, i am little cat
If (whatever is (not) null)
Jimmacle
Jimmacle2y ago
is null and is not null are valid patterns
basically, i am little cat
With non curly braces Keep braces thing Don't get rid of them
ero
ero2y ago
what i wrote isn't valid, no it was a substitute for is null/is not null
ero
ero2y ago
because i was lazy
kaziux
kaziuxOP2y ago
Legal Person and Individual are diffrent entity types
Jimmacle
Jimmacle2y ago
idk why i thought they were bools, i'm still waking up
ero
ero2y ago
(in law, there are "legal persons" and "natural persons")
basically, i am little cat
Legal persons?
ero
ero2y ago
"legal persons" actually being registered groups of people
ero
ero2y ago
so for example a corporation
basically, i am little cat
Why it isn't legal people then
Jimmacle
Jimmacle2y ago
that's not fancy law school sounding enough
Jimmacle
Jimmacle2y ago
probably just for consistency tbh, english gets goofy with pluralization
ero
ero2y ago
i mean it doesn't have to be groups of people. you can open your own little shop, get it legally registered, and anything you do related to your business is you acting as a legal person but when you just get groceries for example, you're making a contract as a natural person
kaziux
kaziuxOP2y ago
I try with
is null
is null
but still code isn't optimized.
basically, i am little cat
Is it tho?
kaziux
kaziuxOP2y ago
Still I have repeating code.
Jimmacle
Jimmacle2y ago
without proper refactoring you'll have repeated code, swapping one expression for another won't do it
Matt
Matt2y ago
if you don't mind - why use is not null instead of != null? are they different, or is it just a best practice?
Jimmacle
Jimmacle2y ago
equality operators can be overridden afaik the pattern style will always actually check if it's a null reference or not
ero
ero2y ago
What they said
Matt
Matt2y ago
I see 🤔
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server