I had an internal fight with myself recently. After many years, I had strong feelings against some of my code in Monkey and the way it was done. So now I’m writing my idea of best practices. Which are my new guidelines to nice code in Monkey.
First off, getters and setters. They’re stupid and I’m not doing it anymore. I’ve been thinking in my head for some time, that I used to be against it. Then I started going with this abstraction of the data idea. But now I’m coming back because I had a thought like, “Wait, what’s the point of this private variable being accessed like it’s public. I made it private for a reason. If I was going to use it like it was public, then why on Earth is it private?!” In the sense of objects, either it is accessible or it’s not. The keywords are there for a reason. I guess this a side effect of Java fan boys. Now, on the other hand, a property (getter/setter) method is okay if the item in question has some sort of special needs performed when the value is updated. Therefore, I stick by that.
My second complaint is some of the naming. Mainly, the fact that there’s no sort of way to differentiate my public methods from my private ones. Now, it might seem like a trivial issue, but when the classes size grows large, yet cannot be split into smaller pieces, it gets to be a pain. So I’m changing that too.
My third complaint, to myself, is spacing. Since I was a hardcore php scripter, I got into the habit of following the Zend guidelines along with some other guidelines to create this ultra spacing. This has turned into somewhat of a code readability liability. So I’m converting back some things to an older style and keeping some of the changes I did make.
The fourth, and last, is constants. In the past, UPPER_CASE was the standard. In today’s world, they feel driven hide the fact that a constant is a constant. Now this concept baffles me. We’re going to make a constant, but you don’t need to know that…even though I could probably figure it out. The newer standards have you name it like a variable. Err, stop. This is a problem. I know it might sound odd, but I’d rather not forgot that I can’t modify a const. And others might get the impression that they can modify a constant. Therefore, I believe the upper case idea should stay. With one modification…
Enjoy
(Monkey)
Function FunctionName( parameter ) ' Function Names (PascalCase)
End
Function Main:Int()
New Test()
Return 0
End
Class Test Extends App ' Class Names (PascalCase)
Private
Field _variableName:Int ' Private Class Members
Field _specialModfied:Int
Const _MIN_VALUE:Int = 5 ' That's right, an underscore for private constants
Method privateMethod:Bool( firstNumber:Int, secondNumber:Int ) ' Spacing, Private Method Name (camelCase)
Local newVariable:Int = _MIN_VALUE
If ( firstNumber > newVariable ) ' Spacing after a control statement and a space between parentheses
If ( secondNumber - firstNumber > 0 )
Return True
End
End
Return False
End
Public
Field VariableName:String ' Public Variable Name
Const PUBLIC_CONSTANT:Int = 1 ' Standard inline with other languages
Method MethodName:Void( variableName:Int, parameter:Int ) ' Public Method Name (PascalCase)
_variableName = variableName
If ( privateMethod(6, 7) ) ' no spaces between function parameter parentheses
Print("Yes")
End
Local arrayVariable:String[] = New String[10] ' No spacing between square brackets
End
Function FunctionName:Int( parameter:Int ) ' Static methods follow function naming convention
Return parameter * 2
End
Method SpecialModified:Void( value:Int ) Property
_specialModified = value
_variableName = value * 2
End
Method SpecialModified:Int() Property
Return _specialModified
End
End
Interface IApp ' Prefixed with I
Method StartUp:Void()
End
' If you're doing a framework, a prefix is not only encouraged, but promoted by me!
Class TiMyClass
End
Class Ti_MyClass
End
Function TiMyFunc:Int()
Return 1
End
Function Ti_MyFunc:Int()
Return 1
End(php)
function FunctionName( $parameter ) { } # Function Names (PascalCase) function main() { new Test(); return; } class Test extends App // Class Names (PascalCase) { private $_variableName; // Private Class Members private $_specialModified; private const _MIN_VALUE = 5; // That's right, an underscore for private constants private function privateMethod( $firstNumber, $secondNumber ) // Spacing, Private Method Name (camelCase) { $newVariable = self::_MIN_VALUE; if ( $firstNumber > $newVariable ) // Spacing after a control statement and a space between parenthesis { if ( $secondNumber - $firstNumber > 0 ) return 1; } } public $VariableName; // Public Variable Name const PUBLIC_CONSTANT = 1; // Standard inline with other languages except .NET based languages public function MethodName( $variableName, $parameter ) // Public Method Name (PascalCase) { $this->_variableName = $variableName; if ( $this->privateMethod(6, 7) ) // no spaces between function parameters parentheses echo "Yes"; $arrayVariable = new array(); // php is dynamic and a bit different than languages where you have to set the size of the array $arrayVariable[] = 5; // No spacing between squre brackets } static function FunctionName( $parameter ) // Static methods follow function naming convention { return $parameter * 2; } } interface IApp // Prefixed with I { public function StartUp(); } // If you're doing a framework, class prefix is not only encourage, but promoted by me! class TiMyClass {}






