我有两个中断服务程序(ISR),基本上完全相同,但每个处理来自不同设备的中断(虽然是相同类型的设备).因此,逻辑是相同的,但它们访问不同的CPU寄存器和存储器位置.
作为一个简单的示例,请考虑以下代码:
extern volatile unsigned int dev1_rx_buffer;
extern volatile unsigned int dev2_rx_buffer;
volatile unsigned char data;
void __attribute__((__interrupt__)) _dev1_interrupt(void)
{
/* Clear interrupt flag */
dev1.IF = 0;
if (dev1.IS_FULL) {
/* Read data from device */
data = dev1_rx_buffer;
} else {
/* do something else using registers of device 1 */
}
/* More stuff using registers of device 1 */
}
void __attribute__((__interrupt__)) _dev2_interrupt(void)
{
/* Clear interrupt flag */
dev2.IF = 0;
if (dev2.IS_FULL) {
/* …Run Code Online (Sandbox Code Playgroud) 我打算在VB6代码之后重构这个代码(由其他人编写).
Public Function GetValue(ID As Long) As Boolean
On Error GoTo eh
'' ... DAL Logic...
eh_Exit:
On Error GoTo 0
Exit Function
eh:
Resume eh_Exit
End Function
Run Code Online (Sandbox Code Playgroud)
您认为原作者的意图是eh什么?
可能只是"呃,发生了什么事?"......
我想让它变得可读,而我不必像现在一样思考它......
private static readonly string dataProvider = ConfigurationManager.AppSettings.Get("Provider");
private static readonly DbProviderFactory factory = DbProviderFactories.GetFactory(dataProvider);
private static readonly string connectionString = ConfigurationManager.ConnectionStrings[dataProvider].ConnectionString;
/// <summary>
/// Executes Update statements in the database.
/// </summary>
/// <param name="sql">Sql statement.</param>
/// <returns>Number of rows affected.</returns>
public static int Update(string sql)
{
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = factory.CreateCommand())
{
command.Connection = connection;
command.CommandText = sql;
connection.Open();
return command.ExecuteNonQuery();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要帮助重写这个,以便它可以使用存储过程.(通过sproc名称和params)有没有人知道我应该怎么做呢?编辑:我遇到问题的区域是试图找出填写参数的方法.
谢谢
这是我的报告控制器中的代码,它看起来很糟糕,有人能给我一些关于如何整理它的建议吗?
# app\controller\reports_controller.rb
@report_lines = []
@sum_wp, @sum_projcted_wp, @sum_il, @sum_projcted_il, @sum_li,@sum_gross_profit ,@sum_opportunities = [0,0,0,0,0,0,0]
date = @start_date
num_of_months.times do
wp,projected_wp, invoice_line,projected_il,line_item, opp = Report.data_of_invoicing_and_delivery_report(@part_or_service,date)
@sum_wp += wp
@sum_projcted_wp +=projected_wp
@sum_il=invoice_line
@sum_projcted_il +=projected_il
@sum_li += line_item
gross_profit = invoice_line - line_item
@sum_gross_profit += gross_profit
@sum_opportunities += opp
@report_lines << [date.strftime("%m/%Y"),wp,projected_wp ,invoice_line,projected_il,line_item,gross_profit,opp]
date = date.next_month
end
Run Code Online (Sandbox Code Playgroud)
我想用一些像这样的方法
@sum_a,@sum_b,@sum_c += [1,2,3]
Run Code Online (Sandbox Code Playgroud) 想象你有以下接口:
public interface IInterfaceA : IInterfaceX
{
//
// declarations
//
}
public interface IInterfaceB : IInterfaceX
{
//
// declarations
//
}
public interface IInterfaceC : IInterfaceX
{
//
// declarations
//
}
Run Code Online (Sandbox Code Playgroud)
现在我想用一个函数替换以下三个执行几乎相同的方法:
class SomeClass
{
IInterfaceA myVarA;
IInterfaceB myVarB;
IInterfaceC myVarC;
void SomeMethodA(IInterfaceX someVarX)
{
myVarA = (IInterfaceA)someVarX;
}
void SomeMethodB(IInterfaceX someVarX)
{
myVarB = (IInterfaceB)someVarX;
}
void SomeMethodC(IInterfaceX someVarX)
{
myVarC = (IInterfaceC)someVarX;
}
}
Run Code Online (Sandbox Code Playgroud)
我想到了类似的东西:
void SomeMethod(IInterfaceX targetVar, IInterfaceX someVarX)
{
//
// here's my …Run Code Online (Sandbox Code Playgroud) 我有一些代码我想重构使用C#迭代器(即IEnumerable).不幸的是,我无法找到最好的方法来允许其他函数使用迭代器而不会导致它重新启动迭代器.
例如:
NewLineEnumerator nle = new NewLineEnumerator();
while (bytesRead > 0)
{
var nlenum = nle.Process(inputData, bytesRead);
foreach (string block in nlenum)
{
DoSomething(nlenum);
}
}
void DoSomething(IEnumerable<string> myiter)
{
foreach (var s in myiter)
{
// myiter is restarted, and begins at the start of the iterator again
}
}
Run Code Online (Sandbox Code Playgroud)
你可能会问我为什么要这样做.原因是我有一个数据流,被"命令块"包围.根据命令,我将其发送到另一个子功能进行处理.所以我想在开始时或结束时继续迭代我在流中停留的位置.
这里有什么建议?
我怎样才能重构这一点,以便numberOfItems不必被声明为变量?
//method: gets the text in a string in front of a marker, if marker is not there, then return empty string
//example: GetTextAfterMarker("documents/jan/letter043.doc","/") returns "letter043.doc"
//example: GetTextAfterMarker("letter043.doc","/") returns ""
//example: GetTextAfterMarker("letter043.doc",".") returns "doc"
public static string GetTextAfterMarker(string line, string marker)
{
int numberOfItems = line.Split(new string[] { marker }, StringSplitOptions.None).Count();
string result = line.Split(new string[] { marker }, StringSplitOptions.None)[numberOfItems-1];
return line.Equals(result) ? string.Empty : result;
}
Run Code Online (Sandbox Code Playgroud) class MainPage(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
tasks_query = Task.all()
tasks = tasks_query.fetch(1000)
if user:
url = users.create_logout_url(self.request.uri)
else:
url = users.create_login_url(self.request.uri)
template_values = {
'tasks': tasks,
'url': url
}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
class Gadget(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
tasks_query = Task.all()
tasks = tasks_query.fetch(1000)
if user:
url = users.create_logout_url(self.request.uri)
else:
url = users.create_login_url(self.request.uri)
template_values = {
'tasks': tasks,
'url': url
}
path = os.path.join(os.path.dirname(__file__), 'gadget.xml')
self.response.out.write(template.render(path, template_values))
Run Code Online (Sandbox Code Playgroud) 我根本不自称是RegEx大师,我对这句话的作用感到有些困惑.我正在尝试重构,这是在按键上调用并吃了很多CPU.
Regex.Replace(_textBox.Text, "(?<!\r)\n", Environment.NewLine);
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在重新设计现有的课程.在这个类中,大约有400行while循环,它完成了大部分工作.循环的主体是if语句,变量赋值的雷区,并且在某个中间有一个"继续".循环的目的很难理解.
在伪代码中,这是我重新设计的地方:
/* Some code here to create the objects based on config parameters */
/* Rather than having if statements scattered through the loop I */
/* create instances of the appropriate classes. The constructors */
/* take a database connection. */
FOR EACH row IN mySourceOfData
int p = batcher.FindOrCreateBatch( row );
int s = supplierBatchEntryCreator.CreateOrUpdate( row, p );
int b = buyerBatchEntryCreator.CreateOrUpdate( row, p );
mySouceOfData.UpdateAsIncludedInBatch( p, s, b);
NEXT
/* Allow things to complete their last …Run Code Online (Sandbox Code Playgroud)