THE ULTIMATE PROFESSIONAL CNC CODE & PROGRAMMING SHORTCUTS BIBLE
Professional CNC programming is not about memorizing hundreds of codes.
It is about knowing which codes matter, how modal states interact, how to reuse proven logic, and how to make programs shorter, safer, easier to debug, and easier to maintain.
This reference combines:
G-Codes
M-Codes
Canned Cycles
Subprograms
Macro Programming
Coordinate Shortcuts
Pattern Programming
Production Programming Techniques
Debugging Methods
Program Optimization
Reusable CNC Code Patterns
IMPORTANT
CNC syntax and behavior vary between Fanuc, Haas, Siemens, Heidenhain, Okuma, Mitsubishi, Mazak and other controls.
Machine builders can also customize M-codes and machine behavior.
Always verify code against the programming manual for the exact controller and machine, then simulate and prove out new programs using appropriate shop procedures.
════════════════════════════════════════════════════════════
SECTION 1 — PROFESSIONAL CNC PROGRAM STRUCTURE
════════════════════════════════════════════════════════════
A clean CNC program normally follows a predictable structure.
Conceptual example:
%
O1000
(PROGRAM NAME)
(SAFE INITIALIZATION)
(WORK OFFSET)
(T1 TOOL DESCRIPTION)
(TOOL CHANGE)
(SPINDLE / COOLANT)
(POSITION)
(MACHINING)
(RETRACT)
(T2 TOOL DESCRIPTION)
…
(RETURN / END)
%
A professional program should make these questions easy to answer:
What tool is active?
What work offset is active?
What coordinate mode is active?
What compensation is active?
What operation is running?
Where is the safe retract position?
What happens next?
Readable code is easier to verify.
Readable code is easier to debug.
Readable code is easier to maintain.
════════════════════════════════════════════════════════════
SECTION 2 — ESSENTIAL G-CODE QUICK REFERENCE
════════════════════════════════════════════════════════════
Common codes on many ISO-style CNC controls include:
G00
Rapid positioning
G01
Linear interpolation
G02
Clockwise circular interpolation
G03
Counterclockwise circular interpolation
G04
Dwell
G17
XY plane selection
G18
XZ plane selection
G19
YZ plane selection
G20
Inch programming
G21
Metric programming
G28
Reference-return related command on many controls
G40
Cancel cutter compensation
G41
Cutter compensation left
G42
Cutter compensation right
G43
Positive tool length compensation on many milling controls
G49
Cancel tool length compensation
G53
Machine-coordinate positioning on many controls
G54–G59
Common work coordinate systems
G73
High-speed peck drilling cycle on many controls
G80
Cancel canned cycle
G81
Basic drilling cycle
G82
Drilling / counterboring cycle with dwell on many controls
G83
Deep-hole peck drilling
G84
Tapping cycle
G90
Absolute programming
G91
Incremental programming
G94
Feed per minute on many milling controls
G95
Feed per revolution on many controls
G96
Constant surface speed on many turning controls
G97
Constant spindle RPM mode on many turning controls
G98 / G99
Meaning depends strongly on machine type and controller context.
Never treat a generic G-code table as universal.
════════════════════════════════════════════════════════════
SECTION 3 — ESSENTIAL M-CODE QUICK REFERENCE
════════════════════════════════════════════════════════════
Common examples include:
M00
Program stop
M01
Optional stop
M02
Program end on many controls
M03
Spindle clockwise
M04
Spindle counterclockwise
M05
Spindle stop
M06
Tool change on many machining centers
M08
Coolant on
M09
Coolant off
M30
Program end and reset/rewind behavior on many controls
M98
Subprogram call on many controls
M99
Subprogram return / looping behavior depending on context
Other M-codes may control:
Air blast
Chip conveyor
Probe
Pallet changer
Tailstock
Chuck
Doors
Clamps
Rotary-axis functions
High-pressure coolant
Machine-specific automation
M-codes are especially machine-builder dependent.
Never assume an unfamiliar M-code is universal.
════════════════════════════════════════════════════════════
SECTION 4 — G00 VS G01
════════════════════════════════════════════════════════════
G00
Used for positioning.
G01
Used for controlled linear feed motion.
Professional shortcut:
Do not waste cutting time feeding through empty space when a verified clearance move can be used.
But optimization never outranks collision safety.
A rapid endpoint can be valid while the path to that endpoint is unsafe.
Verify the complete motion.
════════════════════════════════════════════════════════════
SECTION 5 — G02 / G03 ARC PROGRAMMING
════════════════════════════════════════════════════════════
G02
Clockwise arc
G03
Counterclockwise arc
Arc definitions commonly use either:
I J K
or
R
depending on the control and programming method.
Conceptual example:
G01 X0 Y0
G02 X20 Y20 R20
Professional programmers verify:
Active plane
Start point
End point
Direction
Radius / center definition
Controller arc conventions
Large-arc behavior
Never debug an arc while ignoring the active G17/G18/G19 plane.
════════════════════════════════════════════════════════════
SECTION 6 — G90 VS G91: ONE OF THE MOST IMPORTANT SHORTCUTS
════════════════════════════════════════════════════════════
G90
Absolute positioning.
G91
Incremental positioning.
Absolute programming is excellent for defining geometry from a fixed origin.
Incremental programming can make repeated patterns extremely compact.
Instead of calculating every absolute position:
X10
X30
X50
X70
A repeated pattern may conceptually use:
G91
X20
X20
X20
Then restore the required positioning mode.
Professional rule:
Whenever temporarily changing a critical modal state, deliberately restore the expected state.
════════════════════════════════════════════════════════════
SECTION 7 — G54 TO G59: MULTIPLE PART SHORTCUT
════════════════════════════════════════════════════════════
Multiple work offsets can allow the same machining logic to be reused at different fixture locations.
Concept:
G54
Machine Part Position 1
G55
Machine Part Position 2
G56
Machine Part Position 3
Instead of copying and manually changing hundreds of coordinates, reuse proven machining logic with different coordinate systems where appropriate.
This is one of the foundations of fixture-based production programming.
════════════════════════════════════════════════════════════
SECTION 8 — G53: MACHINE COORDINATE SHORTCUT
════════════════════════════════════════════════════════════
On many controls, G53 provides non-modal positioning in the machine coordinate system.
It can be useful for:
Clearance positions
Tool-change positioning
Moving away from fixtures
Known machine positions
But machine coordinates are not work coordinates.
A safe G54 coordinate may be completely different from a safe G53 coordinate.
Verify machine travel and machine-builder behavior before use.
════════════════════════════════════════════════════════════
SECTION 9 — G43 TOOL LENGTH COMPENSATION
════════════════════════════════════════════════════════════
A common milling pattern is:
T1 M06
…
G43 H01 …
Conceptually:
T number
selects or calls a tool.
H value
references tool-length compensation data on many controls.
A professional programmer keeps tool identity and compensation identity easy to audit.
Typical debugging question:
Does the physical tool match the T call and the intended H offset?
Wrong tool-length compensation can create severe positioning errors.
════════════════════════════════════════════════════════════
SECTION 10 — G41 / G42 CUTTER COMPENSATION
════════════════════════════════════════════════════════════
G41
Cutter compensation left
G42
Cutter compensation right
G40
Cancel cutter compensation
Why professionals use cutter compensation:
Tool diameter can be adjusted without rewriting every programmed contour.
It can support:
Tool wear compensation
Finish-size adjustment
Tool replacement
Process tuning
But compensation requires correct:
Lead-in
Lead-out
Tool-radius data
Compensation direction
Geometry
Never activate compensation blindly at an arbitrary contour point.
════════════════════════════════════════════════════════════
SECTION 11 — G81 DRILLING SHORTCUT
════════════════════════════════════════════════════════════
Without a canned cycle, every hole may require several explicit motion blocks.
A drilling cycle can compress repeated logic.
Conceptual Fanuc-style example:
G81 X10. Y10. Z-12. R2. F120.
X30. Y10.
X50. Y10.
X70. Y10.
G80
One cycle definition.
Multiple hole positions.
G80 cancels the active canned cycle.
Exact parameters and behavior must be verified for the controller.
════════════════════════════════════════════════════════════
SECTION 12 — G83 DEEP-HOLE DRILLING
════════════════════════════════════════════════════════════
G83 is commonly used for peck drilling.
Conceptual example:
G83 X20. Y20. Z-30. R2. Q5. F100.
Typical purposes:
Chip evacuation
Deep-hole drilling
Controlled pecking
Q often represents a peck-related value, but exact semantics and supported options vary by control.
Professional shortcut:
Define the cycle once and then provide additional hole positions when the controller supports modal canned-cycle positioning.
════════════════════════════════════════════════════════════
SECTION 13 — G84 TAPPING
════════════════════════════════════════════════════════════
G84 is commonly associated with tapping.
Conceptual relationship:
FEED = PITCH × RPM
Example for a 1.5 mm pitch thread at 500 RPM:
Feed = 1.5 × 500
Feed = 750 mm/min
This relationship is useful for understanding tapping synchronization.
Actual rigid-tapping syntax and machine requirements vary.
Always verify:
Pitch
Spindle direction
RPM
Feed mode
Rigid-tapping requirements
Controller syntax
════════════════════════════════════════════════════════════
SECTION 14 — REPEAT HOLES WITHOUT REPEATING CODE
════════════════════════════════════════════════════════════
Beginner approach:
Write the entire drilling sequence for every hole.
Professional approach:
Separate:
WHAT TO DO
from
WHERE TO DO IT.
WHAT:
Drilling cycle
WHERE:
Hole coordinates
This simple programming philosophy dramatically improves readability.
════════════════════════════════════════════════════════════
SECTION 15 — M98 SUBPROGRAM SHORTCUT
════════════════════════════════════════════════════════════
M98 is commonly used for subprogram calls on Fanuc-style controls.
Conceptual structure:
MAIN PROGRAM
O1000
…
M98 P2000
…
M30
SUBPROGRAM
O2000
…
M99
The main program controls the overall job.
The subprogram contains reusable logic.
Benefits:
Less duplicated code
Easier editing
Smaller programs
Reusable machining routines
Cleaner production logic
════════════════════════════════════════════════════════════
SECTION 16 — M98 REPEAT SHORTCUT
════════════════════════════════════════════════════════════
Some controls support repeat information in an M98 call.
This can allow one subprogram to execute multiple times.
However, P/L formatting and repeat-count conventions vary by controller.
Do not copy an internet example without checking the exact control manual.
The general idea is powerful:
CALL ONCE
↓
REPEAT MANY TIMES
instead of duplicating the same blocks.
════════════════════════════════════════════════════════════
SECTION 17 — M99
════════════════════════════════════════════════════════════
M99 commonly returns execution from a subprogram.
Its behavior can change depending on:
Whether execution is inside a subprogram
Whether it appears in the main program
Controller implementation
Associated addressing
Incorrect M99 logic can create unwanted repetition or looping.
Always verify the call/return chain.
════════════════════════════════════════════════════════════
SECTION 18 — M97 LOCAL SUBPROGRAMS
════════════════════════════════════════════════════════════
On Haas controls, M97 is commonly associated with local subprogram calls.
This can be convenient when reusable logic belongs inside the same program.
Concept:
MAIN LOGIC
↓
CALL LOCAL ROUTINE
↓
RETURN
Advantages:
Single program file
Compact routines
Easy local repetition
M97 is not a universal equivalent across all CNC controls.
════════════════════════════════════════════════════════════
SECTION 19 — M198 EXTERNAL SUBPROGRAMS
════════════════════════════════════════════════════════════
On supported controls, M198 can be associated with calling programs from external storage or related external program sources.
Applications may include:
Large program libraries
Standard routines
Reusable production programs
Externally stored machining logic
Exact support, storage device, syntax, path handling, and behavior are controller-specific.
════════════════════════════════════════════════════════════
SECTION 20 — SUBPROGRAMS AS PROFESSIONAL CODE REUSE
════════════════════════════════════════════════════════════
Suppose the same machining sequence appears 20 times.
Beginner strategy:
Copy it 20 times.
Professional strategy:
Write it once.
Call it 20 times.
This reduces:
Program size
Editing time
Copy/paste mistakes
Maintenance burden
The principle is:
DO NOT DUPLICATE LOGIC THAT CAN BE SAFELY REUSED.
════════════════════════════════════════════════════════════
SECTION 21 — NESTED SUBPROGRAMS
════════════════════════════════════════════════════════════
A subprogram can call another subprogram on controls that support the required nesting level.
Concept:
MAIN
↓
PART ROUTINE
↓
FEATURE ROUTINE
↓
RETURN
This allows modular program architecture.
Example hierarchy:
MAIN PROGRAM
Fixture Logic
Part Logic
Hole Pattern
Machining Feature
Use nesting carefully.
Excessive nesting makes troubleshooting difficult.
════════════════════════════════════════════════════════════
SECTION 22 — MACRO VARIABLES
════════════════════════════════════════════════════════════
Macro-capable controls can replace hard-coded values with variables.
Conceptual example:
#1 = 50.
#2 = 25.
G01 X#1 Y#2
Instead of editing coordinates throughout the program, modify controlled input values.
Applications:
Part families
Pattern generators
Custom cycles
Automatic calculations
Probing
Tool management
Production automation
Macro availability and variable ranges depend on controller/options.
════════════════════════════════════════════════════════════
SECTION 23 — PARAMETRIC PROGRAMMING
════════════════════════════════════════════════════════════
Traditional program:
X10
X20
X30
X40
Parametric thinking:
START POSITION
SPACING
×
INDEX
This changes CNC programming from writing positions to describing relationships.
Generic concept:
POSITION = START + INDEX × SPACING
That formula can generate:
Hole arrays
Slots
Bolt circles
Repeated pockets
Fixture patterns
Part families
════════════════════════════════════════════════════════════
SECTION 24 — WHILE LOOP SHORTCUT
════════════════════════════════════════════════════════════
Macro-capable controls may support WHILE loops.
Conceptual logic:
SET COUNTER
WHILE COUNTER IS WITHIN LIMIT
MACHINE FEATURE
MOVE TO NEXT POSITION
INCREASE COUNTER
END LOOP
A 100-feature pattern can potentially be described with a small amount of reusable logic.
Professional rule:
Every loop must have a clearly reachable termination condition.
════════════════════════════════════════════════════════════
SECTION 25 — IF STATEMENT SHORTCUT
════════════════════════════════════════════════════════════
Conditional logic lets the program make decisions.
Concept:
IF CONDITION IS TRUE
DO ACTION
Applications:
Dimension validation
Tool-life logic
Probe-result checking
Optional operations
Part-family selection
Error detection
Custom alarms
This is where CNC code begins to behave more like conventional software.
════════════════════════════════════════════════════════════
SECTION 26 — BOLT CIRCLE FORMULA
════════════════════════════════════════════════════════════
For a bolt circle:
X = XC + R × COS(A)
Y = YC + R × SIN(A)
Where:
XC = center X
YC = center Y
R = bolt-circle radius
A = hole angle
For equally spaced holes:
ANGLE STEP = 360 / NUMBER OF HOLES
This eliminates the need to manually calculate every hole coordinate.
Example:
6 holes
Angle step:
360 / 6 = 60 degrees
Coordinates can then be generated mathematically.
════════════════════════════════════════════════════════════
SECTION 27 — LINEAR HOLE ARRAY FORMULA
════════════════════════════════════════════════════════════
For equally spaced holes:
POSITION = START + (INDEX × SPACING)
Example:
Start X = 10
Spacing = 25
Positions:
10
35
60
85
110
Instead of storing every coordinate, store:
Start
Spacing
Quantity
This is a professional pattern-programming technique.
════════════════════════════════════════════════════════════
SECTION 28 — GRID PATTERN SHORTCUT
════════════════════════════════════════════════════════════
A rectangular grid can be generated with two counters.
Outer Loop
Rows
Inner Loop
Columns
Concept:
FOR EACH ROW
FOR EACH COLUMN
MACHINE FEATURE
This is useful for:
Hole plates
Fixture plates
Vacuum tables
Electronics enclosures
Production fixtures
Nested loops can replace hundreds of manually written coordinate blocks.
════════════════════════════════════════════════════════════
SECTION 29 — PART FAMILY PROGRAMMING
════════════════════════════════════════════════════════════
Suppose the same component exists in:
50 mm
75 mm
100 mm
125 mm
versions.
Instead of maintaining four completely separate programs, a macro-capable system may use parameters such as:
PART LENGTH
PART WIDTH
HOLE SPACING
DEPTH
FEATURE COUNT
Then derive geometry from those inputs.
One controlled program can potentially support an entire family of similar parts.
════════════════════════════════════════════════════════════
SECTION 30 — CUSTOM ALARMS
════════════════════════════════════════════════════════════
Some macro-capable controls provide mechanisms for generating custom alarms.
Concept:
IF INPUT IS INVALID
STOP PROGRAM
DISPLAY MESSAGE
Example conditions:
Diameter impossible
Depth too large
Tool missing
Probe failed
Part outside expected range
Incorrect setup value
Professional programming does not only calculate valid motion.
It detects invalid conditions before motion occurs.
════════════════════════════════════════════════════════════
SECTION 31 — RANGE CHECKING
════════════════════════════════════════════════════════════
Never blindly trust variable input.
Suppose:
Expected pocket depth:
0 to 30 mm
Operator enters:
300 mm
A robust macro should detect that the value is outside the expected range.
Concept:
IF DEPTH > MAXIMUM
ALARM
IF DEPTH < MINIMUM
ALARM
Range checking converts a clever macro into a more robust production tool.
════════════════════════════════════════════════════════════
SECTION 32 — PROBING + MACRO SHORTCUTS
════════════════════════════════════════════════════════════
Probing can provide measured values to automated logic on supported systems.
Potential workflow:
PROBE PART
↓
READ RESULT
↓
CALCULATE ERROR
↓
UPDATE CONTROLLED OFFSET
↓
CONTINUE MACHINING
Applications:
Automatic setup
Part alignment
Work-offset setting
Feature verification
Tool-wear correction
Closed-loop machining
Probe macros and system variables are highly controller- and probe-package-specific.
════════════════════════════════════════════════════════════
SECTION 33 — AUTOMATIC OFFSET CORRECTION
════════════════════════════════════════════════════════════
Conceptual workflow:
TARGET = 50.000
MEASURED = 49.980
ERROR = TARGET – MEASURED
Then determine whether the error is:
Acceptable
Correctable
Abnormal
Never automatically apply unlimited corrections.
Use:
Correction limits
Measurement validation
Repeat checks
Alarm thresholds
Maximum cumulative correction
Automation should detect abnormal conditions rather than hide them.
════════════════════════════════════════════════════════════
SECTION 34 — TOOL LIFE LOGIC
════════════════════════════════════════════════════════════
Macro logic can support tool usage tracking on suitable controls.
Concept:
TOOL USED
↓
INCREMENT COUNTER
↓
COMPARE WITH LIMIT
↓
CONTINUE OR CHANGE TOOL
Possible strategies:
Parts per tool
Cutting time
Feature count
Measured wear
Process condition
A backup or sister tool may then be selected when supported by the machine automation.
════════════════════════════════════════════════════════════
SECTION 35 — OPTIONAL BLOCK SKIP
════════════════════════════════════════════════════════════
Many CNC systems support block-delete / optional block-skip functionality using lines beginning with “/” according to the control’s rules.
Possible applications:
Optional probing
Optional inspection
Optional finishing
Optional stops
Setup-only operations
This allows one program to support controlled variations without maintaining many nearly identical copies.
Verify the machine’s block-delete behavior before relying on it.
════════════════════════════════════════════════════════════
SECTION 36 — M01 AS A DEBUGGING CHECKPOINT
════════════════════════════════════════════════════════════
M01 is commonly used as an optional stop.
Useful checkpoints:
After tool change
Before critical machining
Before deep drilling
Before probing
Before a rotary move
Before finishing
During prove-out, optional stops can make complex programs easier to verify.
════════════════════════════════════════════════════════════
SECTION 37 — COMMENTS ARE A PROFESSIONAL SHORTCUT
════════════════════════════════════════════════════════════
Comments do not reduce cycle time.
They reduce human debugging time.
Weak:
T8 M06
Better:
(T8 – 10MM CARBIDE END MILL – FINISH)
Useful comments can identify:
Operation
Tool
Feature
Setup
Critical dimensions
Expected conditions
Macro inputs
Revision notes
A program should be understandable months after it was written.
════════════════════════════════════════════════════════════
SECTION 38 — SEQUENCE NUMBERS
════════════════════════════════════════════════════════════
N numbers can improve navigation and troubleshooting.
Example:
N100
N200
N300
Instead of numbering every line sequentially by 1, leaving gaps can make later edits easier.
Sequence numbers may also interact with controller-specific search, restart, local subprogram, or macro behavior.
Use a consistent shop standard.
════════════════════════════════════════════════════════════
SECTION 39 — REMOVE COPY-PASTE PROGRAMMING
════════════════════════════════════════════════════════════
If you copy the same 40 blocks ten times, you now maintain 400 blocks.
If you write one verified routine and call it ten times, you maintain one logical routine.
Before copying code, ask:
Can this become a canned cycle?
Can this become a subprogram?
Can this become a local subroutine?
Can this become a loop?
Can this become a macro?
Can this use another work offset?
Can this be expressed mathematically?
That question alone can dramatically improve CNC programming.
════════════════════════════════════════════════════════════
SECTION 40 — MODAL PROGRAMMING
════════════════════════════════════════════════════════════
Many CNC commands remain active until changed or cancelled.
These are modal commands.
Examples can include:
G01
G17
G21
G40
G43
G54
G90
G94
Professional programmers use modal behavior to reduce unnecessary code while keeping program state understandable.
Too much repetition creates clutter.
Too little explicit state creates ambiguity.
The goal is controlled clarity.
════════════════════════════════════════════════════════════
SECTION 41 — SAFE STATE RESET
════════════════════════════════════════════════════════════
Programs should establish the modes they depend upon rather than assuming the previous program left the machine in the desired state.
Common categories to establish or cancel include:
Plane
Units
Positioning mode
Canned cycle
Cutter compensation
Tool length compensation
Feed mode
Work coordinate
Exact codes depend on machine type and control.
The principle:
DO NOT DEPEND ON UNKNOWN PREVIOUS STATE.
════════════════════════════════════════════════════════════
SECTION 42 — PROGRAMMING BY OPERATION
════════════════════════════════════════════════════════════
Organize code into logical operations.
Example:
OP10
FACE
OP20
DRILL
OP30
ROUGH POCKET
OP40
FINISH POCKET
OP50
CHAMFER
OP60
PROBE
This improves:
Navigation
Debugging
Tool management
Setup sheets
Revision control
Operator understanding
════════════════════════════════════════════════════════════
SECTION 43 — MINIMIZE AIR CUTTING
════════════════════════════════════════════════════════════
Cycle-time optimization often begins outside the material.
Look for:
Excessive retract heights
Long positioning moves
Repeated unnecessary moves
Redundant tool changes
Repeated spindle starts
Unnecessary coolant changes
Poor operation order
However:
Never trade verified clearance for a few seconds of cycle time.
Optimize after safety and reliability are established.
════════════════════════════════════════════════════════════
SECTION 44 — MINIMIZE TOOL CHANGES
════════════════════════════════════════════════════════════
Tool changes can consume significant production time.
Where process constraints allow, consider grouping operations intelligently.
Instead of:
Tool 1
Tool 2
Tool 1
Tool 3
Tool 1
A better sequence may reduce repeated tool calls.
But machining quality, accessibility, thermal effects, workholding, and process requirements still control operation order.
════════════════════════════════════════════════════════════
SECTION 45 — ROUGHING VS FINISHING LOGIC
════════════════════════════════════════════════════════════
Separate roughing and finishing strategy.
ROUGHING
Goal:
Efficient material removal
FINISHING
Goal:
Final geometry
Surface quality
Tolerance
This separation makes it easier to:
Change finish allowance
Replace finishing tool
Adjust compensation
Re-run finishing
Inspect between operations
════════════════════════════════════════════════════════════
SECTION 46 — LEAVE FINISH STOCK AS A VARIABLE
════════════════════════════════════════════════════════════
Instead of burying finish allowance inside many coordinates, treat it as a controlled parameter where macro programming is appropriate.
Concept:
FINISH_ALLOWANCE = 0.2
Geometry can then reference that value.
Changing the process may require changing one value instead of dozens of coordinates.
════════════════════════════════════════════════════════════
SECTION 47 — REUSABLE FEATURE ROUTINES
════════════════════════════════════════════════════════════
Build proven routines for common features.
Examples:
Drill Hole
Peck Drill Hole
Tap Hole
Circular Pocket
Rectangular Pocket
Bolt Circle
Slot
Chamfer
Probe Bore
Probe Boss
Face Surface
Tool Check
Instead of thinking:
“I need another program.”
Think:
“I need another combination of verified machining modules.”
════════════════════════════════════════════════════════════
SECTION 48 — PROGRAM HEADER TEMPLATE
════════════════════════════════════════════════════════════
A professional header may include:
PROGRAM NUMBER
PART NUMBER
PART NAME
REVISION
MACHINE
MATERIAL
STOCK
WORK OFFSET
PROGRAMMER
DATE
NOTES
CRITICAL WARNINGS
Example:
(PROGRAM: O1000)
(PART: EXAMPLE)
(REV: A)
(MATERIAL: ALUMINUM)
(WORK OFFSET: G54)
(VERIFY TOOLS AND OFFSETS BEFORE RUN)
The exact standard should match the shop’s workflow.
════════════════════════════════════════════════════════════
SECTION 49 — TOOL COMMENT TEMPLATE
════════════════════════════════════════════════════════════
Useful tool comment:
(T01 – 12MM END MILL – ROUGH)
Instead of:
(TOOL 1)
Better documentation reduces setup errors.
Possible fields:
Tool number
Diameter
Tool type
Operation
Holder
Special requirement
════════════════════════════════════════════════════════════
SECTION 50 — G-CODE DEBUGGING SHORTCUT: READ THE STATE
════════════════════════════════════════════════════════════
When a program behaves unexpectedly, do not stare only at the failing line.
Check the active state.
Ask:
G90 or G91?
G20 or G21?
G17, G18 or G19?
Which work offset?
G40, G41 or G42?
Is G43 active?
Which H offset?
Is a canned cycle active?
Which tool?
Which feed mode?
Which spindle mode?
Many CNC bugs are state bugs.
════════════════════════════════════════════════════════════
SECTION 51 — DEBUG FROM THE CRASH LINE BACKWARD
════════════════════════════════════════════════════════════
If a suspicious movement occurs on line 500, the cause may have been established on line 100.
Look backward for:
Modal changes
Offset changes
Variable assignments
Subprogram calls
Tool changes
Coordinate-mode changes
Compensation
Macro branches
The line that moves incorrectly is not always the line that created the error.
════════════════════════════════════════════════════════════
SECTION 52 — DEBUG SUBPROGRAMS LIKE SOFTWARE
════════════════════════════════════════════════════════════
Check:
Who called the routine?
What was the entry position?
Which modes were active?
Which variables existed?
What changed inside the routine?
Where does it return?
What state exists after return?
This mental model makes M98/M99 debugging much easier.
════════════════════════════════════════════════════════════
SECTION 53 — CNC PROGRAM DIFF
════════════════════════════════════════════════════════════
When a working program suddenly stops working after an edit, compare:
OLD VERSION
vs
NEW VERSION
Look specifically for:
Changed coordinates
Changed signs
Changed decimal points
Changed tools
Changed H/D offsets
Changed feeds
Changed speeds
Changed G-codes
Changed M-codes
Changed variables
Changed subprogram numbers
One-character differences can have large physical consequences.
════════════════════════════════════════════════════════════
SECTION 54 — PROFESSIONAL G-CODE CHECKLIST
════════════════════════════════════════════════════════════
Before production verify:
Correct Program
Correct Revision
Correct Machine
Correct Work Offset
Correct Tools
Correct Tool Lengths
Correct Tool Diameters
Correct Units
Correct Coordinate Mode
Correct Plane
Correct Spindle Direction
Correct RPM
Correct Feed
Correct Z Depth
Correct Retract
Correct Canned Cycles
Correct Subprograms
Correct Macro Inputs
Correct Fixture
Correct Stock
Correct Simulation
════════════════════════════════════════════════════════════
SECTION 55 — MILLING CODE CHEAT SHEET
════════════════════════════════════════════════════════════
COMMON MOTION
G00
Rapid
G01
Linear feed
G02
CW arc
G03
CCW arc
PLANES
G17
XY
G18
XZ
G19
YZ
UNITS
G20
Inch
G21
Metric
POSITIONING
G90
Absolute
G91
Incremental
WORK OFFSETS
G54–G59
Common work coordinate systems
COMPENSATION
G40
Cancel cutter compensation
G41
Left compensation
G42
Right compensation
G43
Tool length compensation on many controls
G49
Cancel tool length compensation
DRILLING
G80
Cancel cycle
G81
Drilling
G82
Dwell drilling / counterboring on many controls
G83
Peck drilling
G84
Tapping
Always verify the exact controller implementation.
════════════════════════════════════════════════════════════
SECTION 56 — M-CODE CHEAT SHEET
════════════════════════════════════════════════════════════
M00
Stop
M01
Optional stop
M03
Spindle CW
M04
Spindle CCW
M05
Spindle stop
M06
Tool change on many machining centers
M08
Coolant on
M09
Coolant off
M30
Program end
M98
Subprogram call on many controls
M99
Subprogram return / loop behavior depending on context
Machine-specific M-codes should always be checked against the machine documentation.
════════════════════════════════════════════════════════════
SECTION 57 — SUBPROGRAM CHEAT SHEET
════════════════════════════════════════════════════════════
M98
Common Fanuc-style subprogram call
M99
Common return mechanism
M97
Local subprogram functionality on Haas controls
M198
External program/subprogram functionality on supported controls
Best applications:
Repeated features
Repeated parts
Modular programs
Pattern machining
Reusable operations
Large program organization
════════════════════════════════════════════════════════════
SECTION 58 — MACRO CHEAT SHEET
════════════════════════════════════════════════════════════
Common macro concepts include:
Variables
Arithmetic
IF
GOTO
WHILE
Comparisons
Trigonometric functions
Rounding functions
System variables
Custom alarms
Exact syntax and availability depend on the controller.
Think of macros as:
G-CODE
VARIABLES
MATHEMATICS
LOGIC
════════════════════════════════════════════════════════════
SECTION 59 — PROFESSIONAL SHORTCUT: CALCULATE, DON’T TYPE
════════════════════════════════════════════════════════════
If geometry follows a mathematical rule, calculate it.
Examples:
Bolt circles
Hole arrays
Repeated slots
Angular patterns
Part families
Variable pocket sizes
Fixture grids
The more repetitive the geometry, the stronger the case for parametric programming.
════════════════════════════════════════════════════════════
SECTION 60 — PROFESSIONAL SHORTCUT: CALL, DON’T COPY
════════════════════════════════════════════════════════════
If machining logic repeats:
CALL IT.
Do not copy it.
Subprograms reduce duplicated logic.
Macros reduce duplicated calculations.
Canned cycles reduce duplicated motion.
Work offsets reduce duplicated geometry.
The shortest professional program is not the program with the fewest characters.
It is the program with the least unnecessary duplication while remaining clear and verifiable.
════════════════════════════════════════════════════════════
SECTION 61 — PROFESSIONAL SHORTCUT: VALIDATE BEFORE MOTION
════════════════════════════════════════════════════════════
For advanced macro programs:
INPUT
↓
VALIDATE
↓
CALCULATE
↓
VALIDATE RESULT
↓
MOVE
Not:
INPUT
↓
MOVE
This distinction is critical.
Before using calculated values for machine movement, check whether they are inside expected ranges.
════════════════════════════════════════════════════════════
SECTION 62 — PROFESSIONAL SHORTCUT: ONE SOURCE OF TRUTH
════════════════════════════════════════════════════════════
Avoid storing the same critical dimension in many unrelated locations.
If possible, define important values once.
Examples:
Part Width
Part Length
Hole Spacing
Finish Allowance
Maximum Depth
Feature Count
Then derive dependent values mathematically.
When a dimension changes, fewer edits are required.
════════════════════════════════════════════════════════════
SECTION 63 — PROFESSIONAL SHORTCUT: MODULAR CNC PROGRAMMING
════════════════════════════════════════════════════════════
Think of the program as modules:
INITIALIZATION
↓
TOOL MANAGEMENT
↓
POSITIONING
↓
FEATURE ROUTINES
↓
INSPECTION
↓
ERROR HANDLING
↓
END / RETURN
This architecture scales much better than thousands of unrelated blocks.
════════════════════════════════════════════════════════════
SECTION 64 — PROFESSIONAL SHORTCUT: CONTROLLER-AWARE CODE
════════════════════════════════════════════════════════════
Never label code simply:
“CNC CODE”
when the behavior is controller-specific.
Prefer:
Fanuc-style example
Haas example
Siemens example
Heidenhain example
Controller-independent concept
This makes documentation more accurate and far more useful to programmers.
════════════════════════════════════════════════════════════
SECTION 65 — CNC CODE TRANSLATION
════════════════════════════════════════════════════════════
A professional programmer frequently encounters code written for another controller.
Do not perform blind search-and-replace conversion.
Compare:
Coordinate systems
Cycles
Subprograms
Variables
Macros
Tool calls
Offsets
M-codes
Rotary behavior
Reference returns
Program-end behavior
Machine-builder functions
Two controls may support similar concepts using different syntax or semantics.
════════════════════════════════════════════════════════════
SECTION 66 — FANUC VS HAAS PROGRAMMING
════════════════════════════════════════════════════════════
Fanuc-style and Haas programming share many familiar ISO-style concepts.
However, differences can exist in:
Local subprograms
Macros
Variables
Settings
Cycles
M-codes
Probing
Program storage
Controller options
Do not assume that a valid Haas example is automatically a valid Fanuc example or vice versa.
════════════════════════════════════════════════════════════
SECTION 67 — MILLING VS TURNING CODE
════════════════════════════════════════════════════════════
The same G-code number can have different relevance or meaning depending on machine type and control.
Turning introduces concepts such as:
Diameter programming
Constant surface speed
Feed per revolution
Tool nose radius compensation
Turning canned cycles
Threading cycles
Milling introduces:
Tool length compensation
XY contour compensation
Drilling cycles
Multiple work offsets
Therefore, every code reference should identify whether it applies to:
MILLING
TURNING
or
BOTH
════════════════════════════════════════════════════════════
SECTION 68 — LATHE PROFESSIONAL SHORTCUTS
════════════════════════════════════════════════════════════
Common turning productivity concepts include:
Roughing cycles
Finishing cycles
Grooving cycles
Threading cycles
Constant surface speed
Feed per revolution
Tool nose compensation
Subprograms
Macros
Exact cycle numbers and parameters can vary substantially between controls.
Always separate generic turning principles from controller-specific syntax.
════════════════════════════════════════════════════════════
SECTION 69 — G96 / G97 TURNING SHORTCUT
════════════════════════════════════════════════════════════
On many turning controls:
G96
Constant surface speed
G97
Constant RPM
Constant surface speed can automatically vary spindle RPM as cutting diameter changes.
A maximum spindle-speed limit is an important part of safe CSS programming where applicable.
Verify controller syntax and chuck/workholding limitations before use.
════════════════════════════════════════════════════════════
SECTION 70 — FEED PER REVOLUTION
════════════════════════════════════════════════════════════
Turning operations commonly benefit from feed-per-revolution programming.
Instead of feed being tied directly to time, feed can be related to each spindle revolution.
This can help maintain more consistent cutting behavior as spindle speed changes.
Again, confirm the feed-mode code for the exact controller.
════════════════════════════════════════════════════════════
SECTION 71 — PRODUCTION PROGRAM ARCHITECTURE
════════════════════════════════════════════════════════════
A scalable production program can be conceptualized as:
MAIN PROGRAM
↓
SETUP VALIDATION
↓
PART SELECTION
↓
TOOL SEQUENCE
↓
FEATURE SUBPROGRAMS
↓
PROBING / INSPECTION
↓
TOOL MANAGEMENT
↓
PART COUNTER
↓
END
This transforms a CNC program from a motion file into a controlled production workflow.
════════════════════════════════════════════════════════════
SECTION 72 — CNC PROGRAMMING GOLDEN PATTERNS
════════════════════════════════════════════════════════════
PATTERN 1
DEFINE → CALL → RETURN
Use for repeated machining logic.
PATTERN 2
INPUT → VALIDATE → CALCULATE → EXECUTE
Use for macros.
PATTERN 3
MACHINE → MEASURE → CORRECT → VERIFY
Use for closed-loop machining.
PATTERN 4
DETECT → STOP → EXPLAIN
Use for abnormal conditions.
PATTERN 5
START + INDEX × SPACING
Use for linear arrays.
PATTERN 6
CENTER + RADIUS × TRIGONOMETRY
Use for circular patterns.
PATTERN 7
ONE PROGRAM + PARAMETERS
Use for part families.
These patterns are more valuable than memorizing isolated codes.
════════════════════════════════════════════════════════════
SECTION 73 — BEGINNER CODE VS PROFESSIONAL CODE
════════════════════════════════════════════════════════════
BEGINNER
Copy
Copy
Copy
Edit coordinates
Hope nothing was missed
PROFESSIONAL
Define
Parameterize
Validate
Reuse
Simulate
Verify
BEGINNER
Writes coordinates.
PROFESSIONAL
Models relationships.
BEGINNER
Repeats code.
PROFESSIONAL
Builds reusable logic.
════════════════════════════════════════════════════════════
SECTION 74 — SPEED IS NOT ONLY CYCLE TIME
════════════════════════════════════════════════════════════
Professional programming optimizes total manufacturing time.
TOTAL TIME includes:
Programming
Setup
Verification
Machining
Inspection
Tool changes
Debugging
Rework
Maintenance
A program that saves 20 seconds per part but requires hours of debugging may not be an optimization.
Optimize the entire process.
════════════════════════════════════════════════════════════
SECTION 75 — THE PROFESSIONAL CNC PROGRAMMER’S CHECKLIST
════════════════════════════════════════════════════════════
Before releasing a program:
Is the controller identified?
Is the machine identified?
Is the program revision correct?
Are units explicit?
Is positioning mode explicit?
Is the correct plane established?
Are compensation states controlled?
Are work offsets correct?
Are tools documented?
Are offsets correct?
Are subprograms verified?
Are macro inputs validated?
Are loops guaranteed to terminate?
Are calculated positions bounded?
Are cycles cancelled correctly?
Are retracts safe?
Are rapid paths safe?
Is fixture clearance verified?
Is holder clearance verified?
Has posted NC been simulated where appropriate?
Has the actual setup been verified?
Has the program been proved out using appropriate procedures?
════════════════════════════════════════════════════════════
SECTION 76 — HIGH-VALUE CNC CODE SEARCH INDEX
════════════════════════════════════════════════════════════
G00 CNC Code
G01 CNC Code
G02 CNC Code
G03 CNC Code
G17 CNC Code
G18 CNC Code
G19 CNC Code
G20 CNC Code
G21 CNC Code
G28 CNC Code
G40 CNC Code
G41 CNC Code
G42 CNC Code
G43 CNC Code
G49 CNC Code
G53 CNC Code
G54 CNC Code
G55 CNC Code
G56 CNC Code
G57 CNC Code
G58 CNC Code
G59 CNC Code
G73 CNC Code
G80 CNC Code
G81 CNC Code
G82 CNC Code
G83 CNC Code
G84 CNC Code
G90 CNC Code
G91 CNC Code
G94 CNC Code
G95 CNC Code
G96 CNC Code
G97 CNC Code
M00 CNC Code
M01 CNC Code
M03 CNC Code
M04 CNC Code
M05 CNC Code
M06 CNC Code
M08 CNC Code
M09 CNC Code
M30 CNC Code
M97 CNC Code
M98 CNC Code
M99 CNC Code
M198 CNC Code
CNC Subprogram Examples
CNC Macro Examples
Fanuc Macro Programming
CNC Programming Shortcuts
CNC Programming Tricks
G-Code Examples
M-Code Examples
CNC Code Examples
════════════════════════════════════════════════════════════
SECTION 77 — TURN THIS PAGE INTO A CNC PROGRAMMING HUB
════════════════════════════════════════════════════════════
Each major code on this page can link to a dedicated deep guide.
Examples:
/g00-cnc-code/
/g01-cnc-code/
/g02-g03-cnc/
/g28-cnc/
/g43-tool-length-compensation/
/g54-work-offset/
/g81-drilling-cycle/
/g83-peck-drilling/
/g84-tapping-cycle/
/g90-vs-g91/
/m98-cnc/
/m99-cnc/
/m98-m99/
/m198-cnc/
/cnc-subprograms/
/fanuc-macro-variables/
/cnc-macro-programming/
/g-code-simulator/
/g-code-checker/
The hub explains the entire system.
Dedicated pages capture specific search intent.
════════════════════════════════════════════════════════════
SECTION 78 — INTERACTIVE TOOL LAYER
════════════════════════════════════════════════════════════
The strongest version of this page does not stop at text.
Add:
G-Code Search
G-Code Simulator
G-Code Explainer
G-Code Error Checker
Modal State Analyzer
M-Code Search
M98/M99 Debugger
Bolt Circle Generator
Hole Pattern Generator
G90/G91 Converter
G02/G03 Arc Calculator
G83 Generator
G84 Feed Calculator
Macro Variable Explorer
CNC Program Diff
Feed & Speed Calculator
Every tool should link back to the relevant educational section.
Every educational section should link to the relevant tool.
════════════════════════════════════════════════════════════
SECTION 79 — THE 10-SECOND CNC CODE LOOKUP
════════════════════════════════════════════════════════════
A professional reference should let a visitor search:
CODE
Example:
G83
and immediately see:
NAME
Deep-Hole Peck Drilling
CATEGORY
Canned Cycle
COMMON USE
Deep drilling
MODAL?
Controller-dependent cycle behavior
COMMON PARAMETERS
Z
R
Q
F
CONTROLLER NOTES
Behavior and available parameters vary.
EXAMPLE
Controller-specific verified example.
RELATED
G73
G80
G81
G82
G84
This structure can be repeated for every supported code.
════════════════════════════════════════════════════════════
SECTION 80 — FINAL PRINCIPLE
════════════════════════════════════════════════════════════
Professional CNC programming is not about writing more code.
It is about expressing machining intent with less duplication, fewer opportunities for error, and clearer control over machine state.
The most powerful shortcuts are not secret G-codes.
They are programming principles:
USE CANNED CYCLES FOR REPEATED STANDARD OPERATIONS.
USE SUBPROGRAMS FOR REPEATED LOGIC.
USE WORK OFFSETS FOR REPEATED LOCATIONS.
USE VARIABLES FOR CHANGING VALUES.
USE FORMULAS FOR PATTERNS.
USE LOOPS FOR REPETITION.
USE CONDITIONS FOR DECISIONS.
USE RANGE CHECKS BEFORE CALCULATED MOTION.
USE COMMENTS FOR HUMAN READABILITY.
USE SIMULATION FOR VERIFICATION.
USE MODULAR PROGRAMS FOR MAINTAINABILITY.
USE CONTROLLER-SPECIFIC DOCUMENTATION FOR FINAL AUTHORITY.
The progression is:
G-CODE
↓
CANNED CYCLES
↓
SUBPROGRAMS
↓
MACROS
↓
PARAMETRIC PROGRAMMING
↓
PROBING
↓
AUTOMATIC CORRECTION
↓
PROCESS AUTOMATION
A beginner writes every movement.
An experienced programmer reuses proven patterns.
An advanced programmer parameterizes them.
A production programmer validates them.
And a modern CNC system connects those patterns with measurement, monitoring, and automation.
WRITE LESS DUPLICATED CODE.
MAKE MACHINE STATE EXPLICIT.
VALIDATE VARIABLES.
REUSE PROVEN LOGIC.
SIMULATE.
VERIFY.
THEN MACHINE.
Leave a comment