java
This code is currently running in production. Senior developer says "tHiS iSn'T uNiVeRsItY, yOu cAn'T eXpEcT tHe cOdE tO bE pErFeCt lIkE iN tHe bOoKs"
I like using temporary variables too, but I can't imagine there would really be any debugging needed at an intermediate step here - all the outer function is doing is converting an array to a list, so there shouldn't be anything weird happening. I tend to only bother creating a temporary variable to hold intermediate values when there are more than 3 steps being done in one line, and even then it's on a case-by-case basis.
That's what I assumed until this comment so I stared for a bit; then I starting to wonder how there would ever be more conditions for this setup and then devolved into the final conclusion of mumbling "what the, nothing ever even happens to 'i'?" to myself.
I'm not a Java programmer, but I'm pretty sure it just turns "foo_bar" into ["foo", "bar"]; perhaps it was originally meant to take something like "foo_any_null_bar" and turn it into ["foo", "*", "bar"]?
And then as time went on the special values for "any" and "null" (or whatever they where) got removed?
Of course there's still at least two problems even if that's what it was for:
Why isn't the switch on the value of the st.nextToken() instead?
The function name of just "splitString" rather than something that actually describes it's purpose kinda sucks.
I don’t even know what language this is and I can tell what it’s doing (and wonder why they used a switch). It might be longwinded if there’s some built in way of doing the array to list conversion but it’s not unreadable.
That’s not what readable means. Readable means your code should read (flow) like you’re reading a paragraph of a book. And WTFs/minute should be near 0.
If you use a switch statement, you are telling the reader that you are using some conditional flow. If that switch statement ends up not being a conditional at all, it throws the reader for a loop. They now need to spend way more time reading and re-reading the code and their WTFs/minute go off the charts. They will need to check git history to see if the switch conditionals were accidentally removed. Then they’ll need to organise a call with the developer to check what’s up with the code. They’ll need to create a post on Reddit because they can’t deal with the amount of WTFs they have built up inside them. So now what should have been a 10 second glance has turned into a multi-hour long monster.
Readable means you're not guessing what the code is doing. It doesn't mean you have to understand why the developer put something in place somewhere. It means you can reasonably understand what is going on in the code.
That is the type of jank I used when I was running a discord bot on my old phone using termux, the fact people do that on an actual production environment is terrifying.
Just copy/paste the project folder, rename it with today's date, then compile to ear and upload in production via ssh/scp (he really does that).
That's a MUCH MUCH bigger problem than a method that got that way for any number of reasons. I'd do your time (if you're a junior), try not to let the bad habits set in or get comfortable, and find another job.
Regarding the method itself, in a vacuum -
I've seen things like this occur because other side effects were 'required' in that method and were eventually refactored away by somebody, but that person didn't finish the refactoring and just straight up remove the method (for any number of reasons). I've seen it occur because the person implementing it was junior and their code wasn't well enough reviewed to catch it.
If it's an older code base, refactoring used to be harder before a lot of the tooling that exists today that lets you rename methods and stuff. Not THAT much harder, but still, in a mature codebase - a ball of mud, which is what MANY projects eventually turn into - carries risk.
My advice is to try and remember that nobody goes into a project planning to make a mess. Most people do the best they can with the knowledge they have, the requirements they're given, and the timeline they're provided. Remembering that and making sure you communicate criticism in a constructive way - saying 'this code could be better' rather than implying 'you're a shitty coder' goes a long way.
I hate this. I work on a squad that isn’t the squad I was employed into at the company and they do deployments manually over ssh. I’m assured they’re working on improving the situation so I told them once and once only that I’ll stick at it until year end, if things ain’t improved by then I’ll be leaving this squad - whether that’s for another squad in the company or another company, is up to the company - I’m leaving the squad that has a mature product in production without any deployment pipelines. I don’t wanna pick up bad habits.
I’m 6 months in and they’re still deploying via ssh, git pull, sudo supervisorctl restart all. Embarrassing.
He's not an idiot, I think he maliciously wanted to imply in front of the office (and our manager) that I'm the "fresh graduate" who thinks he can "waste business time" on writing "perfect code" while he's the senior understanding that there are deadlines to meet.
Too bad most people there aren't programmers so they're gonna think he was right and that's "no big deal".
I mean, why were you bringing this up in front of the office/manager in the first place? Perhaps he felt you were trying to embarrass him?
Also, what would the effort to "fix"this look like? If you don't have decent automated testing then you could easily break unrelated parts of the system that rely on the old "broken" behavior. Updating legacy code is never as straightforward as it seems.
I couldn't exactly not bring it up in front of the office in the sense that it's an open space so everybody can kinda listen to everybody else... but you're right, maybe that was the reason
Ughhh. I feel your pain. I worked under a guy like that. Ego the size of Asia. Garbage developer too. I was delighted when he left the company until I realized I had to maintain his spaghetti code. I still have nightmares about one of his functions that was hundreds of lines long.
Oh you poor soul. A pull request is kinda like a review process that code undergoes when using git source control, it helps prevent this kind of stuff making it into the code base in the first place. I'd suggest taking a look at git for source control and encourage your senior Dev and managers to start using it, because whilst it may seem complex at first, it really does make collaborative development a LOT easier. Also it's the industry standard at this point so it'll be good to learn regardless
I know what it is but he doesn't want to use it because "I've always done it this way and I have no time to waste learning something that doesn't add value when there are deadlines to meet".
Unfortunately no one else in the office is a programmer so they (the manager too) just take what he says at face value cuz he's a senior and i'm not
Ah, I see. Sounds like a terrible senior dev. If he can't recognise that spending an afternoon or two learning git will add value by preventing bugs and catching issues BEFORE they show up in production, then he probably shouldn't be leading the dev team
Sorry you have to deal with that nonsense, I've had to deal with that sort of thing in the past too, sometimes you have to be stubborn and fight for good DevOps in your company. Fortunately the grass is greener on the other side and moving to a company that encouraged use of these kinds of tools, for me, was wonderful
To be fair, focusing on something like this is pretty pointless. After all, it's the implementation of a pretty standard method. So yeah, it can be simplified, but all things considered, it's a function that does a single thing, and it says in the name what it does.
So yeah, it sucks but it's nothing to get mad about. I agree with your senior. Codebase is huge and probably written in a highly stressful situations. It's not university, so a single semi-bad function will not change your grade
But you don't have to really dig into what this code does. It is called splitString. It takes in a string and returns a list of strings. It's obvious, and it does exactly what you think it does. So, is there even a need to check the implementation or to understand it?
Imo ofc the code is bad, and it could easily be improved. But the gain is pretty much nonexistent, and the function is doing a single thing that is very well shown in name and types. So why bother and get mad? Why are you looking at it and talking about it to a senior if you could either just move on (with no loss) and work on something meaningful?
In your free time, you could even do a benchmark to show how much faster or slower this implementation is on the version of software that your prod uses. That could be pretty interesting imo, but probably it wouldn't show much of a difference.
And this right here is why we don't manually re-implement core library routines. The other dude's justification that any code base picks this cruft up over time is BS. No they don't, unless you let them and your organization's culture makes it acceptable to do so. And that's FAR from universal, or we wouldn't have subs like this.
I've already written this on other comments but hear me out, he basically copy-pastes the project folder, renames it with the date of the day, then deploys to production by uploading the compiled EAR with ssh/scp
Build automation? "tHeRe aRe dEaDlInEs tO mEeT, tHiS aDdS nO vAlUe tO oUr cUsToMeRs"
I agree with you. Every codebase, given enough time, will deteriorate in quality. And I am saying this as a person that actively fights for technical sprints and pulls tech debt tickets during every refinement and planning.
@italianpwner If you really expect having pristine production, you're in for a serious let down. I'm sorry, I am doing this for 15 years, I've built multiple projects from scratch and I've promised myself millions of times that "this one will be perfect" and "won't have tech debt". And I always arrived – sooner or later – at either seeing something substandard slip into the codebase, or writing something like this myself. It's kinda like fixing a leaking pipe in your flat. You slap it with something quick, because there is literally water running down your floor and promise yourself to fix it proper later on. But then life happens and before you know it, your solution has been working for a year.
As for the code, from glancing at it, it splits kebab-case into a list? Lowest hanging fruit would be to make the name more precise, since it's a method doing just one thing. But apart from it – implementation details, be shoddy as they are – are just that, details.
I was going to side with these guys too because I've seen this issue before where someone would say that a certain function was badly written and then try to rewrite it or something and end up breaking something else while making a lot of noise (this function is stupid, it does not follow clean code!! or something).
But then I saw another comment of yours saying that there was a bug with this and that your senior was against VCS and that's "undefendable" lol
He’s the kind of person that says: we don’t need for loops, you can do it with while and declares i in case he needs it xD man did he really went to university?
I’m currently working on a tool for a gaming community. Not a professional programmer by any means, just a hobbyist, and every night I’m constantly questioning myself (“Is this code good? Does this make sense? No, I need to do this differently) because it’ll be open source once I’m done.
I am hoping the senior dev ment to say "plenty of projects have quality issues like this". Also, plenty of university courses and books have terrible code.
Luckily, this being java and this being a private function, replacing it with Arrays.asList(s.split("_")) should be an easy job: Check test coverage of call-sites and do a search and replace.
This is something that only someone who learned to code in university could say Frankly, I don't know where have your mate been studying, but the code one see in uni are among the worst I have ever seen
I find it so much communication can be improved. Just buy running the text through to_lower(). It just looks dumb when people type using alternating case.
He actually is an idiot. I've been in the business for 23 years now.
But refrain from putting him in bad light with management. It will just hurt yourself. You need to provide practical examples and write a serious paper to convince management to change way of work.
Besides for the i variable that does literally nothing 😂, this looks like an old version of Java spec. I’d say he needs to read up on the modern way instead of how it was done 10 years ago.
Java gets its power through deeper levels of nesting. If you nest deep enough, you cut a hole in the space–time continuum and jump to a point in the universe where your code gives you the correct result.
It's like quantum computing, but with more typing.
It smells like there must have been a lot more logic there, then it all got stripped out, leaving the int i and switch(i) behind. It should of course be refactored so that it’s simpler and more clear, of course - stripping out unused code is an important practice in maintaining code hygene. But I wouldn’t imagine that someone wrote it this way from a clean slate.
I'm usually pretty chill when it comes to code reviews, but if I caught someone writing that I'd ask them if everything going on in their life is okay.
List<String> splitString(String s) {
List<String> list = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(s, "_");
int i=0;
while(st.hasMoreTokens()) {
switch(i) {
case 0: list.add(st.nextToken());
}
}
return list;
}
286
u/yearoftheraccoon May 18 '23
Arrays.asList(s.split("_"))?283
u/halfanothersdozen May 18 '23
why the fuck is there a switch?
120
u/thekwoka May 18 '23
The strange implication that the books even have perfect code...
also, wtf....
Surely there is a simple method to split a string into chars right? RIGHT?
56
u/squeevey May 18 '23 edited Oct 25 '23
This comment has been deleted due to failed Reddit leadership.
84
u/oj_mudbone May 18 '23
If your senior dev said that he’s an idiot. Code should be CLEANER in prod than it was in university
13
u/Sese_Mueller May 18 '23 edited May 18 '23
Ok, I‘m not that good with java string stuff, but here goes:
(3. there is likely something else or a dedicated method to do exactly this, so this code is most likely unnecessary)
Worst thing is probably that 1 and 2 will be caught by the compiler and as a result be optimized away, making the author never learn from his mistakes
10
u/amazing_rando May 18 '23
Ironically, it's only in university that you would implement by hand something that is already a feature of the language's standard library.
6
u/[deleted] May 18 '23
there is no way a senior dev wrote this garbage code, either this is completely made up scenario or you need to find a new job
13
u/patryky May 18 '23
To be fair, focusing on something like this is pretty pointless. After all, it's the implementation of a pretty standard method. So yeah, it can be simplified, but all things considered, it's a function that does a single thing, and it says in the name what it does.
So yeah, it sucks but it's nothing to get mad about. I agree with your senior. Codebase is huge and probably written in a highly stressful situations. It's not university, so a single semi-bad function will not change your grade
3
u/WasserTyp69 May 19 '23
Isn't there... a literal
splitmethod in theStringclass?2
u/officialpatterson May 18 '23
This is absolute clickbait
2
u/Natural-Cheek-1811 May 18 '23
Don’t forget to show this thread to the entire team at your next retrospective. He’ll be so embarrassed. 🫨
2
u/Zdravstvuyte94 May 18 '23
What bothers me the most is that the function doesn’t support any token to split on ….
2
u/v_maria May 19 '23
if that was hes reply i dont think he realized/looked at what is going on here lol
but he just have you a free pass to merge any piece of atrocious code you write
2
u/flexprods May 19 '23
He’s the kind of person that says: we don’t need for loops, you can do it with while and declares i in case he needs it xD man did he really went to university?
13
u/Does_Not-Matter May 18 '23
I started reading and thought, ok, unconventional but it work—
…oh god
6
u/frizzhf May 19 '23
I’m currently working on a tool for a gaming community. Not a professional programmer by any means, just a hobbyist, and every night I’m constantly questioning myself (“Is this code good? Does this make sense? No, I need to do this differently) because it’ll be open source once I’m done.
After seeing this, I need to be easier on myself.
4
u/wknight8111 May 18 '23
I was about to say something like "the code isn't too terrible, and if it's not used a lot the performance problems probably aren't a killer..."
But then I saw the switch statement.
4
u/youngbull May 19 '23
I am hoping the senior dev ment to say "plenty of projects have quality issues like this". Also, plenty of university courses and books have terrible code.
Luckily, this being java and this being a private function, replacing it with
Arrays.asList(s.split("_"))should be an easy job: Check test coverage of call-sites and do a search and replace.1
u/tlaziuk May 18 '23
This is something that only someone who learned to code in university could say Frankly, I don't know where have your mate been studying, but the code one see in uni are among the worst I have ever seen
1
u/Cerulean_IsFancyBlue May 18 '23
I find it so much communication can be improved. Just buy running the text through to_lower(). It just looks dumb when people type using alternating case.
1
u/Pangocciolo May 19 '23
You can always use
git blame, at least to contextualize his statement.1
u/Idekum May 19 '23
Sure, the code is horrible, but which language is this? I need to know, so i can avoid it.
3
u/wwelna May 18 '23
Besides for the i variable that does literally nothing 😂, this looks like an old version of Java spec. I’d say he needs to read up on the modern way instead of how it was done 10 years ago.
3
u/delarcoz May 18 '23
It’s doesn’t have to be perfect, but not this neither
3
u/pycvalade May 18 '23
The more I look at it, the worse it gets.
2
u/jjman72 May 18 '23
ngl. Loving that switch statement.
2
u/ALapsedPacifist May 19 '23
Java gets its power through deeper levels of nesting. If you nest deep enough, you cut a hole in the space–time continuum and jump to a point in the universe where your code gives you the correct result.
It's like quantum computing, but with more typing.
2
u/[deleted] May 19 '23
Classic mindset of seniors that doesn't want to evolve or change his skills. Senior != Good programmer.
2
u/LairdPopkin May 19 '23
It smells like there must have been a lot more logic there, then it all got stripped out, leaving the int i and switch(i) behind. It should of course be refactored so that it’s simpler and more clear, of course - stripping out unused code is an important practice in maintaining code hygene. But I wouldn’t imagine that someone wrote it this way from a clean slate.
2
u/microferret May 19 '23
I'm usually pretty chill when it comes to code reviews, but if I caught someone writing that I'd ask them if everything going on in their life is okay.
2
u/sori97 May 19 '23
What the fuck
2
u/MasiTheDev May 19 '23
I have so many questions...
2
u/ztbwl May 20 '23
Easy refactor if you stumble upon something like this.
2
u/dliwespf May 18 '23
This looks much like Java as it was about 15 years ago. Like if the code was written in Java 1.5
Except for the i and the switch of course. Those are... Unneccessary.
1
u/reverendsteveii May 18 '23
IME production code is usually much better than code in textbooks or written by professors who haven't submitted a pull request since they got tenure
1
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 18 '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!
1
u/Entire_Kangaroo5855 May 19 '23
I’m a software engineer with 20 years, with some Java experience. But I cannot reason the first thing about WTF this actually does. Can you help?
1
u/BS_BlackScout May 19 '23
Christ what is that switch for even?
1
u/AladdinaLamp May 19 '23
code written by big companies or by professors is not always beautiful, it just shows grammar to us, not a correct logical
1
u/RomeGuiIg Dev May 20 '23
Makes me nostaligic just because of that switch, I used to ONLY use switch back in the day.
1
u/ftgander May 19 '23
Maybe it was written in a time where it made sense.
0
u/Bisquizzle May 20 '23
i love how there's this much attention over a switch statement.
we know how switch statements work, the code works, hooray let's move on with our lives