So, I’m rewriting my engine and I’m struggling with API names.
I was using prefixes and suffixes to differentiate the engine and subsystems stuff. For example, this program for an hypothetical action game:
Code:
program Game;
uses
Mingro, mngSprites, Title, Playfield, sysutils;
function Initialize: Boolean;
begin
if mngInit and mngInitializeSprites then
begin
PlayerSpriteSheet := mngLoadSpriteSheet ('player.spr');
if PlayerSpriteSheet = Nil then
Exit (True)
else begin
mngLog.Trace (etError, 'Can''t load sprite sheet.');
Exit (False)
end
end
else begin
mngLog.Trace (etError, 'Can''t init engine!');
Exit (False)
end
end;
begin
if Initialize then while RunTitle do RunGame
end.
What I was thinking is to use the unit name as namespace, so the previous program will look like this:
Code:
program Game;
uses
Mingro, mngSprites, Title, Playfield, sysutils;
function Initialize: Boolean;
begin
if Mingro.Initialize and mngSprites.Initialize then
begin
Playfield.PlayerSheet := mngSprites.Load ('player.spr');
if Playfield.PlayerSheet = Nil then
Exit (True)
else begin
Mingro.Log.Trace (etError, 'Can''t load sprite sheet.');
Exit (False)
end
end
else begin
Mingro.Log.Trace (etError, 'Can''t init engine!');
Exit (False)
end
end;
begin
if Initialize then while Title.Run do Playfield.Run
end.
I don’t see that kind of API anywhere but I think it is clean and clear and more appealing than using prefix/sufix to differentiate between subsystems (except the fact that when two units declare an object with same name and you don’t precede it with the unit name, FPC doesn’t warns and picks one resulting in some funny debug sessions).
What do you think about this kind of API?
Bookmarks