Good Day everyone!
Is it possible to make commands or codes shorter ?
Example, I want to make Serial.println
shorter.
Like this: "o" = Serial.println
so I can put o("Printed text");
instead of Serial.println("Printed Text");
Is there a way to shorten this?
Read up on macros. As this sounds like lazyness (no offense intended), be aware that there is a risk that your code is less readable, especially with single charater macros.
Below is not tested
#define PRINTLN(x) Serial.println(x)
Some more info about preprocessor directives
Extract:
Even though the most common use of of #define directive is to create named constants, it can do much more than just that. For example, let’s say you want to know which of two given numbers is smaller. You could write a function that will do just that.
int min(int a, int b) {
if(a < b) {
return(a);
}
return(b);
}
Or in a simpler way with a ternary operator:
int min(int a, int b) {
return((a < b) ? a : b);
}
However, both of these functions will be compiled and will take up precious program storage space. We can achieve the same effect with the following function-like macro, that will take much less program space.
#ifndef MIN
#define MIN(A, B) (((A) < (B)) ? (A) : (B))
#endif
Now, each occurrence of “MIN(A, B)”, will be replaced with “(((A) < (B)) ? (A) : (B))” where “A” and “B” can be either a number, or a variable. Notice that the #define is wrapped in the same protective construct that prevents user from defining the macro twice.
When creating macros, you have to keep in mind that once again, they are processed as text. That’s why in the definition above, pretty much everything is wrapped in brackets. Try and guess the result of the following operation.
#ifndef MULTIPLY
#define MULTIPLY(A, B) A * B
#endif
//some code...
int result = MULTIPLY(2 - 0, 3);
The value of result should be 6, since 2 – 0 is 2 and 2 * 3 is 6 right? What if I told you, that the result will be 2? The following is what actually gets compiled:
int result = 2 - 0 * 3;
Since multiplication has priority over subtraction, it is obvious now that the result has to be 2, because 3 * 0 is 0 and 2 – 0 is still 2. The correct version will look like this:
#ifndef MULTIPLY
#define MULTIPLY(A, B) ((A) * (B))
#endif
3xpLoiT_PH:
Good Day everyone!
Is it possible to make commands or codes shorter ?
Example, I want to makeSerial.println
shorter.
Like this:"o" = Serial.println
so I can puto("Printed text");
instead ofSerial.println("Printed Text");
Is there a way to shorten this?
Yes, but why? Why make the code hard to read and maintain? There is no valid reason to do this outside of being lazy.
Romonaga:
Yes, but why? Why make the code hard to read and maintain? There is no valid reason to do this outside of being lazy.
Amen
In addition to the reasons not to do this given above, if you do this and need help with your code, far fewer people will be prepared to unscramble it to see what the issue is.
Perhaps the OP believes, obviously incorrectly, that the fewer characters in the source code, the smaller the executable? It seems to be a common belief. Even at least one reply in this thread suggests the same thing...
Regards,
Ray L.
Sorry mates, I'ts not being lazy. What I'm trying to do is to make my project use less bytes.
3xpLoiT_PH:
Sorry mates, I'ts not being lazy. What I'm trying to do is to make my project use less bytes.
Your proposed approach won't do that. Unless you means bytes of source code on your PC's hard drive. But, trying to do that is pointless as disk space is essentially free.
3xpLoiT_PH:
Sorry mates, I'ts not being lazy. What I'm trying to do is to make my project use less bytes.
Doing what you are suggesting will not reduce the number of bytes, in some cases, it can actually use more.
RayLivingston:
Perhaps the OP believes, obviously incorrectly, that the fewer characters in the source code, the smaller the executable? It seems to be a common belief. Even at least one reply in this thread suggests the same thing...Regards,
Ray L.
Yep, you were spot on!
The stored ino file might be shorter but the executable will be the same size.
Compiling consists of a few steps. Before the actual compile is done, the code is scanned for defined names (PRINTLN in the example I gave) and they will all be replaced by what you defined (Serial.println). No saving whatsoever.
lesept:
However, both of these functions will be compiled and will take up precious program storage space. We can achieve the same effect with the following function-like macro, that will take much less program space.
Macro expands program space, not shrink it. In theory, macro is faster than function because you don't do address look-up.
Are you running short of program storage space, or dynamic memory? You can save on dynamic memory in the print statement by using the F () function, which causes the compiler to store the text in program storage.
Serial.println(F("Printed text"));
arduino_new:
Macro expands program space, not shrink it. In theory, macro is faster than function because you don't do address look-up.
Nonsense. Macros are simple character substitutions. Perhaps you're thinking of the 'Inline' compiler directive?
gfvalvo:
Nonsense. Macros are simple character substitutions. Perhaps you're thinking of the 'Inline' compiler directive?
inline is basically c++ macro. They do the same thing which results in a larger executable code == program space.
arduino_new:
inline is basically c++ macro. They do the same thing which results in a larger executable code == program space.
A compiler directive is NOT a macro. The former is an instruction to the compiler. The latter is operated on by the pre-processor.
gfvalvo:
A compiler directive is NOT a macro. The former is an instruction to the compiler. The latter is operated on by the pre-processor.
"is basically c++ macro" in this sense it's better, I don't mean they are literally the same.
Still they both expand program space.