Here's my implementation of function according to The Power of 10;
"Restrict functions to a single printed page."
As bonus, I didn't use global variable if possible. So if a function or procedure need a variable from others, it have to be passed using parameter on that function.
If we look at the code below, we know that it can be rewritten using a long single procedure or function. But according The Power of Ten, a function should be as short as possible so it could be printed in a single page.
So, instead one long multiple page function, I write/break it as several short-single-printed-page functions. :)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
Edit4: TEdit;
procedure proses;
function konversi(a:real;c,d:char):string;
function konversiC(a:real;d:char):string;
function konversiF(a:real;d:char):string;
function konversiR(a:real;d:char):string;
function konversiK(a:real;d:char):string;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
proses;
end;
procedure tform1.proses;
var
a:real;
s,b:string;
c,d:char;
begin
a:=strToFloat(edit1.Text);
s:=edit2.Text;
c:=s[1];
s:=edit4.Text;
d:=s[1];
b:=konversi(a,c,d);
edit3.Text:=b;
end;
function tform1.konversi(a:real;c,d:char):string;
begin
case c of
'C':konversi:=konversiC(a,d);
'F':konversi:=konversiF(a,d)
'R':konversi:=konversiR(a,d)
'K':konversi:=konversiK(a,d)
else konversi:='error';
end;
end;
function tform1.konversiC(a:real;d:char):string;
begin
case d of
'C':konversiC:=floatToStr(a);
'F':konversiC:=floatToStr(a*9/5+32);
'R':konversiC:=floatToStr(a*4/5);
'K':konversiC:=floatToStr(a+273);
else konversiC:='Error';
end;
end;
function tform1.konversiF(a:real;d:char):string;
begin
case d of
'C':konversiF:=floatToStr((a-32)*5/9);
'F':konversiF:=floatToStr(a);
'R':konversiF:=floatToStr((a-32)*4/9);
'K':konversiF:=floatToStr((a-32)*5/9+273);
else konversiF:='Error';
end;
end;
function tform1.konversiR(a:real;d:char):string;
begin
case d of
'C':konversiR:=floatToStr(a*5/4);
'F':konversiR:=floatToStr(a*9/4+32);
'R':konversiR:=floatToStr(a);
'K':konversiR:=floatToStr(a*5/4+273);
else konversiR:='Error';
end;
end;
function tform1.konversiK(a:real;d:char):string;
begin
case d of
'C':konversiK:=floatToStr(a-273);
'F':konversiK:=floatToStr((a-273)*9/5+32);
'R':konversiK:=floatToStr((a-273)*4/5);
'K':konversiK:=floatToStr(a);
else konversiK:='Error';
end;
end;
end.