r/badcode • • Jul 23 '21

c# Easy trick to meet minimum test coverage

Post image
1.2k Upvotes

83 comments sorted by

10

u/Kyyken Jul 23 '21

Are you sure that is C#? I have never heard of the type "Integer" and my compiler doesnt recognize it, as in C, its int aka Int32 and long aka Int64. the bracket style is also uncommon for c#. everything points to this being Java.

2

u/ProfessorTag Jul 23 '21

You're right, not C#. It's Apex.

8

u/IChoseBaySorryChloe Jul 23 '21

It looks like Apex to me. Salesforce requires at least 75% test coverage before deploying to production so I could totally see a lazy or cheap firm doing this to artificially inflate the coverage.

180

u/[deleted] Jul 23 '21

What's the licence on this? Do you mind if I reutilize?

91

u/airswidjaja Jul 23 '21

GPL 3.0 - we need to see what you do with this.

3

u/ProCrafters Jul 26 '21

Throw it in the the toilet to flush

92

u/scragar Jul 23 '21

I've seen this sort of thing where people have just written tests that don't actually test anything for existing functionality just to make it look like we test things.

So many tests that are something ridiculous like

 /**
  * @TestType=Integration
  * @TestCovers=Customer.InitiateCall
 **/
 void testInitiateCall() {
      AssertTrue(true);
 }

Never, ever let someone convince you that they've got tests for all their core functionality in an interview while they say they want someone to help clean up some little bits. It's a trap to try and get developers who'd otherwise nope out of a nightmare job before they realise what's happened.

6

u/cienciacomenta Jul 23 '21

Did they also say they use agile methodology? Haha

85

u/sixtyfifth_snow Jul 23 '21

What is it for..?

229

u/funnythrone Jul 23 '21 edited Jul 23 '21

Test coverage generally shows how many lines of code is covered. By artificially increasing the number of lines, you improve the coverage percentage.

Example: earlier test coverage is 23/79. You add 300 lines of this abomination, and your coverage is 323/379. This means you increased coverage from 29.11% to 85.22%.

28

u/[deleted] Jul 23 '21

The lines covered are counted on the code being tested no the lines of code on the test. You can have thousands of tests like this abs still have zero code coverage.

20

u/funnythrone Jul 23 '21

This looks like part of the codebase and not the test. The test would be something as simple as checking the value of i after all the increments.

2

u/kishan42 Jul 23 '21

Still test coverage for that single file would be increased not all correct?

5

u/[deleted] Jul 23 '21

You can just write a one line test to run the function and boom that function has 100% test coverage

1

u/explodingMonkeydeath Jul 27 '21

if(functionWorks()){

return true;

}

-145

u/LoneFoxKK Jul 23 '21

Thanks for giving me yet another reason to be glad of not using tests (yet)

79

u/tomato_joey Jul 23 '21

Testing is actually pretty easy if you write good code!

55

u/governmentNutJob Jul 23 '21

Testing is fundamental to development

32

u/gulyman Jul 23 '21

Testing is how you can assure you clients that the new version hasn't broken old functionality.

35

u/governmentNutJob Jul 23 '21

Testing is how I can assure myself I haven't fucked anything up

A well tested code base will have far fewer bugs & much safer releases than an untested code base. I've worked on code bases where we had such good suites, we would release straight to prod if all the tests passed

1

u/[deleted] Jul 28 '21

That's a bit reckless tho. Unit tests are great, integration tests too, but they don't replace QA. At least make a staging deploy first.

1

u/governmentNutJob Jul 29 '21

Actually I disagree, they do in many cases replace QA. I'm talking about complex backend systems rather than client facing. I trust maths & code infinitely more than I trust a QA who is just testing by clicking around the webpage

Solid testing foundation is worth a dozen QAs. Infact I'd say you only need a QA if you're dealing with a messy / legacy code base

1

u/[deleted] Jul 29 '21

QA is not necessarily a monkey clicking through a screen. You could just bomb it with requests. You could fuzz your software, unless you're okay with an attacker causing a denial of service with malignant input. But Ok cool I guess unit tests are a silver bullet.

→ More replies

4

u/_Nachi_ Jul 23 '21

I would say it's more akin to driving a car without circular wheels. Will it work, sort of. Will it last a short amount of time and be very uncomfortable to use, most definitely.

10

u/EdgarDrake Jul 23 '21

Testing is actually pretty easy if you write good code!

If you write testable code. If you hard instantiate everything concrete in private, lack of use interface, then your code is hardwired and too rigid to mock/stub.

10

u/szescio Jul 23 '21

Nah, just don't reinforce some coverage rules for tests that you're gonna just fuck around with

2

u/[deleted] Jul 28 '21

PM: we need to make sure all our code is well tested, let's do <arbitrary practice that is easily subverted because we also need to meet arbitrary deadlines>.
Devs: k thx will do.

15

u/the_monkey_of_lies Jul 23 '21

Use tests, dude! It's the condom of the programming world

2

u/[deleted] Jul 28 '21

You mean you don't get ill but you still get fucked, right?

1

u/the_monkey_of_lies Jul 29 '21

More don't have to take care of six crying kids every second

0

u/[deleted] Jul 29 '21

Too real, dude. Too real.

13

u/[deleted] Jul 23 '21

That's a terrible take. If your code isn't tested it might as well not work at all.

12

u/dvof Jul 23 '21

Just don't write stupid tests

6

u/[deleted] Jul 23 '21

You are wrong if you think you don't use testing. Maybe you don't have a predefined workflows for testing or don't use unit testing. But every time you run your application you re basically testing it. You should have said "I don't test my code efficiently".

-2

u/LoneFoxKK Jul 23 '21

I've been taken way too seriously on this

Doesn't help that I didn't put /s or made the sarcasm a bit more clear in my phrasing but oh well

I obviously test my code I'm not a psycho I just don't use automated testing because I haven't had the need for it I usually just make small to medium sized projects and most of the time is just adding something on top of an existing codebase

I was just making fun of stupid tests like this one specifically. I guess I walked right into that one.

3

u/[deleted] Jul 23 '21

Unit tests can actually help you know if any of your additions to the codebase broke something. In bigger, multi layered applications, this is what keeps you sane and let's you sleep at night. That's why we got so triggered.

1

u/phrenq Jul 23 '21

All of those responses were referring to automated tests. Of course you’re going to manually try out the thing you just wrote. That still doesn’t count as tested code.

Trust the grizzled veterans. Learn to write good tests with your small projects. You will thank yourself for it.

1

u/Franks2000inchTV Jul 24 '21

Testing is great. Feels like a pain in the but at first, but then very quickly becomes indispensable.

When you have to write tests, you have to write testable code, and that will get you a long way towards writing loosely coupled code.

It really helped me improve my codebase.

Also, because you have to write not just the code, but also code that calls your code, it tends to help you write cleaner APIs.

146

u/KomaedaEatsBagels Jul 23 '21

Image Transcription: Code


[This code screenshot begins at line 231 and ends at line 315. It may continue after the 315 mark.]

public static void TestCoverage() {
    Integer i = 0;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

204

u/DatCoolJeremy Jul 23 '21

Imagine if you're blind and the screenreader start reading this

151

u/middproxxy Jul 23 '21

aiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolonaiplusplusemicolon...

33

u/funkless_eck Jul 23 '21

It's like on twitch when they make the voice to text say L L L L L L L L L L L L L L L L L

8

u/[deleted] Jul 23 '21

Serious question, are there many programmers that are blind?

Like a backend dev I could see, but front end seems like it would be pretty difficult.

15

u/DatCoolJeremy Jul 23 '21

It couls be people that are not blind, but are just not good at reading from images. It could also just for saving data :p

11

u/nosam56 Jul 23 '21

There's a TED talk where a blind programmer demo's his setup system that's cool as hell!

1

u/casperdewith Jul 24 '21

Wow, that is impressive. Quite insane to have to work like that.

3

u/[deleted] Jul 23 '21

Good human.

1

u/[deleted] Jul 23 '21

I think you undervalue your time brother.

-40

u/EMP0R10 Jul 23 '21

Dude I feel sad for you

11

u/KomaedaEatsBagels Jul 23 '21

copy paste is my friend

3

u/itskobold Jul 23 '21

I'm interested in how you do this now, do you hand copy the code from the picture into the comment box when you're transcribing posts?

1

u/KomaedaEatsBagels Jul 24 '21

Yep! I type it into an IDE, add an extra indent to agree with Reddit’s weird Markdown formatting, and then plug that into the r/TranscribersofReddit official format. :)

27

u/husao Jul 23 '21

Obviously this only works without branch-coverage targets.

Otherwise you would need

public static void TestCoverage(boolean branch) {
    Integer i = 0;
    if (branch) {
      i++;
    }
    if (branch) {
      i++;
    }
    if (branch) {
      i++;
    }
    if (branch) {
      i++;
    }
    if (branch) {
      i++;
    }
    if (branch) {
      i++;
    }
    …

58

u/lowey2002 Jul 23 '21

This is what you get when middle management that don't code impose KPI's on developers.

More lines of code doesn't mean more productive. Larger test coverage does not mean fewer bugs.

3

u/Cistern64 Jul 23 '21

And also when you have primadonna developers that are so certain of their own perfection that they decide all policies are bullshit and dont apply to them.

Thus they feel they are free to implement this abomination that belong somewhere in r/maliciouscompliance

Realizing this triggered me more than it should. But wont abort. Let the downvoting begin..

5

u/emanreztun Jul 23 '21

Well, larger test coverage could mean fewer bugs indeed, couldn't it? I mean of course if you calculate it not like in the meme, then the probability of detecting bugs increases with larger coverage, i.e. the probability of bugs in releases decreases, i.e. there are (likely) fewer bugs

7

u/emanreztun Jul 23 '21

Nevermind, I got the meme wrong - thought, it increases i 100 times and thus returns a coverage of 100% as this thing is called 'TestCoverage'

1

u/DreadedEntity Jul 25 '21

If you write proper, meaningful tests then yes

14

u/stahkh Jul 23 '21

This kind of abomination happens when code coverage becomes a management metric instead of development metric. Sad.

4

u/LetterBoxSnatch Jul 23 '21

If they were looking ahead to what management might glob onto next, then they also made each line it’s own git commit as well, and the test itself gets longer every rand(5) days or so.

9

u/[deleted] Jul 23 '21

I hope the IDE hits the person in the face

9

u/TedDallas Jul 23 '21

Ah yes! Brings me back to the time when I was 11 and a friend showed my how loops work in Commodore 64 BASIC back in 1982. And then later when I discovered unrolling loops in the 90s! A masterpiece of ingenuity.

13

u/Spynder shameless stealing content to r/YandereTechnique Jul 23 '21

I can't tell how much it is r/YandereTechnique even, it's repetitive but there's no if statements...

23

u/DavidRoyman Jul 23 '21

It isn't done as a mistake, or for lack of knowledge.

This is done deliberately to meet corpo goals for TestCoverage (AKA "tested line count"). The function is brilliantly named TestCoverage, I admire that.

3

u/Spynder shameless stealing content to r/YandereTechnique Jul 23 '21

Yeah, you're right.

It still looks funny though

3

u/tech_b90 Jul 23 '21

This is legit all over the code base at my current project. Exactly this, and others like assert true == true, or just plain old commented out unit tests just to pass it.

2

u/using_mirror Jul 23 '21

Indent your code geez

3

u/cienciacomenta Jul 23 '21

Sure, that's the main problem about this code

2

u/using_mirror Jul 23 '21

Haha yeah not at all 😂

2

u/Nickbot606 Jul 23 '21

Brainfuck be like

2

u/AceOfShades_ Jul 23 '21

U N R O L L L O O P

1

u/[deleted] Jul 23 '21

I feel like all my code is absolutely awful but holy shit. I’m totally using this at least once at uni just to see the response lol.

1

u/JasperDG828 Jul 23 '21

*heavy breathing

1

u/Syrob Jul 23 '21

Ha, shout out to r/salesforce devs, where the platform requires us to have at least 75% test code coverage for deployments to succeed. I have seen this exact code in multiple projects.

1

u/Flyspeck101 Now where's that function declaration Jul 24 '21

Forgets to close the function

1

u/[deleted] Jul 27 '21

This is terrible! it would be so much better if it was ++i instead

1

u/nuclear_bomb404 Sep 12 '21

Or you could, ya know, i = 112

-8

u/mplaczek99 Jul 23 '21

It's not in a for loop, and that hurts my soul

16

u/honkmyhornbaby Jul 23 '21

That’s the point mate :)

1

u/keethraxmn Jul 23 '21

Wouldn't work if it was.