What is your preferred way to implement actions based on the invalidity of multiple variables:
ie:
invalid_get() {
return a_invalid || b_invalid || c_invalid;
}
a_invalid_set(v) {
a_invalid=v;
if(v) {
validate_add();
} else {
validate_remove();
}
}
function validate_remove() {
if(!invalid_get()) {
validate_remove_do();
}
}
OR:
var invalids_num:Int;
function a_invalid_set(v) {
a_invalid=v;
if(v) {
++invalids_num;
validate_add();
} else {
--invalids_num;
validate_remove();
}
}
validate_remove() {
if(invalids_num==0) {
validate_remove_do();
}
}
I am guessing that a int check against 0 is faster and is without question the correct pattern, certainly for a large number of properties.
Related
I have some old ugly code like this
if(!isLogin)
{
if(confirm("Login first ?"))
{
doLogin();
return;
}
else
{
doStuff1();
doStuff2();
doStuff3();
}
}
else
{
doStuff1();
doStuff2();
doStuff3();
}
For refactoring, here's what I did
if(!isLogin && confirm("Login first ?"))
{
doLogin();
}
else
{
doStuff1();
doStuff2();
doStuff3();
}
I'm not sure... is new code logic equals old part, and possible to make it shorter ?
You need to add return; after doLogin(); to be the same. You could drop the else { and } too, and unindent the three doStuff?() functions:
if (!isLogin && confirm("Login first ?"))
{
doLogin();
return;
}
doStuff1();
doStuff2();
doStuff3();
I'm showing an array with items of type 'request' in a table. I want to sort the columns of the table so I planned to make a click method for every column header. This methods sorts the array based on the value of the property shown in that column.
public sortProduct(): void {
this.requests.sort((a, b) => {
if (a.productName < b.productName)
return -1;
if (a.productName > b.productName)
return 1;
return 0;
});
if (!this.productSortOrder) {
this.requests.reverse();
this.productSortOrder = true;
} else {
this.productSortOrder = false;
}
}
This works, but now I need to make a method for every column. I am looking for a way to call a sort method like this:
this.requests.sortMethod(property, order);
This method would then sort the requests array based on the property of the objects in the array and in the given sortorder.
How can I do that? I guess I'm looking for something like Func<> in C#.
You can us a function signature for a similar effect to Func
sortProduct<T>(prop: (c: Product) => T, order: "ASC" | "DESC"): void {
this.requests.sort((a, b) => {
if (prop(a) < prop(b))
return -1;
if (prop(a) > prop(b))
return 1;
return 0;
});
if (order === "DESC") {
this.requests.reverse();
this.productSortOrder = true;
} else {
this.productSortOrder = false;
}
}
// Usage
sortProduct(p=> p.productName, "ASC");
Or you can use the property name instead (keyof Product will ensure the string must be a property of Product):
sortProduct<T>(propName: keyof Product, order: "ASC" | "DESC"): void {
this.requests.sort((a, b) => {
if (a[propName] < b[propName])
return -1;
if (a[propName] > b[propName])
return 1;
return 0;
});
...
}
// Usage
sortProduct("productName", "ASC");
sortProduct("productName_", "ASC"); // Error
You can use a SortUtil class with a static template method sortByProperty:
export class SortUtil {
static sortByProperty<T>(array: T[], propName: keyof T, order: 'ASC' | 'DESC'): void {
array.sort((a, b) => {
if (a[propName] < b[propName]) {
return -1;
}
if (a[propName] > b[propName]) {
return 1;
}
return 0;
});
if (order === 'DESC') {
array.reverse();
}
}
}
I have a RadPageview in Strip Mode and I want my program to had support a right-to-left language. When I changing the Right-to-Left Property to true it works fine. but when I run my program and use Arrow Keys (Left or Right) it doesn't work correctly. Left key goes to Right and Right key goes to Left. How can I fix it?
Telerik answered my question:
http://www.telerik.com/forums/right-to-left-arrow-keys-problem
the possible solution that I can suggest is to create a custom RadPageViewStripElement and override its IsNextKey and IsPreviousKey methods on a way to reverse the previous/next navigation as follows:
public class CustomPageView : RadPageView
{
public override string ThemeClassName
{
get
{
return typeof(RadPageView).FullName;
}
}
protected override RadPageViewElement CreateUI()
{
switch (this.ViewMode)
{
case PageViewMode.Strip:
return new CustomRadPageViewStripElement();
default:
return base.CreateUI();
}
}
}
public class CustomRadPageViewStripElement : RadPageViewStripElement
{
public CustomRadPageViewStripElement()
{
}
protected override Type ThemeEffectiveType
{
get
{
return typeof(RadPageViewStripElement);
}
}
protected override bool IsNextKey(Keys key)
{
if (this.RightToLeft)
{
if (key == Keys.Left)
{
return true;
}
else
{
return false;
}
}
return base.IsNextKey(key);
}
protected override bool IsPreviousKey(Keys key)
{
if (this.RightToLeft)
{
if (key == Keys.Right)
{
return true;
}
else
{
return false;
}
}
return base.IsPreviousKey(key);
}
}
I fix it programmatically by using ProcessCmdKey of form.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Right))
{
int x = radPageView1.Pages.IndexOf(this.radPageView1.SelectedPage) - 1;
if (x < 0)
x = radPageView1.Pages.Count() - 1;
else if (x >= radPageView1.Pages.Count())
x = 0;
radPageView1.SelectedPage = radPageView1.Pages[x];
return true;
}
else if(keyData == Keys.Left)
{
int x = radPageView1.Pages.IndexOf(this.radPageView1.SelectedPage) + 1;
if (x <= 0)
x = radPageView1.Pages.Count() - 1;
else if (x >= radPageView1.Pages.Count())
x = 0;
radPageView1.SelectedPage = radPageView1.Pages[x];
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
PHP offers useful magic constants like:
__CLASS__
__FILE__
__METHOD__
and so on. Also the
get_class()
function provides a similar functionality.
Is there anything similar in Dart?
Compiler constants similar to PHP not available. But you can do this manually (not constant value).
This slower but it works.
import 'package:stack_trace/stack_trace.dart';
void main() {
print(__LINE__);
print(__METHOD__);
print(__FILE__);
new Foo();
}
class Foo {
Foo() {
print(__CLASS__);
}
}
String get __CLASS__ {
var frames = new Trace.current().frames;
if(frames.length > 1) {
var member = frames[1].member;
var parts = member.split(".");
if(parts.length > 1) {
return parts[1];
}
}
return null;
}
String get __METHOD__ {
var frames = new Trace.current().frames;
if(frames.length > 1) {
return frames[1].member;
}
return null;
}
String get __FILE__ {
var frames = new Trace.current().frames;
if(frames.length > 1) {
return frames[1].uri.path;
}
return null;
}
int get __LINE__ {
var frames = new Trace.current().frames;
if(frames.length > 1) {
return frames[1].line;
}
return null;
}
4
main
/home/andrew/dart/for_web/test/bin/test.dart
Foo
When attempting to validate a journal I use the LedgerJournalEngine ErrorExists for each voucher in the journal. For some reason it doesn't catch all errors in the code but if I use the validate button in the client the errors are in the info log.
Is there a better way to validate a voucher in a journal?
changecompany(ledgerJournalTable.dataAreaId)
{
ledgerJournalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes,NoYes::No);
lje = LedgerJournalEngine::construct(ledgerJournalTable.JournalType);
lje.newJournalActive(ledgerJournalTable,true);
ledgerJournalCheckPost.parmLedgerJournalEngine(lje);
try
{
ledgerJournalCheckPost.run();
}
catch
{
ledgerJournalCheckPost.validate();
while select ledgerJournalTrans where ledgerJournalTrans.JournalNum == ledgerJournalTable.JournalNum
{
if(lje.errorExists(ledgerJournalTrans.Voucher))
{
errors.addError(lje.errorLog(ledgerJournalTrans.Voucher),ledgerJournalTrans.RecId);
}
}
}
}
So this is what I have come up with, so far it seems to be working as expected. If anyone has a better way please let me know.
changecompany(ledgerJournalTable.dataAreaId)
{
ledgerJournalCheckPost = LedgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::No);
lje = LedgerJournalEngine::construct(ledgerJournalTable.JournalType);
lje.newJournalActive(ledgerJournalTable,true);
ledgerJournalCheckPost.parmLedgerJournalEngine(lje);
try
{
ledgerJournalCheckPost.run();
while select ledgerJournalTrans where ledgerJournalTrans.JournalNum == ledgerJournalTable.JournalNum
{
if(lje.errorInJournal() || ledgerJournalCheckPost.numOfErrorsInList()>0)
{
errors.addError(lje.errorLog(ledgerJournalTrans.Voucher),ledgerJournalTrans.RecId);
}
}
ledgerJournalCheckPost.parmPost(NoYes::Yes);
ledgerJournalCheckPost.run();
}
catch
{
ledgerJournalCheckPost.validate();
while select ledgerJournalTrans where ledgerJournalTrans.JournalNum == ledgerJournalTable.JournalNum
{
if(lje.errorInJournal() || ledgerJournalCheckPost.numOfErrorsInList()>0)
{
errors.addError(lje.errorLog(ledgerJournalTrans.Voucher),ledgerJournalTrans.RecId);
}
}
}
ledgerJournalCheckPost = null;
lje = null;
ledgerJournalTrans = null;
ledgerJOurnalTable = null;
}
return errors;