Working with primitive data structures

Steve Mu
3 min readOct 12, 2021

What I learned from Code Complete

Strings

Avoid string literals. Use name constants instead.

You might need to change its content later.

Made it easier to change strings grouped in a file for multi-lingual support.

Using of named constants clarify your intentions. I think ’N’ could be named NO_VALUE, ‘Y” could be named “YES_VALUE”

Decide on internationalization strategy early. Store all strings in external resource, like a seperate file, or creating build for each language, such as hosting english version on /site/en and chinese version on /site/cn, or determine language at runtime — read language setting from browser or user setting and show the specific language?

Booleans

boolean can make tests in if statement clearer.

Enumerated types

it improves readability. You can use enumerated types in function parameter instead of true, false etc.

Use enumerated types as boolean, such as Success, Warning, FatalError to distinct different kind of failure or success.

You can use it in loop. Define start and end value in the enumerated types, such as InvalidFirst, First, … Last in a enumerated type.

named constants

help make sure software is soft and easy to change.

you can store the length of an array in the named constant.

any technique that centralizes control helps maintenance.

Arrays

consider use container class to access data sequentially, such as sets, queue, stack, before choosing using array. random access to array is error prone.

Use meaningful variable name in nested loops to avoid index cross-talk.

Type aliasing

example: alias an float, later easier to change to double and facilitate information hiding

defined a named constant for array length. you will find many places in app in which you will use it.

helps you centralize a type in one place instead of spreading typing details all over

create type aliasing with real-world problem facing names

Using your own type document a variable. Coordinate xis better than float x

Structures

pretty similar to js objects, can group related data together. For example, employee datas and supervisor datas.

Pass information on a need-to-know basis. Sometimes pass a whole employee object to a function, sometimes just pass one or two as separate parameters.

Pointers

a pointer point to a location in the memory. It does now know the meaning of the content.

The base type of the pointer tells how to interpret the content, such as string, integer, float etc.

Global data

you want to write class and functions that as little connection as possible to global data. Information hiding and modularity helps making big program maintainable.

Big programs need to manage complexity. Modularity or breaking down a big program into smaller pieces so you can think one part at a time helps with that. If you use global data, you can’t concentrate on one part of program at a time, you have worry about other parts of the program that use the global data.

global data eliminate tramp data. When you pass an object to a function and that function don’t use it and just pass it to another function, the object is trap data.

Start making a variable local. Only make it a little globaler as needed.

--

--