4 ms·
Nothing's really jumped out to me yet, but tools need to raise flags about the complexity of code. A pre-commit hook that recognizes long functions, classes wi
by lucidguppy2000 11y ago
Nothing's really jumped out to me yet, but tools need to raise flags about the complexity of code. A pre-commit hook that recognizes long functions, classes with more than ten member variables, classes that program to an implementation rather than interface? Could it check to see that classes that can be easily unit-tested are?
The world expects programmers to make clean code, but we've not really advanced our tools much outside of Java and C#. I'm always envious of what the refactoring tools can do in those languages.
- xorcist 11y agoI never understood many people's fixation with avoiding long functions/methods. I would much rather have that hook check long call stacks. I have never went "wtf" as I dove into a new code base because of long functions, but I do it all the time when five levels of inheritance is used to provide all the magic necessary to "shield" me from whatever the code is actually doing.
- Kevguy 11y agoI definitely _have_ gone "wtf" when dealing with some long functions. A 380 line switch/case comes to mind. I have also had to deal with the 5 level deep inheritance that was equally difficult in its own way. Figuring out where to balance between length and depth is one of the more difficult parts of programming in my opinion. After many years maintaining my and other people's code, I do tend toward shorter methods. On the other hand, splitting up a method into several methods just to keep under a hard line limit causes more problems than it solves.
- xorcist 11y agoSure, of course I prefer shorter code. But I'd much rather read a hundred straightforward lines than wrap my head around yet another abstraction (of which there are thousands in any non trivial codebase). I must disagree on the switch statement. A main loop with an rather large switch statement is a perfectly normal way to write a trivial interpreter. C provides a nice way of doing it, and as long as you declare your constants properly it's all perfectly readable. A lot of excellent code bases are structured that way. (Awk comes to mind, IIRC, and it's very easy to read and extend.)
- lucidguppy2000 11y agoKeep in mind - a function shouldn't contain more than one level of abstraction really. Having a 50+ line function, the code is telling you that you have a class in there struggling to get out. Also - you may be superior to others and able to understand a 50 line function. But what about your colleagues? The average cognitive limits of are well known. I doubt we will evolve to beyond these flaws in my lifetime.
- nitrogen 11y agoCode Climate is a service that does this for Ruby code. I'd much prefer something running locally, though.
- bazzargh 11y agomost of codeclimate's metrics come from things you can run locally (eg they blog about this here http://blog.codeclimate.com/blog/2013/08/07/deciphering-ruby-code-metrics/ http://blog.codeclimate.com/blog/2013/08/07/deciphering-ruby...): http://ruby.sadi.st/Flog.html http://ruby.sadi.st/Flog.html http://ruby.sadi.st/Flay.html http://ruby.sadi.st/Flay.html http://brakemanscanner.org/ http://brakemanscanner.org/ What makes codeclimate useful is the polish/ease of use on top of these tools. It also makes it easier for me to get projects to adopt new tools - if they're in CC already, and CC start doing bundler-audit reports...we get them for all projects.
- ajuc 11y agoThe things that cause real problems aren't local (like long functions, big classes, etc). They are global, like hidden assumptions about the stuff you interface with. Requirements regarding order of invocations of some methods of seemingly unrelated classes. Code that shouldn't really be needed, but stuff breaks when you remove it. Esoteric automaticaly generated code driven by xmls or .xlsx, that nobody really understand, but copy-paste when you need another special-case.