As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Besides Cyclomatic Complexity what other code metric systems exist?
Code metrics
Do you find cyclomatic complexity a useful measure?
What code metric(s) convince you that provided code is “crappy”?
Wikipedia provides a
simple overview of metrics.
There are actually lots, and lots of metrics. In fact, any function of the source code that maps source code to a measurement scale (integers, reals, classification enums) can be considered to be a metric.
The problem with most code metrics is that they tend to be proportional to SLOC, and if that's the case, SLOC is just as good. What is best to do with metrics is to measure your code now, and track how the metric changes over time; the trend, up or down, will tell you almost more than the metric will itself. Up means bad news; the code is getting more complicated.
It is also useful to consider code (complexity) metrics in conjunction with bug rates. A high complexity in a module, and and high bug rate in the same module, suggests that a redesign of that module may be a good idea to prevent further troubles in the field. High complexity and low bug rates would suggest the code looks scary but isn't.
Halstead's Software Science is a fantastic metrics suite that can give you interesting insight into the actual constructs of your program.
There is a model created by Card and Glass as well, but I'm not sure if that is available outside of books. I would look for it regardless.
Have a look at the 82 code metrics definitions supported by the tool NDepend integrated in Visual Studio 2012, 2010 and 2008. Disclaimer: I am one of the developers of the tool
Notice that these code metrics can be composed through CQLinq queries and rules to define your own custom code metrics.
For example, one popular code metric other than Cyclomatic Complexity is the C.R.A.P metric. Basically, the C.R.A.P define crappy code as complex methods (with high Cyclomatic Complexity), poorly covered by tests. The default CQLinq rule to define the C.R.A.P metric is:
// <Name>C.R.A.P method code metric</Name>
// Change Risk Analyzer and Predictor (i.e. CRAP) code metric
// This code metric helps in pinpointing overly complex and untested code.
// Reference: http://www.artima.com/weblogs/viewpost.jsp?thread=215899
// Formula: CRAP(m) = comp(m)^2 * (1 – cov(m)/100)^3 + comp(m)
warnif count > 0
from m in JustMyCode.Methods
// Don't match too short methods
where m.NbLinesOfCode > 10
let CC = m.CyclomaticComplexity
let uncov = (100 - m.PercentageCoverage) / 100f
let CRAP = (CC * CC * uncov * uncov * uncov) + CC
where CRAP != null && CRAP > 30
orderby CRAP descending, m.NbLinesOfCode descending
select new { m, CRAP, CC, uncoveredPercentage = uncov*100, m.NbLinesOfCode }
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
This question has some good responses, but has been moved to a more appropriate forum at this link.
Online systems, such as ALEKS, Cengage's WebAssign, and even Khan Academy employ some kind of logical matching for polynomial expressions and numerical values (ie, fractions). What unpaid tools (libraries, command line programs, scripts, etc) exist that can provide expression/numerical matching? For example, a student enters the expression
but the following expression is equivalent and would also be acceptable:
The question about how to do this mathematically has an excellent answer in this post, and a question addressing one particular way to implement this has a partial answer in this post. Sympy looks promising, but the command line Maxima could work, and so could the WolframAlpha API, Maple, MatLab, and any number of symbolic computer algebra systems.
It's fine to talk about things that "could work", but what tools are already being used? How has this already been implemented? Can anyone speak from experience about what online math learning programs are using on the backend? Give examples or direct to existing projects.
To clarify the question, I'm talking about logically comparing simple expressions(middle/high school math), minimally complicated, with canonical forms typically easy to obtain. The implementation will be online (html+nifty_tool) and input will most likely be captured as a string unless someone can suggest a better input method for math learners - a LaTeX front-end perhaps?
Providing that you could translate the student's input into Python it would be easy enough to verify the equality of expressions in most cases. For instance,
>>> from sympy import *
>>> var('p')
p
>>> f_1 = 2*p**2*(p+5)-8
>>> f_2 = 2*(p**2+4*p-4)*(p+1)
>>> f_1.expand()==f_2.expand()
True
If you have an input widget that enables a student to enter expressions of the kind displayed in your question, and that outputs LaTeX, say, then you might be able to use a parser such as https://github.com/alvinwan/tex2py to get the inputs you need for sympy.
Take a look at STACK, which is an automated system for assessing students' math answers. STACK is based on Maxima. The main web site appears to be: http://www.stack.ed.ac.uk/
I found some other links that could be interesting to you:
Moodle plugin for STACK: https://moodle.org/plugins/qtype_stack
Resources for the Moodle plugin at Github: https://github.com/maths/moodle-qtype_stack
Some description about how STACK uses Maxima: https://github.com/maths/moodle-qtype_stack/blob/master/doc/en/CAS/Maxima.md
I'm actually not sure how STACK makes use of Maxima to determine whether an answer is correct. If the form of the answer doesn't matter, then ratsimp(answer - expected) should be 0 if answer is equivalent to expected. But if the form of the answer must be verified as well, the comparison becomes more complicated. I can imagine some ways to do that, but I don't know what STACK actually does.
I see the issues forum for the Github project (https://github.com/maths/moodle-qtype_stack/issues) seems to have a fair amount of traffic, so perhaps if you run into problems you can ask for help there.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I watched a talk in live where the person said they at work are doing Scala functionally, they use case classes, transform a type to another, immutability everywhere, etc.
How does this actually work? I'd love to see a simple hello world application with pure functional approach.
Also I can't see how can I get rid of var altogether because sometimes I just need it.
There is a course on Coursera just about that.
"Hello world" is not really good to demonstrate the functional approach, as there is nothing functional about it. (In fact, as #delnan noted, writing to the standard output is considered a side effect, so this program can never be made purely functional.)
The most likely reason for you to need var is using imperative style loops, which is indeed not the functional approach. The functional equivalent would be to either use some of the available filter / transform functions on a collection, or use recursion.
Simple example: finding all strings in a list which start with 'F'. Imperative style (Java):
List<String> result = new ArrayList<String>();
for (String s : strings) {
if (s.startsWith("F")
result.add(s);
}
Imperativeness would be even more pronounced with the old style iterator loop.
In contrast, functional style could be something like:
val result = strings.filter(_.startsWith("F"))
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Thinking of a web service entirely built on top of MongoDB, while I am pretty confortable with PyMongo, I would like to know if you guys have any positive or negative experiences/stories about either of these ODMs: MongoKit, MongoEngine and MongoAlchemy, the latter having a Flask specific package "Flask-mongoalchemy".
I use MongoEngine with flask no problems. We've written (collected resources) which include wtform support and flask-debugger support as well:
https://github.com/MongoEngine/flask-mongoengine/
I don't really have any real experience or story to offer, but i played with both MongoKit and MongoAlchemy, and i personally decided to try MongoAlchemy, because i like the syntax a little better (probably due to my Django heritage).
MongoKit:
class BlogPost(Document):
structure = {
'title':unicode,
'body':unicode,
'author':unicode,
'date_creation':datetime.datetime,
'rank':int
}
MongoAlchemy:
class BloodDonor(Document):
first_name = StringField()
last_name = StringField()
age = IntField(min_value=0)
gender = EnumField(StringField(), 'male', 'female')
blood_type = EnumField(StringField(), 'O+','A+','B+','AB+',)
Both will help you to validate your data, will let you impose something like a schema (only on application level), and will save you some typing (specifically brackets).
MongoKit is more complete. I chose MongoAlchemy because I didn't want to type structure = {} all the time, and specifying your db and collection using con.test.example.BlogPost() just felt wrong (although you don't have to do it this way).
Try both, and choose the one which works better for you.
As you already mentioned, there is a Flask-MongoAlchemy extension, which works great.
If you want to use MongoKit, the excellent Flask documentation will get you going in no time:
http://flask.pocoo.org/docs/patterns/mongokit/
The great thing is that you just can try one, if you don't like it you can switch to another, or drop to pymongo without having to change anything in the database.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I can say that I really love Scala but now I would like to know the features you cannot live without when working with Scala? What about Scala 2.8?
If I had to go back to Java here's what I'd miss the most: closures, higher order functions, pattern matching, case classes, and the emphasis on immutability.
I've been on 2.8 for a while. If I had to go back to 2.7, the main thing I'd miss is the consistency, cleanness, and richness of the 2.8 collections API. It's waaaaaay better the 2.7 stuff. But I'd also really miss named and default arguments.
Type inference saves so much pointless typing. map and foreach and the like on collections are great, especially in combination with default-lazy iterators and really easy function syntax.
But, as someone who does a lot of scientific computing, what I'd actually miss the most is being able to write high-performance code, wrap it in efficient classes, and then use maps and mathematical operators (+, *, whatever) to manipulate those high-level constructs the way that I actually think about them.
As for 2.8 vs. 2.7--the improvement is pretty incremental from my perspective. It's a little better in many areas; there's little to point to and say, "Oh wow, that!". I expect the new specialized annotation will help me a lot, but I haven't seen it fully in action in the library yet, so I'm withholding judgment.
I enjoy writing in Scala. That's the #1 feature in my book :)
I can just get on with what I want instead of dancing through Java's hoops:
val/var means I don't have to write the type twice
Closures mean I don't have to write lots of anonymous interfaces and can re-use lots more code
Named parameters mean I don't have to remember the position of each argument - great for both reading and writing
Case classes mean I get toString and equals for free... makes debugging far easier!
A decent API for collections (e.g. map, fold) mean that I can say what I want to do instead of dancing the iteration dance
As for 2.8 vs 2.7... I've only ever really spent quality time with 2.8 ;-)
I think it is not a feature but the conciseness which Scala achieves is what I like the most.
This is of course only possible because of type inference, closures, a great type system etc.
I just do not think you can break it down to one or two features. They work together and the result, concise code, is what I would call the killer feature.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I know that there is no right answer to this question, I'm just asking for your opinions.
I know that creating HUGE class files with thousand lines of code is not a good practice since it's hard to maintain and also it usually means that you should probably review your program logic.
In your opinion what is an average line count for a class let's say in Java (i don't know if the choice of language has anything to do with it but just in case...)
Yes, I'd say it does have to do with the language, if only because some languages are more verbose than others.
In general, I use these rules of thumb:
< 300 lines: fine
300 - 500 lines: reasonable
500 - 1000 lines: maybe ok, but plan on refactoring
> 1000 lines: definitely refactor
Of course, it really depends more on the nature and complexity of the code than on LOC, but I've found these reasonable.
In general, number of lines is not the issue - a slightly better metric is number of public methods. But there is no correct figure. For example, a utility string class might correctly have hundreds of methods, whereas a business level class might have only a couple.
If you are interested in LOC, cyclomatic and other complexity measurements, I can strongly recommend Source Monitor from http://www.campwoodsw.com, which is free, works with major languages such as Java & C++, and is all round great.
From Eric Raymond's "The Art Of Unix Programming"
In nonmathematical terms, Hatton's empirical results imply a sweet spot between 200 and 400 logical lines of code that minimizes probable defect density, all other factors (such as programmer skill) being equal. This size is independent of the language being used — an observation which strongly reinforces the advice given elsewhere in this book to program with the most powerful languages and tools you can. Beware of taking these numbers too literally however. Methods for counting lines of code vary considerably according to what the analyst considers a logical line, and other biases (such as whether comments are stripped). Hatton himself suggests as a rule of thumb a 2x conversion between logical and physical lines, suggesting an optimal range of 400–800 physical lines.
Taken from here
Better to measure something like cyclomatic complexity and use that as a gauge. You could even stick it in your build script/ant file/etc.
It's too easy, even with a standardized code format, for lines of code to be disconnected from the real complexity of the class.
Edit: See this question for a list of cyclomatic complexity tools.
I focus on methods and (try to) keep them below 20 lines of code. Class length is in general dictated by the single responsibility principle. But I believe that this is no absolute measure because it depends on the level of abstraction, hence somewhere between 300 and 500 lines I start looking over the code for a new responsibility or abstraction to extract.
Small enough to do only the task it is charged with.
Large enough to do only the task it is charged with.
No more, no less.
In my experience any source file over 1000 text lines I will start wanting to break up. Ideally methods should fit on a single screen, if possible.
Lately I've started to realise that removing unhelpful comments can help greatly with this. I comment far more sparingly now than I did 20 years ago when I first started programming.
The short answer: less than 250 lines.
The shorter answer: Mu.
The longer answer: Is the code readable and concise? Does the class have a single responsibility? Does the code repeat itself?
For me, the issue isn't LOC. What I look at is several factors. First, I check my If-Else-If statements. If a lot of them have the same conditions, or result in similar code being run, I try to refactor that. Then I look at my methods and variables. In any single class, that class should have one primary function and only that function. If it has variables and methods for a different area, consider putting those into their own class. Either way, avoid counting LOC for two reasons:
1) It's a bad metric. If you count LOC you're counting not just long lines, but also lines which are whitespace and used for comments as though they are the same. You can avoid this, but at the same time, you're still counting small lines and long lines equally.
2) It's misleading. Readability isn't purely a function of LOC. A class can be perfectly readable but if you have a LOC count which it violates, you're gonna find yourself working hard to squeeze as many lines out of it as you can. You may even end up making the code LESS readable. If you take the LOC to assign variables and then use them in a method call, it's more readable than calling the assignments of those variables directly in the method call itself. It's better to have 5 lines of readable code than to condense it into 1 line of unreadable code.
Instead, I'd look at depth of code and line length. These are better metrics because they tell you two things. First, the nested depth tells you if you're logic needs to be refactored. If you are looking at If statements or loops nested more than 2 deep, seriously consider refactoring. Consider refactoring if you have more than one level of nesting. Second, if a line is long, it is generally very unreadable. Try separating out that line onto several more readable lines. This might break your LOC limit if you have one, but it does actually improve readability.
line counting == bean counting.
The moment you start employing tools to find out just how many lines of code a certain file or function has, you're screwed, IMHO, because you stopped worrying about managebility of the code and started bureaucratically making rules and placing blame.
Have a look at the file / function, and consider if it is still comfortable to work with, or starts getting unwieldly. If in doubt, call in a co-developer (or, if you are running a one-man-show, some developer unrelated to the project) to have a look, and have a quick chat about it.
It's really just that: a look. Does someone else immediately get the drift of the code, or is it a closed book to the uninitiated? This quick look tells you more about the readability of a piece of code than any line metrics ever devised. It is depending on so many things. Language, problem domain, code structure, working environment, experience. What's OK for one function in one project might be all out of proportion for another.
If you are in a team / project situation, and can't readily agree by this "one quick look" approach, you have a social problem, not a technical one. (Differing quality standards, and possibly a communication failure.) Having rules on file / function lengths is not going to solve your problem. Sitting down and talking about it over a cool drink (or a coffee, depending...) is a better choice.
You're right... there is no answer to this. You cannot put a "best practice" down as a number of lines of code.
However, as a guideline, I often go by what I can see on one page. As soon as a method doesn't fit on one page, I start thinking I'm doing something wrong. As far as the whole class is concerned, if I can't see all the method/property headers on one page then maybe I need to start splitting that out as well.
Again though, there really isn't an answer, some things just have to get big and complex. The fact that you know this is bad and you're thinking about it now, probably means that you'll know when to stop when things get out of hand.
Lines of code is much more about verbosity than any other thing. In the project I'm currently working we have some files with over 1000 LOC. But, if you strip the comments, it will probably remain about 300 or even less. If you change declarations like
int someInt;
int someOtherInt;
to one line, the file will be even shorter.
However, if you're not verbose and you still have a big file, you'll probably need to think about refactoring.