To be fair. He is the only developer at the company (including me now). He mainly works on SAP stuff and backend. They needed the app fast. I was hired to update their old react app and add some features on the frontend as a junior. The app is mainly for internal use. He usually has to work on multiple things at the same time aswell. Maybe phrased the title badly it's not dissapointing i just find it kinda funny.
(Assuming your boss is not a developer by trade), this is actually kind of cute. It looks like they're trying to incorporate different things they have learned throughout their js (I assume?) self-taught journey and are making a genuine effort.
I've been in IT since the 1990s and I can 100% positively say that the code running the world is the equivalent of chewing gum, baling wire, and duct tape.
And if your boss is a non-native English speaker, I’d argue this is even less bad - as the only thing that really struck me as likely to rot was the name of the method. My $0.02.
Can somone explain what parts are wrong here? the nullcheck is off and validatevalues should probably do more than just nullcheck but I have the feeling there is more here than I'm spotting.
Instead of checking for truthfulness in the if statement you can just return !!(variable). This will directly convert it to true out false. There is actually no need for the first method. And the variable names should be descriptive.
Also, the first method is generic about validation, but then the call-site assumes any validation error is due to a specific field; so if the _hasValidateValues() method ever adds any more validation, and that additional validation fails, an inaccurate error message will be shown.
Better to do something like
_validate = (): string | null => {
if (!this.state.plateNumber) {
return this.props.t('DeliveryNote.LicensePlate.FormInvalid');
}
// then other validations can be added as early-returns here
return null;
};
_onSubmit = () => {
const errMsg = this._validate();
if (errMsg !== null) {
toastError(errMsg);
return;
}
}
Not sure why this is downvoted, maybe because of snark?
_hasValidateValues can simply be _isValid or _validate.
This can sound like a minor point, but in larger code bases its critical to name as clearly and efficiently as possible so that you don’t spend time figuring out what each function does. It changes your workflow from a guessing game to scanning the code base with confidence
Looks like it's for internationalization, likely "translate" or such. It's pretty common to name functions like that really short, commonly just _, to reduce noise, as they're used on every user-facing string.
This post gives me the vibe of a junior developer who nitpicks very specific things as being "bad" when in reality it's fine, and there a bigger issues to be worrying about.
The main issue I see is that it's a greenfield project in 2023 using React class components and not hooks...
My team did this in their old class components too. My guess is they thought of it as a more elegant way to declare a function than having to say this.myFunction = this.myFunction.bind(this)
It's more of a paradigm/coding style than anything. Everywhere you use a lambda expression you can use an explicit function and vice-versa. One could use a lambda because an explicit definition isn't needed as it's called only once in the code but an explicit one would clarify what it's doing and clearly separate tasks.
This looks like the _hasValidateValues function was supposed to contain more field checks, but they were never added in and the code was just abandoned as is.
This is JS, so _hasValidateValues is not a NO-OP: it's a boolean cast!
Is it what the author really meant? Probably not. Is it error prone in this context? Probably (I'd prefer reading "undefined" instead of "true" if the underlying value has not been initialized, it's easier to spot the error).
It's a relatively old react app for internal use. Knowing him he probably had more logic in the function but deleted it overtime and just forgot about it. I just found it kinda funny.
If a class component works fine and devs don't have bandwidth to refactor it into an FP then I don't see anything wrong with it. There's a reason why react still supports class components and even React.createClass
There's a joke at state level that goes along the lines of "soon as these snowflakes get together and decide on a single library for an implementation all of javascript will go under." Lol.
One question that always gets them is in javascript what is this bit of code doing?
= () =>
Very few webdevs that I've talked to, aged mid 20s and younger, can answer that. and they're working for pretty high ups in pretty lucrative positions. that's why i will only ever contract.
(Notice how "return" is implied - you can include it if you like though)
(I included the normal if statements to demonstrate ternary expressions too).
You can have multiple lines in a lambda, if you include { } to create a block scope.
If you're curious, just open up chrome now on a computer (I'm on mobile) and open the developer tools, go to console and tinker around there to test syntax quickly.
This similar thing exists in a bunch of languages nowadays, but can be confusing at first to see.
If you're still confused, please ask. I love sharing knowledge and learning from others :)
When I was on a contract the in-house IT staff knew they needed specific libraries and they knew the libraries were "best in class" for the function they were downloaded for, but they couldn't verbally tell me what any of it was doing. And I just nodded and smiled. Cause I knew I'd be back...no fault of their own.
151
u/1337GameDev Mar 08 '23
Honestly --
This isn't horrible. Just not optimal.
I've absolutely done the validate method similarly in the past -- as it's easy to add extra checks and make it very clear what's going on.
He absolutely just needs comments.
If that's 100% the ONLY validation check, ever, then he can simplify it, as it has unnecessary lines -- but it's not horrible.
At the end of the day it has these:
4
u/anxiety_enjoyer Mar 08 '23
To be fair. He is the only developer at the company (including me now). He mainly works on SAP stuff and backend. They needed the app fast. I was hired to update their old react app and add some features on the frontend as a junior. The app is mainly for internal use. He usually has to work on multiple things at the same time aswell. Maybe phrased the title badly it's not dissapointing i just find it kinda funny.
436
u/LivingInAnIdea Mar 08 '23
(Assuming your boss is not a developer by trade), this is actually kind of cute. It looks like they're trying to incorporate different things they have learned throughout their js (I assume?) self-taught journey and are making a genuine effort.
85
u/geehops Mar 08 '23
If this is bad enough for bad code, I assure you that bad code is literally running the world… 😀
84
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 08 '23
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!
55
u/josephblade Mar 08 '23
Can somone explain what parts are wrong here? the nullcheck is off and validatevalues should probably do more than just nullcheck but I have the feeling there is more here than I'm spotting.
15
u/_ncko Mar 08 '23
This isn’t amazing, but it isn’t terrible. By this point in my career, I wouldn’t really consider this bad to be honest. My standards are so low now.
7
u/No-Quail5810 Mar 08 '23
Probably looking to the future where that function will "validate" other things...
4
u/TheZipCreator Mar 08 '23
this is like, the least bad thing in this code, but type-annotated lambda expressions just feels weird
7
u/Excitedastroid Mar 08 '23
this works, just a lot of unnecessary steps were taken
2
u/[deleted] Mar 08 '23
Boss doesn’t mean better. He has some management skill you don’t have!
2
u/nathena_19 Mar 09 '23
it is possible your boss will add more “rules” inside this code, the plateNumber is just one of them.
2
u/FrezoreR Mar 09 '23
hadValidateValues hurts reading :/
2
u/stringTrimmer Mar 09 '23
This is why my license plates still haven't been delivered?
2
u/maffoobristol Mar 09 '23
This post gives me the vibe of a junior developer who nitpicks very specific things as being "bad" when in reality it's fine, and there a bigger issues to be worrying about.
The main issue I see is that it's a greenfield project in 2023 using React class components and not hooks...
4
u/[deleted] Mar 09 '23
The only thing I see wrong here is the missing
TODOto finish the validation function.1
u/goosemano82 Mar 08 '23
Why the lambdas vs explicit function definitions? I’ve seen other devs do this as well, is this an effort to avoid setting obj protos?
1
u/Neckbeard_Prime Mar 08 '23
This looks like the
_hasValidateValuesfunction was supposed to contain more field checks, but they were never added in and the code was just abandoned as is.1
u/tbagrel1 Mar 09 '23
This is JS, so
_hasValidateValuesis not a NO-OP: it's a boolean cast!Is it what the author really meant? Probably not. Is it error prone in this context? Probably (I'd prefer reading "undefined" instead of "true" if the underlying value has not been initialized, it's easier to spot the error).
1
u/Haaxor1689 Apr 05 '23
Number one rule to follow while coding should be Keep It Super Simple and this definitely isnn't it.
-6
u/thekwoka Mar 08 '23
react class components? now thats bad.
-37
u/GreenKi13 Mar 08 '23
god i hate javascript and all the script kiddies importing 5,000 libraries.
-1
u/[deleted] Mar 08 '23
What is bad about this ? Is it the super plateNumber validation?
-3
u/Understanding-Fair Mar 08 '23
Syntactic sugar, but still not so great.
-22
u/imagebiot Mar 08 '23
Wow that is trash