Short Function.

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.


.

via Blogger http://ift.tt/2oRJBT1

Advertisements

Hakuna Matata


[TIMON]

Hakuna Matata!
What a wonderful phrase
[PUMBA]
Hakuna Matata!
Ain’t no passing craze
[TIMON]
It means no worries
For the rest of your days
[TIMON AND PUMBA]
It’s our problem-free philosophy
Hakuna Matata!

[TIMON]
Hakuna Matata!
[YOUNG SIMBA]
[spoken] Hakuna Matata?
[PUMBA]
[spoken] Yeah. It’s our motto!
[YOUNG SIMBA]
[spoken] What’s a motto?
[TIMON]
[spoken] Nothin’! What’s-a-motto with you?!
[PUMBA]
[spoken] Those two words will solve all your problems.
[TIMON]
[spoken] That’s right. Take Pumbaa here
[sung] Why…when he was a young warthog
[PUMBA]
When I was a young warthog
[TIMON]
[spoken] Very nice
[PUMBA]
[spoken] Thanks.
[TIMON]
He found his aroma lacked a certain appeal
He could clear the savannah after ev’ry meal
[PUMBA]
I’m a sensitive soul though I seem thick-skinned
And it hurt that my friends never stood downwind
[PUMBA]
And, oh, the shame
[TIMON]
Oh, the shame!
[PUMBA]
Thought of changin’ my name
[TIMON]
What’s in a name?
[PUMBA]
And I got downhearted
[TIMON]
How did ya feel?
[PUMBA]
Ev’rytime that I…
[TIMON]
[spoken] Hey, Pumbaa! Not in front of the kids!
[PUMBA]
[spoken] Oh, sorry.
[TIMON AND PUMBA]
Hakuna Matata!
What a wonderful phrase
Hakuna Matata!
Ain’t no passing craze
[YOUNG SIMBA]
It means no worries for the rest of your days
[ALL]
It’s our problem-free philosophy
Hakuna Matata!
Hakuna Matata! Hakuna Matata!
Hakuna Matata! Hakuna Matata!
Hakuna Matata! Hakuna Matata!
Hakuna Matata! Hakuna —
[(OLDER) SIMBA]
It means no worries for the rest of your days
[ALL]
It’s our problem-free philosophy
Hakuna Matata! [repeat]
[PUMBA]
I say “Hakuna”
[TIMON]
I say “Matata”

via Blogger http://ift.tt/2oRz4rc