by zx2c4 · 10y ago · 0 comments
Nicely put. :) Thanks for the link.
Needlessly increased variable scopes - and thus increased risk of harder to follow cross-dependencies - is probably the biggest downside risk. But I've found code that executes linearly easier to read and understand when it's written linearly. When everything is factored into functions - particularly impure functions - you need to click all over the place to understand the whole.
If there are higher-level primitives that can be used - filters, maps, folds, etc. - then they can reduce the verbosity overall. Some monadic styles can flatten nested code by computing a pipeline / computation rather than having lots of nested loops. But depending on the language and the problem domain, performance may suffer.
This is a great piece of advice and one that has worked for me in the past when refactoring code like the one referenced.
I was quite surprised at how often copy-paste-modify
operations resulted in subtle bugs that weren't
immediately obvious.
I now strongly encourage explicit loops for everything,
and hope the compiler unrolls it properly.
I found this particularly interestingI refactor as soon as my code gets past 3 levels, I don't know how one is supposed to readily understand this block of code.
The lack of comments isn't what troubles me. Comments get old and, when they do, they get completely misleading and downright dangerous. As soon as you feel the need to add a comment, it's often because that particular chunk of code should be extracted into its own function/method/whatever and given an intelligent name. Comments should be reserved for really important stuff, like when the code does something totally non-obvious.
http://hg.icculus.org/icculus/lugaru/file/97b303e79826/Sourc...
Question regarding index access: There are tons of for loops with player[k] occurrences and similar. Personally I would create a local variable and prevent further index-retrievals.
Is this worthwhile or would a compiler optimize the array-index access away? Or maybe subsequent uses of the same index are cached by the cpu? Does someone know?
Grabs the eye bleach
generally agree on commenting, but it's also about discipline - method-level commands are rarely updated, but if there is some particular algorithm within method, I don't see a reason to describe it beforehand on a line or two.
General rule - if method is longer than my screen, it's time to refactor. very rarely this isn't a good idea.
Consder this:
char* p1, p2;
You could erroneously think that char* is a type, and p1 and p2 are variables of that type. That's not correct however, since p1 is indeed char*, but p2 is char. Such declarations therefore should be strongly avoided for clarity.[1]: https://github.com/CRYTEK-CRYENGINE/CRYENGINE/blob/release/C...
So I always do char \p1. Yes, for a non-C programmer its not as intuitive, but C isn't intuitive a lot of the time (cough, char p1[] what?). But you avoid making these mistakes altogether.
Note in function declarations I use char
, because you cannot do statement expansion with a comma there, and because in headers the variable name is optional anyway. char *p;
?I prefer to use char* p; as well as std::string& s; and etc. rather than attaching the type syntax to the name, since in essence that's part of the type. But yes, it's a mess that this inherited C logic doesn't treat it that way clearly. To avoid such mistakes, it's better not to use comma declarations altogether, you can perfectly avoid them.
It's a good feeling when working with new languages like Rust which avoid such mess.
The point is, though, that while in a developers mental model you attach ref or ptr to the type, the language itself does not, and writing it out like it does can easily drive newer developers who do not understand the nuance to make hard to recognize mistakes as a result of it.
What can be the rationale behind writing this [1] ?
[1] https://github.com/CRYTEK-CRYENGINE/CRYENGINE/blob/release/C...
I suppose this is the result of deadlines and lax code reviews.
Long functions which have grown over time are also understandable, given the realities of changing requirements and such.
Thousands of lines of uncommented code, however, are not understandable... in the sympathetic sense, or literally.
"I've worked in the Games industry with age old engines, I currently work in embedded development with a code base over 10 years old - I'm fully aware of what "real life" code is like. There is no excuse for not writing well commented code - or even code with well named variables and functions that reduce the need for as much commenting. If you suffer from code like this in your workplace, perhaps actively comment it as you reach an understanding of what it does."
Relatively experienced 6 year C++ dev - I mainly mentioned university because things where much stricter - but having an environment with decent code reviews and maintaining a little self discipline goes a long way with the sanity of your co-workers.
6 years is significant, but it's not a long time. There's much to learn, particularly about the cultural aspects of various segments of the industry.
This is excellent code that was produced on a tight timeline.
Real-world work stresses getting stuff done. And in this case parts of the code can also take a life of their own.
Ideally both ideologies meet somewhere in between and you get code that works and is maintainable. But sometimes you can get bad code that is shipped... or an unhealthy obsession with polished code that never ships. One is infuriating, the other makes businesses slowly unfeasible.
This is usually the result of years of changing/updating the code. Any living code base tends to grow and accumulates cruft over time. Just because it doesn't fit the ideal you learned in university doesn't mean it is bad code, this is real life code.
There is no excuse for not writing well commented code - or even code with well named variables and functions that reduce the need for as much commenting.
If you suffer from code like this in your workplace, perhaps actively comment it as you reach an understanding of what it does.
Frequently changed parts of the code were a hideous mess of 90% comments to 10% code. And then occasionally we'd just rename an entire file by prepending 'old' to it and starting fresh with a new file.
That place was such a learning experience on the concept of technical debt. But at least I got a fun talking point for my resume. "x++, is that a typo?" "No, sir..."