I have some new habits.

Started by
79 comments, last by Tutorial Doctor 10 years ago

Okay, I have some new programming habits. I need to know if they are practical.

Why use True/False?


on = True
off = False
 
if light.on then
     JumpForJoy()
end

Sometimes I don't like the names of standard functions:


function Display(text)
    print(text)
end

I really like to use Synonyms for functions that do similar but different tasks.


Begin()
Start()
Commence()
Play()
 
End()
Finish()
Conclude()
Stop()

Being descriptive with my argument names:

function Walk(speed)
       speed = true
end
 
if slowly then
     MakeASadPuppyFace()
end
 
Walk(slowly)

Anyone have any peculiar programming habits that could be of some help?

In other words, any way I can make my code more human-readable?

They call me the Tutorial Doctor.

Advertisement

I don't understand what's going on here.

1) In your first code snippet, you define on and off (Lua uses lowercase true and false keywords), but not light. Saying


if (light.on) then

is the same as


if (light.on == true) then

if that's what you're asking.

If you mean using


if (light == true) then

then to me, that's not clear as "light" would be an an instance of a light object. light.on or lightOn would be better.

2) As you're using Lua, you could just write


local display = print;

This also preserves print's vararg ability.

3) As long as you're clear and consistent (if you use "start" in one place, you use it in all others, and the correct antonym "stop" in all others too), this doesn't matter.

4) What is going on here? Your function won't work properly because only tables, userdata and functions are passed by address in Lua. Also "slowly" is an unclear name.

Falling block colour flood game thing I'm making: http://jsfiddle/dr01d3k4/JHnCV/


Why use True/False?

That really just looks like a boolean property "on" of the "light" object. Usually "True" is implicit as in first order logic, simply stating "P" means the predicate P is true, so "if light.on then" translates to "if the predicate `light.on` is true then". Types and variables are not the same thing.


Sometimes I don't like the names of standard functions:

I wouldn't recommend doing this. It might look cute in the short term but once you work with other people they will have no idea what you mean by "Display". Does it display things on a window? Does it print them out in the console? Does it do anything else in addition to that? Unless your function actually does something more than just being a pass-through to an existing standard function, you should just use the standard function, it's clearer and idiomatic. Otherwise everyone rolls their own version of function and type names and it's harder to understand other people's code. If you must, make it an alias instead of writing useless wrapper code, but I don't find it a good habit.


I really like to use Synonyms for functions that do similar but different tasks.

It really depends on your code. In most cases it's not advisable since it's harder to immediately know what a function does ("is it Begin() that does X? no, wait, that would be Start().. or Play()... lemme check the code...") but in a few select cases it might make sense depending on what you are writing. I would honestly think hard about whether it is meaningful to do this, because you could waste a lot of time confusing yourself - and others - down the road with all these identical-sounding functions.


Being descriptive with my argument names:

Being descriptiive with argument names is a good thing. Though in your given example the code might look cute with the grammatically correct "walk slowly" function call, but why is speed a boolean variable? Shouldn't it be a floating-point variable giving the speed in units per second, or at least an enum like "slow", "normal", "fast" or similar? "Slowly" is also not very precise - if you just want an argument that says whether to move "slowly" or "quickly", then you should probably just call the argument itself "slowly", or make it accept an enum with two values "slowly" and "quickly". That's much more expressive if you don't intend to actually pass in a speed but only one of two options.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Good points.

the code:


if light.on then
 
end

is an example of how I might use "on" to mean "true."

so the code would be :


light.on = true
light.off = false
 
if light.on then
 
end

This is distinguished from:


fan.on = true
fan.off = false

I have actually used the quickly and slowly adjectives by assigning numerical values to them, which set the speed of the walk.


if Walk(slowly) then
velocity = 1
end
 
if Walk(quickly) then 
velocity = 3
end

Note: I am using the word velocity instead of speed here (my actual code is not so confusing, I will post a snippet in another reply.) I also don't set the velocity this way, but this is one way I could do it.

The walk function would return the boolean value:


function Walk(speed)
    speed = true
    return speed
end

I used this for my senses system so that I could type:


if See(door) then
   TurnLeft()
end

Where the See() function checks a collision between an object and the door object, and returns true if the collision happens.

Then I can see all types of things.


if See(person) then
if See(wall) then

Slowly and quickly are interpretations of the numerical values. For instance, when you think "slowly" you think not as fast as "quickly."

I can explicitly change the interpretation by adjusting the numerical value to make quickly mean "3 times as fast."

They call me the Tutorial Doctor.

Example from a working script:


-----------------------------------------------------------------------------------
-- Maratis
-- Jules script test
-----------------------------------------------------------------------------------
on = true
off = false

-- Get objects
Player = getObject("Player")
Head = getObject("Head")
eyes = getObject("Eyes")
ears = getObject("Ears")
box = getObject("Box")
monkey = getObject("Monkey")
building = getObject("Building")
smallBox = getObject("small box")
door = getObject("door")

soundplay1 = off
soundplay2 = off
soundplay3 = off
soundplay4 = off


seeBox = getObject("SeeBox")--sound
seeMonkey = getObject("SeeMonkey")--sound
seeBuilding = getObject("SeeBuilding")--sound
tack= getObject("Tack")--sound
rightway = getObject("rightway")

--Text Overlay
camera = getObject("Camera0")
gui = getScene("gui")

--Get text Objects from another Scene to overlay on first scene
see = getObject(gui,"see")
touch = getObject(gui,"touch")
hear = getObject(gui,"hear")
collect = getObject(gui,"collect")
state = getObject(gui,"state")
hear_distance =getObject(gui,"hearDistance")

--Project a scene over a camera
enableCameraLayer(camera,gui)

--Emotional States
mood = 5 --initial mood (normal)
sad = false
happy = false
normal = true

--setText(txt_readout,distance)


--SENSES
--SEE
function See(object)
	if isCollisionBetween(eyes,object) then
		seenObject = getName(object)
		setText(see, "I see a ".. seenObject)
		seeing = true
		
		return true
	else 
		--setText(see,"I see nothing")
		seeing = false
	end
end

--TOUCH
function Touch(object)
	if isCollisionBetween(Player,object) then
		touchedObject = getName(object)
		setText(touch, "I feel a ".. touchedObject)
		touching = true
	else setText(touch,"I feel nothing")
		touching = false
	end
end

--SPEAK
function Speak()
	if See(box) then
		if soundplay1 == off then
			playSound(seeBox)
		end
		soundplay1 = on
	else 
		soundplay1 = off
	end
	
	if See(monkey) then
		if soundplay2 == off then
			playSound(seeMonkey)
		end
		soundplay2 = on
	else
		soundplay2 = off
	end
	
	if  See(building) then
		if soundplay3 == off then
			playSound(seeBuilding)
		end
		soundplay3 = on
	else
		soundplay3 = off
	end
	
	if  See(door) then
		if soundplay4 == off then
			playSound(rightway)
		end
		soundplay4 = on
	else
		soundplay4 = off
	end
end

--ACTIONS
function Jump(object)
	if seeing then
		setGravity({0,0,-4})
		addCentralForce(object,{0,0,9},"local")
	end
end

function Destroy(r,s,t)
	deactivate(r)
	deactivate(s)
	deactivate(t)
end

function Collect(object)
	if isCollisionBetween(Player,object) then
		Destroy(object)
		playSound(tack)
		setText(collect,"I collected a " .. getName(object))
	end
end


--mood = {sad = false, normal = false, happy = false}
--mood.sad, mood.normal, moode.happy

function ChangeEmotion()
	
	if mood >=0 and mood < 4 then
		sad = true
		normal = false
		happy = false
		--setText(state,"I am sad")
	end
		
	if mood>=4 and mood <=6 then
		sad = false
		normal = true
		happy = false
		--setText(state,"I am normal")
	end
		
	if mood >6 and mood <=10 then
		sad = false
		normal = false
		happy = true
		--setText(state,"I am happy")
	end
end

function TriggerEmotionChange()
	if See(box) then
		mood = 10
		setText(state, getName(box) .. "es " .. "make me glad")
	elseif See(monkey) then
		mood = 3
		setText(state, getName(monkey) .. "s " .. "make me sad")
	elseif See(building) then
		mood = 5
		setText(state, getName(building) .. "s " .. "make me turn around in circles")
	
	elseif See(door) then
		mood = 5
		setText(state, "This is a locked " .. getName(door))
	else
		setText(state,"Thinking...")
	end
end


-- Scene Update
function onSceneUpdate()
	ChangeEmotion()
	TriggerEmotionChange()
	Move()
	
	Touch(building)
	Touch(monkey)
	Touch(box)
	Touch(door)
	
	See(box)
	See(monkey)
	--See(building)
	Speak()
	
	Hear(monkey)
	Collect(smallBox)
	--Collect(box)
	--Collect(monkey)
	See(door)
	
	--Jump(Player)
	--Destroy(txt_seeing,txt_hearing,txt_touching)
	
end


They call me the Tutorial Doctor.


light.on = true
light.off = false
 
if light.on then
 
end

This could get confusing. When you want to toggle the on/off state, you have to remember to set both so that you don't have a light that's both on and off. Also, off is implied by not on and so isn't even required.

Your Walk() example is confusing too. It returns true in all cases and isn't clear what's going on. For the see example, using "canSee(object)" would be better as just "see(object)" could imply that you want to look at the object instead.

In your full code example, your functions like "see" and "touch" are doing multiple things and as I said, from their name aren't clear on purpose.

Falling block colour flood game thing I'm making: http://jsfiddle/dr01d3k4/JHnCV/


For the see example, using "canSee(object)" would be better as just "see(object)" could imply that you want to look at the object instead.

Great suggestion! I actually threw together that script without considering how it would sound grammatically (I do have to fix this). I should be returning "seen" not true.

I was thinking of


if Seen(object)

but I want to use the word "seen" as a Boolean.

something like


if boy.seen 

The see() and touch() functions are doing multiple things for the purposes of the level I am doing, but all they basically do is get a collision and return a Boolean.

Now that I think about it, I need to compile a Demo of my Senses system (although I wanted to clean it up first).

Edit: I could do:


if boy.Sees(object) then

They call me the Tutorial Doctor.

Is lua different to where


light.on = true
light.off = false

isnt just setting attribute values??

Tutorial doctor this is all an ellaborate ruse isn't it? No one in their right mind would share such valuable programming techniques.

Your authority is not recognized in Fort Kick-ass http://www.newvoxel.com

How the Synonyms are used:


menu = Menu() --Creates a new menu
menu.Start() --Starts the menu
menu.End() --Stops the menu
----------------------------------------------------------------------------------------

--getScene("sceneName")
--getScenesNumber()

cut_scene = newCutScene() --Creates a new cut-scene
cut_scene.Play()		 --Plays the new cut-scene
cut_scene.Stop() --Stops the new cut-secene

--getScene("sceneName")
--getScenesNumber()

cut_scene_2 = newCutScene() --Creates a new cut-scene
cut_scene_2.Play()	changeScene(scene) --Plays the new cut-scene
cut_scene_2.Stop() --Stops the new cut-secene
-------------------------------------------------------------------------------------
--loadLevel("levels/myLevel.level")

level1 = Level() --Creates a new level
level1.Begin() = 		loadLevel("levelName") --Starts the new level
level1.Finish() --Stops the new level
-------------------------------------------------------------------------------------
--getObject(« objectName »)
--activate(object)
--deactivate(object)
--isVisible(object)
--isActive(object)

prop1 = Prop()=	--Creates a new prop
prop1.Create(room) --Creates the mesh for the new prop
prop1.Destroy()

They call me the Tutorial Doctor.

This topic is closed to new replies.

Advertisement