Do shorter variable names result in smaller code (hex)

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Do shorter variable names result in smaller code (hex)

Post by daveb0360 »

Hi,
This WILL seem like a silly question to most but hey !.....I'm dumb.

If I use shortened variable names in my flowcode file, will this reduce final size of hex code? Logic tells me it should but my logic is not usually Spock like.

Dave

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Do shorter variable names result in smaller code (hex)

Post by Benj »

Hi Dave,

No questions are silly or dumb, always best to ask if you are unsure :D

A variable name, macro name etc are basically just markers for the compiler. The compiler takes these human understandable markers and converts them into machine markers or memory addresses. Therefore the variable name has no effect on the size of the code. This is exactly the same way as a internet URL works. The web address is a human representation of an IP address, the local DNS server provided by your ISP is used to translate from the human representation to machine representation.

This is why decompilers that convert machine code back into a high level language like C struggle, they can not know what the original meaningful variable and function names were which makes the output code hard to understand.

Best practise is to use variable and macro names that tell you exactly what the variable or macro is doing. This allows the code to be more understandable should you ever need to come back to the code and make edits.

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Why can't I use 'readasvoltage' openly, as is, literally?

Post by daveb0360 »

Thanks Ben, Fully understood.

Another question though....something I'm struggling to get my head around.
Using the fiollowing assumptions.
My program does not need to run quickly or be accurate with timings. But it does need to be accurate and consistent in reading voltages from resistive dividers.

I want to read the voltage at a thermistor and use it to perform various decisions. eg. a thermostat.
FCv4 has an operator called 'readasvoltage'.
Why can't my program read as voltage and use this voltage, as read, to perform these decisions? Every example almost in the forum, has coders converting the voltage read to integers, bytes etc and using c code to read lookup tables......which I just can't grasp and such methods only interpret approximations or rounded numbers.

why can't I just say.....simplified....and ignoring syntax etc..
Read AN0, Readasvoltage,
If voltage <=2.43 then LED=1
if voltage >=3.02 then LED=0

It's a very loose example I know but I hope you see what I am asking. I am an electronics engineer who thinks in volts.......not bytes and integers and the mental effort that is going into all the other peripheral maths is confounding me.

Dave

dazz
Posts: 1314
Joined: Sun Jan 15, 2012 3:15 pm
Has thanked: 474 times
Been thanked: 458 times
Contact:

Re: Do shorter variable names result in smaller code (hex)

Post by dazz »

Hi Dave

Not sure if this helps but click on the adc component, then look at the pic below and click on the help bit arrowed and there is an explanation of read as voltage

Regards
Dazz
Untitled.png
(245.16 KiB) Downloaded 952 times
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Do shorter variable names result in smaller code (hex)

Post by Benj »

Hi Dave,

The Read as Voltage is mainly used for display purposes but could also be used for decisions. Basically the read as byte and read as int use simple mathematics the chip can process very fast. Floating point variables are different in that they need much larger processing time because they are essentially 32-bit numbers and the processor can only handle 8-bit at any one time. Therefore using these can needlessly bloat your code if you can get away without using them.

A simple conversion from byte or int maths is as follows.

VCC voltage - assuming 5V or 3V3

8-bit ADC (BYTE) - Voltage = (VCC / 256) * ADC reading
10-bit ADC (INT) - Voltage = (VCC / 1024) * ADC reading
12-bit ADC (INT) - Voltage = (VCC / 4096) * ADC reading

The bit depth of the read as int function depends on the maximum ADC bit depth for your microcontroller.

You can do decisions based on floating point variables like this for the PIC (other families should allow float decisions directly via the decision icon).

Variables
flt1 - Float
result - Byte

Component Macro
flt1 = ADC_read_as_voltage

Calculation Icon
flt1 = 0.45
result = float_gt(flt1, 2.4)

Decision Icon
result

Using just the variable name returns yes if the variable is set to anything other then 0.

Here is a full list of the comparison functions available.
float_eq - is float equal
float_ge - is float greater then or equal
float_gt - is float greater then
float_le - is float less then or equal
float_lt - is float less then

It might end up easier to do the ADC reading as a byte or int, do all your decisions and processing and then if you need to display the voltage then perform the calculation above into a float variable and print this out.

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Re: Do shorter variable names result in smaller code (hex)

Post by daveb0360 »

Hi Guys,
So, because all my variables have a known value, apart from the measured ones. I have compiled a small spreadsheet to calculate the 10bit integer result for a real world value as shown below.
This will enable me to enter the known values as integers within the variables and perform decisions based on measured voltages - v- known voltages expressed in their integer form.

Maybe this is the long way round but it enables me to calculate the known values from external spreadsheets and simulations without using up iternal coding and calculations within the flowcode.

Hopefully, I am on the right track...


Dave
Attachments
Integer calc.PNG
(6.74 KiB) Downloaded 3064 times

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Voltage to 10bit integers - Method

Post by daveb0360 »

So,
In short, I have attached the spreadsheet for the 2k2 Thermistor I am using which shows the 10bit integer for the calculated temperatures.

This gives, within the range I am most interested in (30 - 50c) a useable resolution of approximately 0.1degC (as opposed to 0.4degC if I used an 8bit byte.) with which to set my target points.

I sure hope I'm right here.
Somebody tell me if I'm on the wrong track please.

Dave
Attachments
Setpoint Calcs.zip
(161.12 KiB) Downloaded 219 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Do shorter variable names result in smaller code (hex)

Post by Benj »

Looks good to me Dave :D

daveb0360
Posts: 139
Joined: Mon Sep 21, 2009 10:17 am
Location: Leicester
Has thanked: 35 times
Been thanked: 12 times
Contact:

Re: Do shorter variable names result in smaller code (hex)

Post by daveb0360 »

Here's an updated spreadsheet others may find useful.
I have equipped it with a front page where variables can be entered and the corresponding integer value returned.
for byte variables, change the value '1024' to 255 and a corresponding byte variable will result. Sure it's obvious to most and most could customise to suit.

Please distribute if it's useful

Dave
Attachments
Setpoint Calcs.zip
(167.53 KiB) Downloaded 224 times

Post Reply