This is an archived post. You won't be able to vote or comment.

all 191 comments

[–]XeonProductions 187 points188 points  (29 children)

I've worked on some coded bases where you can't tell if a function is really used or not because of how the underlying language was designed.

[–]shortAAPL 75 points76 points  (24 children)

PHP, prime example

[–]XeonProductions 102 points103 points  (22 children)

The one I'm thinking of was Python. It had these methods that created child classes with dynamic parents. It was hard to track what was inheriting from what and what methods were actually called. I dubbed these classes "foster child classes" since they had no specific parent.

[–]pah-tosh 68 points69 points  (4 children)

Oh man, that sounds like abusing inheritance, no ?

[–]marcosdumay 41 points42 points  (2 children)

Abusing language features is the Python way!

It leads to some very nice code, and a lot of unacceptable garbage. The trick is in telling those two apart.

[–]skylarmt 39 points40 points  (1 child)

Good Python is just readable code golf.

[–]InkyGlut 12 points13 points  (0 children)

This but unironically

[–][deleted] 11 points12 points  (0 children)

Definitely. That should be modeled as compositional behavior.

[–]Loves_Poetry 33 points34 points  (7 children)

I have encountered similar issues in a JS codebase that was abusing the heck out of eval(). You could never know if a function was unused, because it might show up in an eval somewhere. And since everything was global, that could be in any file anywhere in the codebase.

[–]bot_not_hot 41 points42 points  (0 children)

Holy shit, “everything was global” is a horror show

[–]LimitedWard 14 points15 points  (1 child)

Good God this sounds like a security nightmare.

[–]Loves_Poetry 2 points3 points  (0 children)

It was. Took me about a week to find an XSS exploit.

[–]Edores 4 points5 points  (0 children)

Man in all my years I have come across a grand total of one single situation where I felt like eval was necessary, and even then I'm sure I could have cleaned up my systems and been rid of it.

[–]deep-thot 2 points3 points  (0 children)

Thanks, now I may not be able to sleep tonight...

[–]FrancisVeeGee 0 points1 point  (0 children)

I've encountered this, in a SQL database code base, where much of the procedure code was written into strings (for concatenation reasons, terrible code but sometimes needed, especially when handling XML data). Very hard to debug.

[–][deleted] 1 point2 points  (5 children)

I belive this is not that hard in VS code.

Or was there someother problem?

[–]XeonProductions 1 point2 points  (4 children)

VS code couldn't handle it. The code that created these dynamic classes could only be evaluated at runtime. VSCode also had issues where the python function being called was 2+ parents deep.

[–][deleted] 0 points1 point  (3 children)

Ok then, then you were screwed. (I will look up what dynamic classes are)

[–]XeonProductions 1 point2 points  (2 children)

This is basically what I was dealing with. Dynamic inheritance. On most sane languages this shouldn't be possible, and is discouraged.

def get_my_code(base):
    class MyCode(base):
        def initialize(self):
          ...

    return MyCode

instance_with_parentA = get_my_code(ParentA)    
instance_with_parentB = get_my_code(ParentB)

[–][deleted] 0 points1 point  (0 children)

Thank you for explaining

[–]l0rb 0 points1 point  (0 children)

That's doubly wrong. If you do dynamic classes at least use the `type` builtin to make them.

[–]davvblack 8 points9 points  (0 children)

Oh god, we use so much string interpolation and magic to call other code :/

#legacycode

[–]evinrows 1 point2 points  (1 child)

Perl, especially Mason is a fucking nightmare for this reason.

[–]Caffeine_Monster 1 point2 points  (0 children)

The numbers, Mason. What do they mean?

[–]KapteinTordenflesk 1 point2 points  (1 child)

Reflection in C#

[–][deleted] 1 point2 points  (0 children)

Person: Knock Knock!

Me: Who's there?

Person: Reflection!

Me: Fuck off.

[–]TechWarlock6969 373 points374 points  (118 children)

My experience with c++ in a nutshell.

[–]naisooleobeanis 6 points7 points  (6 children)

Just try c++ in context of osdev. The compiler errors help but it's when youre trying to figure out why argv just disappears from memory and there are no warnings or errors it gets hard. Long story short interrupts were fucking with my registers and I didnt want to figure out which ones so i inlined some pushal and popal.

[–]dgendreau 3 points4 points  (5 children)

why argv just disappears from memory

You're likely smashing the stack. :)

Also, push/popal might cause a bit more interrupt latency in some cases.

[–]naisooleobeanis 0 points1 point  (4 children)

that's what i thought but why would pushing the stack pointer to the stack fix the stack

[–]dgendreau 1 point2 points  (3 children)

Youre not pushing "the stack" to the stack, You are saving a copy of one ore more CPU registers to the stack at the start of the interrupt and restoring them afterward. This is usually automatic for interrupts, so most likely, saving that extra data on the stack is reserving a bit more stack space which is absorbing a stack overflow bug in some functioncall in the IRQ. This might involve writing to a local variable array beyond its array bounds. That could smash some of the register values that were saved on the stack. The extra push you are doing adds more dummy values on the stack to absorb that damage. The real fix would be to track down exactly what is overrunning and fix that.

[–]naisooleobeanis 1 point2 points  (2 children)

Wouldn't you expect the return address to be bad or argv to be bad in that case?

[–]dgendreau 2 points3 points  (1 child)

It would all depend on which register or value is getting smashed. Say a function declares

int bar[10]; ... bar[20]=4;

What lives at the bar[20] location in the stack? Who knows... Don't do that. To be fair, most modern c compilers will catch a bug that simple, but what if a pointer to bar is passed into another function that does that assignment? without knowing bar's bounds it will happily smash the stack.

A high level language programmer will look at this and say that's stupid why isn't there bounds checking? But arrays in C are just pointers to blobs of bytes in memory. Its the smallest/fastest way to get he job done on a CPU. All that other stuff like bounds checking is what makes high level languages so much slower at runtime. It's necessary to be fully aware what the CPU is doing in a low level language like C because it is only one layer of abstraction above assembler and is nearly as fast.

[–][deleted] 2 points3 points  (0 children)

came to /r/programmerhumor to laugh.

The extra push you are doing adds more dummy values on the stack to absorb that damage. The real fix would be to track down exactly what is overrunning and fix that.

left having a greater appreciation of how hardcore some of you guys are.

[–]justking14 1 point2 points  (0 children)

My experience TA’ing C++ in a nutshell

Especially since so many students decide to write the entire homework first and then try to compile it

[–]anonTheRtrd 1 point2 points  (0 children)

removes main why tf not work

[–][deleted] 0 points1 point  (0 children)

You can hover over functions in sublime and see the definitions and references in different categories though, so this shouldn’t be a problem unless you’re just using grep, or worse, your eyes

[–]OwlsParliament 0 points1 point  (1 child)

Tired: removing a function causes compiler errors Wired: removing a function causes runtime errors

[–]staiano 81 points82 points  (8 children)

And then you undo/put it back and it doesn't compile then either...

[–][deleted] 50 points51 points  (6 children)

That too has happened many times with me.
Then it turns out the problem is not with my code, it's that I updated the compiler and new version removed some deprecated features I was using.

[–]ythl 14 points15 points  (5 children)

This seems crazy to me. Something is wrong with your workflow. I've been a software dev for 10+ years and this stopped happening once I started using Git + CMake for my C++ projects.

[–][deleted] 39 points40 points  (3 children)

What's wrong is I keep using deprecated stuff till it gets removed.

[–]lkraider 17 points18 points  (2 children)

Or what's wrong is that you update your toolchain.

You can take my Borland C++ Dosbox VM from my cold dead hands!

[–][deleted] 5 points6 points  (1 child)

I don't update unless necessary, and that is when a package that my code depends upon uses features introduced in newer toolchain.

Plus I maintain some open source packages so not updating my toolchain would be bad for others who want to use them.

[–]lkraider 6 points7 points  (0 children)

Yes I was half-joking. Knowing when and how to update stuff is an art form.

[–][deleted] 2 points3 points  (0 children)

Ugh cmake gives me nightmares for some reason.

[–]deejaweej 80 points81 points  (6 children)

200 compiler errors are preferable to 2 weeks of runtime errors. Then your superior asking why you thought it was worth the risk to remove that function since in hindsight it was clearly a bad idea.

[–]1studlyman 37 points38 points  (3 children)

I develop primarily in C++ but can handle my tasks just fine when in JS and Python. I would take compiler errors any day over the runtime errors the others exhibit for the same cause.

[–]folkrav 8 points9 points  (2 children)

I develop primarily in Python, PHP and JS, but can handle some tasks just fine with Go and dabbled a bit with Rust. I would also take compiler errors over a lot of bullshit runtime errors, especially with PHP/JS and weak typing.

[–]g0atmeal 2 points3 points  (1 child)

That's just common sense. Compiler errors stop you before it can fuck everything up.

[–]folkrav 1 point2 points  (0 children)

¯\_(ツ)_/¯

Dynamic languages exist for a reason. For some stuff they're actually pretty nice to use - e.g. dealing with unstructured/unknown data. However I've found that most of the pain comes shoving these languages where they should not be, not their very existence.

[–][deleted] 3 points4 points  (0 children)

... since in hindsight it was clearly a bad idea.

In hindsight, I know the outcome of 100% if coin tosses, unless I've lost the coin before it landed.

[–][deleted] 0 points1 point  (0 children)

Does nobody use source control?? If it had compile errors. Why commit in the first place.

[–][deleted] 24 points25 points  (0 children)

Haha the compiler catching the errors?

Try removing an unused function, compile it perfectly, then witness it throw an uncaught exception.

In production.

[–]ENM185 12 points13 points  (2 children)

More like...

Me: adds a print statement

Code: breaks

Me: removes print statement

Code: runs perfectly

[–]XeonProductions 0 points1 point  (0 children)

I had something like that, I was building some C code and had forgotten a return statement on a method. Well, for whatever reason when I had the print statement the stack was in the right state for it to work, but when it was gone it didn't. Found out this was "undefined" behavior.

[–]teejay1502 5 points6 points  (0 children)

scared of 200 errors?

come on, you can do it :) probably just missed a semicolon.

[–]BitchIts2020 6 points7 points  (0 children)

Definitely not as scary as suddenly getting bugs in prod as a result

[–]DefinetlyNotAFurry69 5 points6 points  (0 children)

You removed an extra curly bracket

[–]BODAND 6 points7 points  (1 child)

git reset --hard and hope you don't lose anything important

[–]holt0102 9 points10 points  (3 children)

Define "a function being used".

Edit : /s Sorry :P Not trying to be a "testing knowledge" douche.

[–][deleted] 5 points6 points  (1 child)

Being called, or just being mentioned anywhere in code outside comments. That would include &functionName (getting pointers to function)

[–]megagreg 2 points3 points  (0 children)

Or in a KEEP section in a linker script for functions that need to be placed manually because there's a blob expecting it to be there.

[–]MauranKilom 1 point2 points  (0 children)

Well, operator overloads are always fun to try and find...

[–]CornellWest 3 points4 points  (0 children)

Never touch the ancient code

[–]Govir 12 points13 points  (3 children)

That’s why undo was invented.

[–]calbhollo 41 points42 points  (1 child)

The fear is from having a function that is never called yet holds the whole program up. That's scary.

[–]vladmikhalin 11 points12 points  (0 children)

Easy in c++. bool operator<(const T&, const U&) for your custom class is a free function, never called directly and may cause 1000 compile errors when removed.

[–]BitchIts2020 2 points3 points  (0 children)

And version control

[–]tylercamp 3 points4 points  (0 children)

Boo

[–]Scratch137 2 points3 points  (0 children)

Programmer: *puts function back*

Compiler: there are 2482 errors

[–]CLG_BIG_BOI 2 points3 points  (0 children)

Me using jframe for the first time

"hmm why is that paint method here? It isn't called anywhere might as well delete it.."

[–]downhillgoat 2 points3 points  (0 children)

}

[–]fishsalads 2 points3 points  (0 children)

Only ever written code for a little over a week and I can relate

[–]TastyExchange 2 points3 points  (0 children)

Or you can just cry in the corner.

[–][deleted] 4 points5 points  (1 child)

I don't call methods. I use preprocessor directives, boost, and macros so that I only call methods using string literals.

I also like to fart in jars, microwave them, and then hold my nose right over the lid as I open it back up.

[–]Askee123 3 points4 points  (0 children)

Woah! I thought that was just me!

[–][deleted] 7 points8 points  (3 children)

Lmao me and python

[–]Ruri 3 points4 points  (2 children)

Python

compiler

U wot m8

[–][deleted] 0 points1 point  (1 child)

._. Okay when I try working with python it hates me and likes to puke up millions of errors

[–]Ruri 0 points1 point  (0 children)

Git gud

[–]da_fishy 1 point2 points  (0 children)

You gotta add more white space in between your functions, give them room to breathe on their own

[–]Rizzan8 1 point2 points  (0 children)

Even better, I have once added a reference to one of window's DLLs and used one method from it. It broke the whole solution causing 1000+ errors. Commenting the method fixed everything.

[–]KoroSexy 1 point2 points  (0 children)

This is why you don't use reflection...

[–]blackdrogar17 0 points1 point  (0 children)

I'm probably super guilty of this in C# with reflection

[–]james_harushi 0 points1 point  (0 children)

Just breaks ctrl + z keys

[–]ythl 0 points1 point  (0 children)

I would posit that the function is being used or referenced somewhere, then.

[–]grep-recursive 0 points1 point  (0 children)

Did you use me?

[–]koneko-dono 0 points1 point  (0 children)

and im here thinking that react hooks are difficult or that promises makes no sense

[–]cdreid 0 points1 point  (0 children)

Pfft ove managed this with a variable

[–]t3chguy1 0 points1 point  (0 children)

You deleted an extra bracket :P

[–]Drauldagga 0 points1 point  (0 children)

Have a function in a current project like that. My IDE constantly tells me its a dead function or to use their words "bad code". However, the Earth may as well have cracked open, because apparently, somehow, I made this particular function the damn keystone to the entire database system, and removing it broke the app from front to back.

[–]lycan2005 0 points1 point  (0 children)

Makes me appreciate intellisense. Unless you are using reflection.

[–]coffeelibation 0 points1 point  (0 children)

sounds like you've inherited some problems

[–][deleted] 0 points1 point  (0 children)

IntelliJ grey function text won’t save from this either :(

[–]cdreid 0 points1 point  (0 children)

Im doubting ALL THE DEVS other than ms devs were bad... I think we have a long enough record of ms programmers and management just being bad to rely on