Programming. Lesson 1. Naming and Assigning Variables and Constants

Hi guys!

I share my experience lesson 1.

The language for the examples is Golang.

*—————————————————————- ———————————*
Context dictionary:

Legal code, code – author’s code, without external intervention;

Variable — a named memory area available for editing (change) by the legal code of the given program;

Constant – a named memory area that is not editable by legal code;

Object, name – a general reference to Variables and Constants.

*—————————————————————- ———————————*
So the rules are:
*—————————————————————- ———————————*
+ name consists of several words together and starts with small letters. Each subsequent word of this name big:

var(
sendResultBytes []byte
)

*—————————————————————- ———————————*
+ name should as much as possible succinctly indicate purpose and meaning object so that you don’t have to comment further on how much it will turn out while maintaining the readability of the code, and at the end of the name I indicate type of this object:

send – send a message
result – collected in one variable result of calculations / data collection
bytes type of variable = []byte / byte slice / slice

// Define constants:
const(
apiUx byte = 1
apiUxPrintLogText byte = 1
)
var(
textHelloWorld []bytes = []bytes(“Hello world!”)
)

// Allocate a new piece of memory for string generation:
sendResultBytes = nil

// API commands come first:
sendResultBytes = append(sendResultBytes, apiUi, apiUiPrintLogText)

// Add the message itself for output to the logging terminal:
sendResultBytes = append(sendResultBytes, textHelloWorld…)

// Send to the distribution channel:
chSendMsgToFront <- sendResultBytes

*—————————————————————- ———————————*
+ y objects – lists / lists there should be common signs in the names:
errorPage404
errorUnknown
errorNameLenMustBe256max
errorNameLenMustBe8min

*—————————————————————- ———————————*
+ if you need to assign a specific value to a variable, it is more convenient to use a reference to a constant:
// can do this way:
bufferLoginLength = 256
bufferPassLength = 256

// more convenient do this way:
const(
maxBufferLength = 256
)
bufferLoginLength = maxBufferLength
bufferPassLength = maxBufferLength

In the second option, you don’t have to run around the whole code to look for all the places where the dimension in 256 is mentioned, it will be enough to change the constant.
This rule applies even to those cases where the value will be equal to “1”.
And the number of constants equal to “1” can be several, but with different names.

*—————————————————————- ———————————*
+ The presence and use of constants helps to realize that when reading a value from it, you will not have to block the mutex, since there will be no situation with simultaneous access to read and write from different code execution threads.

*—————————————————————- ———————————*
+ you need to start the name with a small letter in order to distinguish the variables of the parent class from the child, we also add an internal mutex to the structure and use tabulation for beauty and readability:

// assign the structure of the parent type:
type broadcastList struct {
Mu sync.Mutex
address string
Name string
UniqID uint64
}

// assign a parent type variable:
parrentBroadcast := new(broadcastList)

// Lock the mutex with Tab indentation for the convenience of visual perception of the code:
parrentBroadcast.Mu.Lock()

// assign values ​​to child type variables:
parrentBroadcast.Address = “@Mars planet”
parrentBroadcast.Name = “Elon Musk”
parrentBroadcast.UniqID = 1

// Release the mutex:
parrentBroadcast.Mu.Unlock()

*—————————————————————- ———————————*
+ for heavy / long operations on distributed values, it is more profitable to use additional variables, if the matter does not concern the criticality in the relevance of values ​​and the consumption of additional memory will not lead to overflow.

For example, to output to the terminal, the heavy construction log.Printf can be used, which in this example is not critical to the accuracy of the distributed data at the time of output, and it is not necessary to execute it in blocking mode:
mu.Lock()
a := unixTimeScaleUint64
mu.Unlock()
log.Printf(“8755 unix = %d”, a)

Thus, the distributed variable unixTimeScaleUint64 becomes available to other threads as early as possible, while a copy of its value is used to output to the terminal in a long instruction.

Pay attention to the number 8755, which indicates the line number of the text code – this is an additional convenience for tracing the program in the programming / debugging mode, so that it is clear at what point the output to the terminal is triggered.

*—————————————————————- ———————————*
+ To quickly return to the place where the code was last edited (within the current session), it is convenient to use the keyboard shortcuts CTRL+Z and immediately CTRL+Y.

*—————————————————————- ———————————*
+ For each group of variables, it is more convenient to use a separate named mutex.

*—————————————————————- ———————————*
+ If I missed something else, then I will supplement it in subsequent lessons.

It will not be possible to discuss, since only one comment per day is available. Or write in HP, if I can suggest something.

Thank you for your attention!

Sergey Popov (Socket Language)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *