-
Notifications
You must be signed in to change notification settings - Fork 378
Get rid of object.humanoid_class == className
#3134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Get rid of object.humanoid_class == className
#3134
Conversation
…ss == "className"> replaced to <humanoid:isType("className")>
| function Humanoid:isType(humanoid_class) | ||
| return self.humanoid_class == humanoid_class | ||
| end | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this; as we already have code made to support a check in a better way: class.is(humanoid, ClassName)
CorsixTH/CorsixTH/Lua/class.lua
Lines 127 to 141 in 39d7211
| function class.is(instance, class) | |
| local typ = type(instance) | |
| if typ ~= "table" and typ ~= "userdata" then | |
| return false | |
| end | |
| local methods = instance | |
| while methods do | |
| if methods == class then | |
| return true | |
| end | |
| local mt = getmetatable(methods) | |
| methods = mt and mt.__index | |
| end | |
| return false | |
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or you could make this function call class.is instead too.
Lots of newbie programmers hacked the code. They tend to copy/paste already existing code blocks. |
|
Of course, much of the code after class checks should probably be moved the proper derived class. |
Describe what the proposed change does
We have a lot of calls in our code like
object.humanoid_class == "Handyman"I found 69 matches in 25 files (!).
(Do you also smell a lot of git conflicts?) 😁
Some objects directly read the fields of another object and also externally compare this field value.
It was a bit bothering me as I think it's a typical violation of encapsulation for me.
Logically, this should be a call to a method of this object, to which the object for comparison is passed.
And the object itself decides the results of this comparison.
Filed
humanoid_classshould not be exposed outside and should not participate in external operations.So I propose to rework this to call the appropriate method.
object.humanoid_class == "Handyman"->
object:isType("Handyman")Overall, it a minor issue.
I was more concerned that this strange approach was being copied and pasted everywhere and was growing. So reading and externally comparing the
humanoid_classfield seemed like a common practice.I was working on another task, and it required me to copy this comparison to other places one more.
But I realised I didn't want to copy that and decided to tidy things up a bit.
Oh yeah, and on top of that, it'll possible break something somewhere. How could it be otherwise? 😂
But I went and lit a candle in the church to prevent this from happening. 🤞🏻 🕯️