Let's say I have method A.M1.
It is called in B.M2 2 times and in B.M3 3 times.
I need to find all number of usages (like Resharper does through find usages interface). So the sum for method calls would be 5.
Following Query:
from m in Methods
where m.HasAttribute("specific attribute")
orderby m.MethodsCallingMe.Count() descending
select new { m, m.MethodsCallingMe }
gives me only 2 usages for method A.M1.
Could you please help me to find the usages number?
It's difficult or impossible to do with NDepend. NDepends shows which methods are calling specified method and not count in this situation them and even not count them recursively or through call graph.
So try to use other techniques. By first of all, why you need such metric? Some kind of method rank?
Anyway a pretty simple way to achieve it, is using RegEx experssion. You can use also NDepend API or regex and ndepend api together. For example, get all method names from source code base using nDepend API and then using regex for each method name count matches except method definition itself.
Related
Let's say I have an articles table with thousands of articles. I am trying to figure out the most performant way to find the 5 longest article names, not the articles themselves.
This is what I have so far:
def self.five_longest_article_names
limit(5).order("(LENGTH(name)) desc").pluck(:name)
end
But that uses SQL. Is there an ActiveRecord query that I could have performed instead?
YourModel.limit(5).order("CHAR_LENGTH(name) desc").pluck(:name)
So, your method becomes:
def self.five_longest_article_names
limit(5).order("CHAR_LENGTH(name) desc").pluck(:name)
end
You could defintely use Arel, but Arels are good for complex queries. In your case, this is a simple usecase, and I think it's better to use SQL in this particular case.
You can always create your own name function for arel:
my_length = Arel::Nodes::NamedFunction.new('LENGTH', YourModel.table[:name])
YourModel.order(Arel::Nodes::Descending.new(my_length)).limit(5)
# or
YourModel.order("#{my_length.to_sql} DESC").limit(5)
I'd suggest you pass it as a named scope to your model and concatenate calls from there.
I have a map of errors and the number of their occurrences in some situation. I want to have a list, which contains that information in order of descending number of errors (i. e. the most frequent error is first element, the second-most - the second etc.).
I wrote following code:
List<Map.Entry<String,Integer>> entryList = new ArrayList<>(errors.entrySet());
entryList.sort{a, b -> b.value <=> a.value}
It works fine, but the sort is striked through (in Eclipse IDE with the Groovy plugin).
Why? Is that method deperecated? If so, what is the correct way to sort a list in Groovy?
Use the Iterable version of sort instead
Because of deprecation - have a look at the docs. Some of sort methods are deprecated in favor of others.
Let's say we have a Ruby method like this:
# Pseudocode
def get(globbed)
a_items = Dir.glob(globbed)
a_items.length == 1 ? a_items.first : a_items
end
The method is meant to return a String containing information about the items in question. If there are many items, it will return an Array. The ternary makes it so that if there is only one item, it just returns that String.
What is the best practice here? Should such a method always return an Array even if there is only one item?
It should always return an array. Returning different things means that whatever method that calls this method would also have to have a condition. That is not good. Whenever you can get rid of a condition, you should. A condition should only be used as a last resort.
As a real example, the jQuery library built on top of JavaScript has the notion of selectors, expressed in the form $(...). This can result in multiple matching dom objects, or a single one. But jQuery always returns an array even if the matched dom object is one. That makes things simple.
It's always about use cases. You have to define what's the responsibility of that method and then decide what makes sense to do.
In this specific case, I would say that, unless there isn't any specific reason to return different types, you should choose the way that is simpler, both to test and to read.
Always returning an array in this case means clearer method interface:
"The method returns an array with the directory content"
instead of the more convoluted
"The method returns an array of directory content if there more than
one object, otherwise return the single object."
So, clarity first of all.
And: testing would result easier. The cyclomatic complexity of the routine is less.
There are cases where the uniformity of return types can't be fulfilled. Just think of the Array method index: it wouldn't be possible to distinguish between "object not found" and "index 0" if the practice here was applied.
Conclusion: here I don't see any reason why to make the method more complex by distinguishing the two cases, so.. KISS.
Hi, ruby provides block, yield and iterator to permit easy array or hash treatment. And it's a good practice to use the same code for one or several numbers of element. Exemple :
a_items.each { |element| file_treatment(element) }
Regards.
Whenever I see Ruby code that says:
arrayNames.collect { ... }
I forget what collect is and have to look up what it is, and find that it is the same as map().
Map, I can understand, mapping 1 byte to a pixel, and function is to map an x to a y, a 2 to a 4, a 5 to a 25, etc. But where does the name "collect" come from? Maybe that will help to remember what a "collect" method is.
It comes from Smalltalk old days. Smalltalk used collect and select instead of map and filter (as used in many other languages) for iterating on its collections.
To add to the other answers, it is kind of an inside-joke in Smalltalk:
inject:into:
collect:
select:
reject:
detect:
Spot the pattern?
kriss is right that the method name has its origins in Smalltalk but to help remember what it does when you see it used you can think of it as "collecting the results from the block in a new array".
I have a method named RenderContent which returns object[]
In my unit test, I need to assert that this array does not contain any objects of type VerifyRequest
At the moment, I'm using the following Assert statement. Is there anything more concise?
Assert.That(
domain.RenderContent().OfType<VerifyRequest>().Count(),
Is.EqualTo(0)
);
I prefer to use fluent syntax. Note also that RenderContent returns object[], not IQueryable<object>.
If you are using NUnit 2.5, you could use something like:
Assert.That(domain.RenderContent(), Has.None.InstanceOf<VerifyRequest>());
But I'm not sure if other unit test frameworks support this assert-style.
Although I don't know the exact NUnit syntax for IsFalse assertion, the best fit for this kind of test is the Any extension method:
Assert.IsFalse(domain.RenderContent().OfType<VerifyRequest>().Any());
It might be tempting to use the Count method, but Any is more efficient, as it will break on the first occurrence.
The Any extension method, which can be given a lambda expression:
Assert.IsFalse(domain.RenderContent().Any(i => i is VerifyRequest));
You could shorten it a tad by using the Assert.AreEqual method instead:
Assert.AreEqual(domain.RenderContent().OfType<VerifyRequest>().Count(), 0);
I prefer the Assert.AreEqual approach; NUNit uses Assert.That for the internal Assert, STringAssert, etc objects. I like just doing Assert.AreEqual(0, domain.RenderContent().OfType().Count()); to check for the counts.
This way, it checks directly that no objects of a type have any number of records, but to a point the variations you see are preference and they all are equally valid. You have to choose what you like for your style of development.