Skip to content

Styleguide

Tonybodo edited this page Jun 19, 2022 · 4 revisions

Styleguide

Code Layout

  1. Use a line break after the methods name
  2. Use a line break after definitions of temporary variables
  3. End the method without dot
  4. Use no dot at the end of a block
  5. Square Brackets always in block form and without whitespaces
  6. Comment after temporary variables
  7. Method name describes parameter as accurate as possible (e.g. anInteger, anAction etc.)
  8. 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

Naming

  1. Choose names that are descriptive
    • timeOfDay, not tod
    • milliseconds, not millis
  2. Capitalize class names
  3. Do not capitalize instance and temporary variables, parameters, and methods
  4. Choose a name indicative of a classification of objects
    • ProblemReport, not Application (too generic)
    • TreeWalker, not TreeWalkerForBinaryTrees (too specific)
  5. Avoid naming a class that implies anything about its implementation
    • PrDatabase, not PrDictionary (aggregation)
    • ProperName, not ProperNameString (aggregation)
    • SortedSet (inheritance)
  6. Choose method names so that code reads as a sentence
  7. Use imperative verbs and phrases for methods which perform an action
    • aReadStream peekWord, not aReadStream word
  8. Use a phrase beginning with a verb for methods that answer a Boolean and use describly adjectives
    • aPerson isHungry, not aPerson hungry
  9. Avoid the parameter type or name in the method name
    • fileSystem at: aKey put: aFile, not fileSystem atKey: aKey putFile: aFile
  10. Use #new for instance creation methods; #initialize for setting values
  11. 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
  12. A get method should have the same name as the variable
  13. A set method should have the same name as the variable, followed by a colon
  14. 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
  15. Typed parameter names should indicate the most general class of objects
    • anInteger, aString, aRectangle, aFile, …
  16. Do not use hard-coded numbers (magic numbers) in an expression
  17. Spell out identifiers completely
    • receivedTime, not rcvdTime or rTime

Comments

  1. Make comments succinct, concise, and grammatically correct
  2. Do not comment bad code – rewrite it
  3. Comment for a class
  4. 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
Clone this wiki locally