-
Notifications
You must be signed in to change notification settings - Fork 1
Styleguide
Tonybodo edited this page Jun 19, 2022
·
4 revisions
- Use a line break after the methods name
- Use a line break after definitions of temporary variables
- End the method without dot
- Use no dot at the end of a block
- Square Brackets always in block form and without whitespaces
- Comment after temporary variables
- Method name describes parameter as accurate as possible (e.g.
anInteger
, anAction etc.) - Use as brackets only when nessesary
A squeak method with temporary variables and if-clauses should have the following look
actionsFrom: aCompiledMethod For: aClass
| methodResult actions|
"here you can comment your method"
actions := OrderedCollection new.
methodResult := aCompiledMethod valueWithReceiver: aClass arguments: {}.
methodResult isDictionary
ifTrue: [methodResult keys do: [:ea | self add: ea IfAnActionTo: actions]]
ifFalse: [methodResult isCollection
ifTrue: [(methodResult do: [:ea | self add: ea IfAnActionTo: actions])]
ifFalse: [self add: methodResult IfAnActionTo: actions]].
^ actions
- Choose names that are descriptive
- timeOfDay, not tod
- milliseconds, not millis
- Capitalize class names
- Do not capitalize instance and temporary variables, parameters, and methods
- Choose a name indicative of a classification of objects
- ProblemReport, not Application (too generic)
- TreeWalker, not TreeWalkerForBinaryTrees (too specific)
- Avoid naming a class that implies anything about its implementation
- PrDatabase, not PrDictionary (aggregation)
- ProperName, not ProperNameString (aggregation)
- SortedSet (inheritance)
- Choose method names so that code reads as a sentence
- Use imperative verbs and phrases for methods which perform an action
- aReadStream peekWord, not aReadStream word
- Use a phrase beginning with a verb for methods that answer a Boolean and use describly adjectives
- aPerson isHungry, not aPerson hungry
- Avoid the parameter type or name in the method name
- fileSystem at: aKey put: aFile, not fileSystem atKey: aKey putFile: aFile
- Use #new for instance creation methods; #initialize for setting values
- Use a descriptive method name to create an object that requires initialization (like constructors)
BookEntry class>>newWithName: aName phoneNumber: aPhoneNumber ^ self new name: aName; phoneNumber: aPhoneNumber; yourself
- A get method should have the same name as the variable
- A set method should have the same name as the variable, followed by a colon
- When two get methods effectively return the same variable, but one adds behavior, prefix the actual get method with basic
books ^ self basicBooks copy
basicBooks ^ books
- Typed parameter names should indicate the most general class of objects
- anInteger, aString, aRectangle, aFile, …
- Do not use hard-coded numbers (magic numbers) in an expression
- Spell out identifiers completely
- receivedTime, not rcvdTime or rTime
- Make comments succinct, concise, and grammatically correct
- Do not comment bad code – rewrite it
- Comment for a class
- Specify methods to be implemented by a subclass in abstract class comments
Collection>>do: aBlock "Evaluate aBlock with each of the receiver's elements as the argument." ^ self subclassResponsibility