Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> It's seldom OK to use static variables even with accessor functions. Wrap the state that the functions operate in structs that you pass to functions.

Why? I note that the word "because" doesn't appear once in your post.



> I note that the word "because" doesn't appear once in your post.

Sorry, my rant was a fairly long to begin with :D

To clarify my position, I'm maintaining and developing as my day job a few MLOC CAD application that is two decades old which has core written partly in C. This is one of the most painfull antipatterns plaguing that codebase.

It's seldom OK to use static variables even with accessor functions. Because:

1) Understandability. Suddenly it's non obvious what the state actually is that your code modifies. Functions are not functions that get input and process output, but become detached nodes in a non-obvious call graph. Yes, it's embedded in the stack, and, yes, you can figure out ways to figure out what is actually happening but it's not obvious from the code. There are quite a few non-obvious things affecting a real world production code. It does not help to add a few non-obvious patterns which are not necessary. I.e. this pattern creates unnecessary accidental complexity that hinders all velocity.

2) Suddenly it's impossible to structure the code to parallellize it. The explanation is fairly long but if it's non obvious I can return to this point.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: