❔ 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
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)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
typos, lines being longer than 140 columns, non-descriptive variable names...
WHAT
generally extracting business rules from the service and putting them somewhere else
You can do that?
yes
you should do that
No wait
I meant like
no
If (whatever is (not) null)
is null
and is not null
are valid patternsWith non curly braces
Keep braces thing
Don't get rid of them
what i wrote isn't valid, no
it was a substitute for
is null
/is not null
Good
because i was lazy
Legal Person and Individual are diffrent entity types
idk why i thought they were bools, i'm still waking up
(in law, there are "legal persons" and "natural persons")
Legal persons?
"legal persons" actually being registered groups of people
Huj
so for example a corporation
Why it isn't legal people then
that's not fancy law school sounding enough
probably just for consistency tbh, english gets goofy with pluralization
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
I try with but still code isn't optimized.
Is it tho?
Still I have repeating code.
without proper refactoring you'll have repeated code, swapping one expression for another won't do it
if you don't mind - why use
is not null
instead of != null
?
are they different, or is it just a best practice?equality operators can be overridden
afaik the pattern style will always actually check if it's a null reference or not
What they said
I see 🤔
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.