A legacy system I worked on had a logging table in the DB with an action_id column. The action ID's were mapped to constants in the code. The file that contained all the (130+) action constants and their ID's had a comment at the end:
// Increment this number if you change the max action ID!!!!
// 132
And I don't recall ever seeing it match the actual ID count, so I 100% believe it lol.
EDIT: found it!. The current highest ID appears to be 158.
im a .net guy and I always hear about java being so verbose that it takes waaay more effort to do simple things, but to me it seems just like c#. Is c# also overly verbose, or is the java thing not really THAT bad?
I'm a Java Guy, C# basically is Java, it was based on it, they just made some improvements. I would have argued in Java's favor a few years ago, but after the Oracle Java acquisition and M$ releasing cross platform implementations, C# is better.
This is Java compiling Java.. There is no Runtime.getRuntime().exec in C#. There is no ByteArrayOutputStream in C#. There is no String constructor with (string, encoding). There is no waitFor method in C# Process class. (WaitForExit). You were saying?
Create a class that stores the number and whether or not it's even. Use hibernate to persist this class to a database. Then whenever isEven is called you use isEvenService to check if the result has already been computed. Now that's what I call optimisation.
... I see your time working with offshore development teams has been mercifully limited.
This really IS - EXACTLY - how Indian offshore teams tackle problems. No regard/effort put into anything except the immediate problem.
It makes me want to wrap my lips around the barrel of a gun.
EDIT: "It's a cultural issue." eyeroll.png. Yeah. Amazing how, if I'm an Indian working onshore, getting paid $150/hr, code like this would be a HUGE issue... but since I'm offshore at $20/hr the issues are brushed aside. Hmm.
I think the only "cultural issue" that needs addressing is in the boardroom, and maybe it can be addressed by a hard talk around shortsighted profit taking, and getting what you pay for.
Sounds real. This smells exactly like when they demand the bug reporter to list all exact steps and then fix only that exact scenario. You get infinite number of tickets to assign/fix/test/deploy/manage in your lovely job where you get paid for time, not for result.
Step 1, write a proper isEven function, Step 2, autogenerate a check for all numbers till max_int, Step 3, use previous isEven function to return true or false for every check Step 4, remove previous isEven fucntion Step 5, save generated list of checks as new isEven function() Step 6, ??? Step 7, Cry with profitz
Reminds me of the my classmates used to bend over backwards to avoid the % operator in intro CS classes because they couldn't be bothered to understand it.
You'd be surprised how many people forget first grade stuff. In there defense though, I can't really remember having much use for the concept of remainders before I learned to program.
Modulo is like the most useful operator out there. You can add speed to games every n points, make numbers loop on each other in recursions and other fun stuff.
Reminds me of myself, I forgot the % in an exam in school. We also had to write a piece of code to check if an int is even or not. My solution was to divide the int by two, then multiply it by two and check if it was the same number as the one given... worked like a charm :D
They are the same if the dividend and divisor are positive.
But if one or both are negative, then things can get a bit weird. The convention used can also change depending on the programming language. More here.
Given two numbers, a (the dividend) and n (the divisor), a modulo n(abbreviated as a mod n) is the remainder from the division of a by n. For instance, the expression "7 mod 5" would evaluate to 2 because 7 divided by 5 leaves a remainder of 2, while "10 mod 5" would evaluate to 0 because the division of 10 by 5 leaves a remainder of 0.
Wait for a "senior offshore developer" to look at this code then chide you for not writing unit tests, and especially for using a ternary operator "because it's inefficient."
Current code returns correct answer when number is uneven. Which is 50% of the time. Plus it returns correct answer for few even numbers. Therefore current code returns correct answer more often than just return 1;
If this person has a job, it gives me great hope about my future prospects. I mean how on earth do you spend more than 4 weeks learning CS and not know when to employ a for loop?
There is nothing clever about it. It’s Boolean logic. The foundation of all digital/binary math. It gets taught in basic CS in high school (or it used to). It’s not optimizing. If you know what % is then you really should know what & | are. It’s like being taught how to write but you don’t know what a pen is.
I think it really depends on with which programming language in mind you are reading this. Your / 2 for example might behave differently from * 0.5 and / 2.0 in some of them.
Also performance for / 2.0 can be lower than * 0.5 in some languages and an optimizing compiler might not find every case where it can invert it at compile time. But that I would say does deserve a comment that it is a performance sensitive area.
I wouldn't say commenting is necessary. The function name is what is happening and this bitwise operation is a foundational idiom I would say. This is definitely not "clever" code. It's just common knowledge.
It's true that premature optimization is bad and it's also true that an optimizing compiler should catch this. However, sometimes things slip through the cracks and I really disagree that bitwise operators reduce readability in this example. In that case, you may as well put the optimal implementation in place.
Premature optimization is bad, but writing clean code that is optimized isn't the same thing. I think this is a case of the latter.
Added note, if this bitwise operation was mixed in within a large function, I'd agree with you, but it isn't. Either way, I think we could both agree the best approach would be to factor out is even and mark it inline anyways.
To the dude that deleted their account (as I can’t reply to a deleted comment):
I’m really kind of bummed that you deleted your account and comments. If you where trolling then I understand. But if you weren’t then we all missed out on an educational conversation. I’ve been doing this coding shit for 35 years and all I really know is that there is way more to learn. I would have enjoyed getting to the bottom of your objections to this method. Many others have commented that, depending on the language and the method with which the language is interpreted, this code could be more inefficient. I do it this way because I started with asm and this is the most efficient way to detected oddness, but who knows how js or java or C# or other languages (especially interpreted) deal with this. We could have all had a conversation based on actually writing code and doing some basic benchmarking. Anyway dude, happy new year and please come back. We can only get better together.
In early BASICs it was common to treat booleans as numbers
hours = 23;
hour = hour - (hours > 12 * 12)
rather than writing an if statement.
Somewhere I have a a single line of code that translates HH:MM:SS to h:mm:am with no 'if' statements, one nasty print statement using the above tricks.
776
u/amicloud Dec 19 '18
...no you fucking didn't, no way that's real.
177
u/[deleted] Dec 19 '18
Well they almost had all the even number covered. At least the ones in the range of numbers they know.
304
u/pixel_sharmana Dec 19 '18
When you are paid per line of code
131
u/[deleted] Dec 19 '18
You just added more checks right?
104
u/pqowie313 Dec 19 '18
Reminds me of the my classmates used to bend over backwards to avoid the % operator in intro CS classes because they couldn't be bothered to understand it.
219
u/triggerhappy899 Dec 19 '18 edited Dec 19 '18
Psssh amateurs, you could easily add the ability to check higher numbers
Just remember to set the number inside the for loop high enough to what you need. Ezpz
Edit: I am so humbled to be in the presence of all you intellectuals
25
u/UnchainedMundane Dec 19 '18
there's an easy fix:
if (a > 14) return isEven(a - 14);22
u/Serundeng Dec 19 '18
* QA proceeds to enter -2*
16
u/newPhoenixz Dec 19 '18
// :TODO: Add at least another 4 billion checks
80
u/bsep1 Dec 19 '18
public static int isEven(int a) {
if(a == 0) return 1;
if(a == 1) return 0;
return isEven(a+2);
}
18
u/Andernerd Dec 19 '18
You can't be serious... it hurts to think that this person was probably paid more than me.
9
u/Isvara Dec 19 '18
Did you add more checks?
7
u/pxOMR Dec 19 '18
You should return "Yes" instead of 1 and "No" instead of 0, it makes the code look cleaner.
/s
7
u/pootisEagle Dec 19 '18
// TODO: Add more checks.6
u/Montuckian Dec 19 '18
I can't even.
10
u/Frichjaskla Dec 19 '18
Use the power of recursion to cover more ground
5
u/[deleted] Dec 19 '18
I’m literally suffering from an aneurysm right now. Thanks I hate it
5
u/John_Fx Dec 19 '18
How many checks did you add?
9
u/InacmaR Dec 19 '18
r/thathappened
4
u/OverjoyedBanana Dec 19 '18
https://xkcd.com/221/
4
u/[deleted] Dec 20 '18
If this person has a job, it gives me great hope about my future prospects. I mean how on earth do you spend more than 4 weeks learning CS and not know when to employ a for loop?
3
u/lordpuza Dec 19 '18
Just curious , how will you guys solve this? Best that I could think of IsEven and if less than 14 ?
5
u/BluudLust Dec 19 '18
return !(number & 1)2
u/corner-case Dec 19 '18
Obviously the param ‘a’ is a 4-bit integer. I see nothing wrong with this.
2
u/Bioniclegenius Dec 19 '18
Wow, this is much better than the
that it was before!
2
u/bionicjoey Dec 19 '18
Relevant:
https://www.reddit.com/r/programminghorror/comments/77tg9z/z/dooj0ha
2
u/[deleted] Dec 19 '18
Obviously you just had to replace
ifs withelse ifs2
u/jstillwell Dec 19 '18
% 2
2
u/globalvarsonly Dec 20 '18
Just rename the function
isEvenNonNegativeIntUnderSixteen()Search and replace!4
u/[deleted] Dec 19 '18
Modulus operator brah, come on.
3
u/AttackOfTheThumbs Dec 19 '18
Where do you work. Was this code in production?
1
u/[deleted] Dec 19 '18
At least use a switch statement then
1
u/abdolence Dec 19 '18
The most frightening thing here is that last comment.
1
u/ivanjermakov Dec 19 '18
Everything is so bad with this code
1
u/thesocialpenguin Dec 19 '18
add more checks?!?!
1
u/sebnukem Dec 19 '18
// TODO: terminate myself1
u/sxule Dec 19 '18
This is my new test I'll give right before FizzBuzz. If they write more than one line, "Thanks for coming down. Now leave while you still can!"
1
u/[deleted] Dec 19 '18
Java doesn't have
%operators?1
u/wildlybland Dec 19 '18
That looks like dumb shit I used to write in college just to get an assignment over with when I first started taking programming courses
1
u/theblood Dec 20 '18
so... you added more checks ? :D
1
u/ouattararomuald Dec 22 '18
Best part is the TODO
1
u/papacheapo Dec 29 '18
This is what happens when managers offer their help to get the product out sooner.
1
u/[deleted] Dec 29 '18
Modulo anyone?
1
u/elschaap Jan 29 '19
At least NPM has a package for this
1
u/Dasky14 Feb 05 '19
FTFY /s
0
u/[deleted] Dec 19 '18
[deleted] — view deleted comment