|
B.1 Variables, Data Types, Database Access, Declaring Variables, and Comments
|
First and foremost, have fun when programming with sheerpower. It is fast.
It is intuitive.
Variable names
User variable names must start with a letter and can include letters,
numbers, the underscore ("_"), and an embedded dollar sign ("$"). Names are
case-regardless. This means that
Account is the same as
AccOUNT. If a variable name consists of multiple words, by convention
each word is separated by an underscore.
Primitive Data Types
- Real -- A number having up to 18 digits to the left of the decimal and 16 to the right.
To support business applications, sheerpower uses
Perfect Precision Math
which eliminates
rounding errors
when performing simple math operations.
When comparing two REAL values in sheerpower, the advantage lies in its Perfect Precision Math, which
ensures exact arithmetic operations. This precision eliminates common floating-point errors seen in other languages,
resulting in accurate comparisons. This is particularly beneficial for business applications where even
minor discrepancies can have significant financial implications. The high precision and consistent results provided by
sheerpower's REAL values make it a reliable choice for critical business computations.
- String -- Characters, such as a person's name, city name, and also arbitrary binary 8-bit data.
- Integer -- A whole number without fractional digits.
- Boolean -- A logical of true (1) or false (0). Used in conditional statements.
- Dynamic -- Can be any data type. Adjusts its data type according to use.
- Database access -- expressed by table_name(field_name)
Of these, the most commonly used data types are REAL, STRING, and BOOLEAN --
in that order. For business applications, INTEGER and DYNAMIC data types are
less frequently used and rarely even needed. The default data type is REAL.
All database access in SheerPower is managed using the
table_name(field_name) syntax. For example, the statement
print payroll(salary) retrieves the current record from the
payroll table, locks it if necessary, extracts the
salary field, and prints its contents.
If you assign a value to a database field, as in
payroll(salary) = 1234.56, SheerPower retrieves the current record
from the payroll table, locks it, and stores the value
1234.56 into the salary field. The modified record will then
be written back to the database and unlocked using a "lazy write" mechanism.
This method ensures the integrity of the table by delaying the actual
database write operation until it is most efficient.
SheerPower’s database access syntax is immune to SQL injection attacks
because it avoids direct SQL manipulation by abstracting table access behind
this syntax. For further details, visit the relevant section
here.
Dynamic Adjustment of table_name(field_name)
In SheerPower, the treatment of table_name(field_name)
is
dynamic and adjusts based on the context in which it is used. This
flexibility allows developers to handle different data types without the
need for explicit conversions, simplifying code and reducing potential
errors.
-
Numerical Context:
When a field from a database table is used in a numerical context,
such as in mathematical operations, SheerPower automatically
interprets and converts the field's value to the appropriate numeric
type. This makes it easy to perform calculations on database fields
without requiring any manual type conversions.
mysalary = 1.5 * payroll(salary)
In this case, payroll(salary)
retrieves the
salary
field from the payroll
table. The
value is automatically converted into a number, allowing it to be
multiplied by 1.5
seamlessly. This dynamic conversion
eliminates the need for explicit type management in mathematical
operations.
-
String Context:
When a field is used in a string context, SheerPower dynamically
converts the value to a string. This is particularly useful when
concatenating or formatting data from the database with other
strings.
mysalary$ = 'my ' + payroll(salary)
Here, payroll(salary)
retrieves the salary
field from the payroll
table, and the value is
automatically treated as a string for concatenation with the text
'my '
. The result, mysalary$
, will hold the
concatenated string without needing explicit conversion.
-
Advantages:
- Seamless Type Handling: SheerPower
dynamically adjusts the type of the database field based on its
usage context, reducing the need for manual type conversion and
simplifying the code.
- Context-Aware Operations: Whether the field
is used in mathematical calculations or string operations,
SheerPower intelligently determines how to handle the field,
ensuring that it behaves correctly in both contexts.
- Efficient Database Interactions: The
table_name(field_name)
syntax provides a clean and
efficient way to access and modify database fields, with built-in
protections against SQL injection attacks, further enhancing
security and performance.
This dynamic type conversion, coupled with the ease of use of
table_name(field_name)
, allows developers to focus more on
business logic rather than handling cumbersome type declarations or
conversions.
Variables with a dollar sign "$" suffix default to being a
string. Those with
a percent sign "%" suffix default to being an
integer. And those with a
question mark "?" suffix default to being a
boolean. Unsuffixed, undeclared
variables are assumed to be declared as
real.
print 1.2 - 1.0
tax_rate = 5.5
fullname$ = 'Sally Sue'
is_wonderful? = true
counter% = 50
declare dynamic answer
declare real inches
declare string address
declare integer frogs
declare boolean is_great
inches = 55.6
print inches
Defining Constants
You can also define constants in your code. Constants cannot be accidently changed once defined.
const speed_of_light_mps = 299_792_458
You can also require that all variables be declared. In this case, undeclared
variables will cause compile-time errors.
option require declare
Declaring Default Output Formatting
There are some variables that when printed you always want
specific formatting to be used. For example, with a variable that
contains cash, every time it is
printed, you might want to include a dollar sign, commas, and also rounded
to two decimal digits.
For details on print formats, see the
SPRINTF() function
declare format '$%.2m' cash
cash = 123456.7
print 'The value is: ';cash
The value is: $123,456.70
Custom Data Types
A custom data type is defined by:
TYPE typename datatype [format 'format string']
After being defined, custom data types can be utilized in
place of any primitive data type. If you declare a variable with a
custom data type, any attempt to assign a different custom data type
to that variable will generate a compile-time error. Custom data types
also improve the readability of your code, making it easier to
maintain in the long run.
type money format '$%.2m'
declare refund money
refund = 123456.728
print 'Your refund is: ';refund
>> Your refund is: $123,456.73
type miles
declare distance miles
distance = 45
// The code below generates a compiler error
// because refund is a type of money and distance is a type of miles
refund = distance
Comments in your code
Comments in your code should explain the reasoning behind specific logic.
sheerpower ignores all comments since they are intended for your
reference and for future developers who need to understand your code.
Comments are prefixed with either !
or //
! this is a comment
// this is a comment