I would like to know what kind of check ,using checkstyle, i could use to avoid complex conditions.
For example:
What i want to avoid
if(NumberHelper.equalsDouble(this.Detail.getQty(), this.qty))
{
...
}
I want the condition to be encapsulated.
if(isDetailFullyCancelled())
{
...
}
private boolean isDetailFullyCancelled()
{
return NumberHelper.equalsDouble(this.Detail.getQty(), this.qty);
}
Thank you :)
Related
I want this.method2() call of base to execute base.method2() while it actually executes derived.method2() (I understand the idea behind the behavior), is it possible to achieve this without methods renaming and what is the best practice here?
const base = class {
method() {
this.method2();
}
method2() {
console.error("base.method2");
}
}
const derived = new class extends base {
method() {
super.method();
}
method2() {
console.error("derived.method2");
}
}
derived.method(); // want 'base.method2' here
You can do this with call.
method() {
base.prototype.method2.call(this);
}
In this case you don't technically need to use call and supply the this value, because you don't use it, but it is the best way to create the call you want so it works in all circumstances.
Incidentally, I don't understand what you are seeking to achieve with the const base = class {} statement. A class declaration would be much less surprising: class base {}.
Hi I want to test duplication by adding same fixture more than twice. It could be the code below:
#Test(expected=DuplicationException.class)
public void saveFailedWithDuplicatedAccount(){
memberServiceImpl.save(member);
memberServiceImpl.save(member);
}
but I don't know how to deal with Mockito coding - like using when(), verify(). Since I am new to mockito, and I have got nothing found in the Google, so is there any example code to check duplicating addition?
You need to save state somewhere.
It may be some kind of internal storage or real database.
And you can extract logic for searching duplicates and mock that.
For example:
Test(expected = DuplicationException.class)
public void saveFailedWithDuplicatedAccount() {
DuplicateService duplicateServiceMock = Mockito.mock(DuplicateService.class);
memberServiceImpl.setDuplicateService(duplicateServiceMock);
memberServiceImpl.save(member);
Mockito.when(duplicateServiceMock.isDuplicate(member)).thenReturn(true);
memberServiceImpl.save(member);
}
public class DuplicateAccountService {
public boolean isDuplicateAccount(String login) {
return false; // Some logic for find duplicates
}
}
I have a class with a method that calls other methods:
class MyClass {
build(methods) {
methods.forEach((method) => {
if (this.method) {
this.method();
}
});
}
stuff() {}
moreStuff() {}
}
const a = MyClass();
a.build(['stuff', 'moreStuff']);
I haven't been able to find any reference on any special methods for classes. My first thought was to use hasOwnProperty (however eslint nags me that I shouldn't use it within the class). The approach above wouldn't work reliably as classes have built-in functions.
I was also looking at Reflect as possibly being my saving grace, but I could really use some guidance on what is the best practice for this scenario?
I think you're looking for
build (methodnames) {
for (const methodname of methodnames) {
if (typeof this[methodname] == "function") {
this[methodname]();
}
}
}
There's nothing special about classes - and in fact you should ignore them. If you want to call some method, the only thing that is important is that there is a function as a property value. It doesn't matter whether the method is an own property of the prototype object of the class that created the instance.
I am creating a method that accepts a IOBJECT parameter. there is multiple class that implement this interface. I need to figure out which type IOBJECT is. how would i go about doing that
You can use typeof, instanceof, or the 'is' operator
It's not ideal, but you can use the "is" operator. Throw it into a switch of if else statment to figure things out.
if(obj is ClassA) {
//sweetness
} else if (obj is ClassB) {
//awesomeness
}
typeof will not work, as suggested in the other reply. It will likely return "object" in all cases. instanceof will work though.
You can do getQualifiedClassName() to get the class name of the object. You can also use describeType() which gives you a more complete description of all the methods and properties of the object.
There is information about both here:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html
It doesn't sound like an ideal situation though. You may want to do something where you can standardize the way you handle all items. For example:
public interface IObject {
function doSomething():void;
}
Then...
function myMethod(obj:IObject):void {
obj.doSomething();
}
I have the following code:
public IQueryable<ITax> FindAllTaxes()
{
return db.Taxes;
}
I am getting the following error
Cannot implicitly convert type 'System.Data.Linq.Table<Models.Tax>' to 'System.Linq.IQueryable<Interfaces.ITax>'
I am trying to use Interface where ever I go, but not sure how to convert this, any help?
We don't have covariance of generic types yet, I'm afraid. You can do IQueryable<Tax>, but not IQueryable<ITax>. You could introduce a conversion, but it'll probably break composability, rendering it useless. You could try it though:
return db.Taxes.Cast<ITax>();
In C# 4.0, this would probably work without the extra cast (although I haven't tried it).
try this
public IQueryable<Taxe> FindAllTaxes() { return db.Taxes; }
or
public IQueryable<Tax> FindAllTaxes() { return db.Taxes; }