DECLARE
num_of_payments NUMBER(2);
payment_num NUMBER(2);
start_date DATE;
date_due DATE;
monthly_payment_amt NUMBER(8,2);
donation_balance NUMBER(8,2);
pledgeamt NUMBER(8,2);
BEGIN
SELECT PLEDGEAMT, PLEDGEDATE, PAYMONTHS INTO pledgeamt, start_date, num_of_payments
FROM DD_PLEDGE
WHERE IDPLEDGE = &IDPLEDGE;
monthly_payment_amt := pledgeamt / num_of_payments;
date_due := start_date;
donation_balance := (pledgeamt - monthly_payment_amt);
payment_num := 0;
FOR i IN 1..num_of_payments LOOP
payment_num := payment_num +1;
date_due := add_months(date_due,1);
DBMS_OUTPUT.PUT_LINE('Payment number: ' || payment_num || ' Due Date: '
|| date_due || ' Payment Amount: ' || monthly_payment_amt || ' Balance: '
|| to_char(donation_balance,'$9999.99'));
donation_balance := donation_balance - monthly_payment_amt;
EXIT WHEN donation_balance < 0; --LOOP until condition is met
END LOOP;
END;
我只是想知道我的代码格式是否正确还是我应该更改一些内容?它起作用,只是觉得它看起来很乱,而且不干净。
答案 0 :(得分:2)
这个问题可能会因为过于宽泛或基于意见而被封闭,但我想举一个例子。
这真的是品味问题。代码样式差异很大。我将使用我团队中的开发人员坚持的代码样式来格式化您的代码。我并不是说这是完美的,但我发现它非常易读。
DECLARE
num_of_payments NUMBER(2);
payment_num NUMBER(2);
start_date DATE;
date_due DATE;
monthly_payment_amt NUMBER(8,2);
donation_balance NUMBER(8,2);
pledgeamt NUMBER(8,2);
BEGIN
SELECT PLEDGEAMT,
PLEDGEDATE,
PAYMONTHS
INTO pledgeamt,
start_date,
num_of_payments
FROM DD_PLEDGE
WHERE IDPLEDGE = &IDPLEDGE;
monthly_payment_amt := pledgeamt / num_of_payments;
date_due := start_date;
donation_balance := (pledgeamt - monthly_payment_amt);
payment_num := 0;
FOR i IN 1..num_of_payments
LOOP
payment_num := payment_num + 1;
date_due := add_months(date_due, 1);
DBMS_OUTPUT.PUT_LINE('Payment number: ' || payment_num || ' Due Date: '
|| date_due || ' Payment Amount: ' || monthly_payment_amt || ' Balance: '
|| to_char(donation_balance,'$9999.99'));
donation_balance := donation_balance - monthly_payment_amt;
EXIT WHEN donation_balance < 0; --LOOP until condition is met
END LOOP;
END;
/
总有一个命名约定,关于case和其他约束的规则,但它是一个好的开始。