« first day (870 days earlier)      last day (3157 days later) » 

11:06 PM
Isn't #define just wonderful?
#define SOME_CONSTANT 10

int main()
{
    // Compiles to 'int 10 = 10', I believe
    int SOME_CONSTANT = 10;
}
This is what happens:
main.cpp: In function 'int main()':
main.cpp:5:23: error: expected unqualified-id before numeric constant
 #define SOME_CONSTANT 10
                       ^
main.cpp:9:9: note: in expansion of macro 'SOME_CONSTANT'
     int SOME_CONSTANT = 10;
Have fun debugging that in production code.
This is why we don't use #define anymore kids
Or maybe even better, some beginner decides to do this instead:
#define SOME_CONSTANT 10

int main()
{
    // Compiles to 'int 10 = 10', I believe
    SOME_CONSTANT = 10
}
And ends up with this:
main.cpp: In function 'int main()':
main.cpp:10:19: error: lvalue required as left operand of assignment
     SOME_CONSTANT = 10
                   ^
Without probably knowing what it means
/me rants
/me done
 
@EthanBierlein It can be useful in some cases, like small one-lineish function-sh things. An example would be #define IS_ODD(n) n & 1
Or #define ASCII(n) n + '0'
 
Yeah, but it shouldn't be used when it's purpose is to be used as a "variable".
Some nasty stuff can happen.
 
Or #define lerp(t, x, y) ((1-t)*x + t*y)
 
I still say #define is a messy way of doing things
 
This is, however, prone to errors.
 
11:15 PM
exactly
 
What would happen if you did lerp(0.7f, x + 5, 9)?
 
preprocesser directives, especially #define are often somewhat dangerous.
@EBrown bad things
I'm assuming
 
You end up with the compiler seeing (1 - 0.7f)*x + 5 + 0.7f * 9.
Which is not what you wanted.
 
#define messy #define
 
Of course, you can fix this in the lerp definition:
 
11:17 PM
@Mat'sMug You want errors? Try:
#define if while
 
#define lerp(t, x, y) ((1 - (t))*(x) + (t)*(y))
 
@EthanBierlein oh that's evil
 
oh yeah
 
I wish #define were more expandable in C#.
More than once have I seen myself writing a function for something that can easily be inlined.
 
11:18 PM
@EBrown cough anonymous functions
 
Of course, with .NET 4.5+ you have the MethodImplOptions.AggressiveInlining which helps with this issue.
So now you can do: [MethodImpl(MethodImplOptions.AggressiveInlining)] on a method which will attempt to inline the method implementation wherever possible.
Which, I suppose, is almost as good as #define.
 
It's certainly less error-prone
 
However, it still has overhead.
I much prefer my #define lines.
 
I much prefer no #define lines
 
What about when you need debug conditions?
 
11:23 PM
Well, in C# you'd need to use #define, simply because it can be top-level
but in other languages, for example, C++, you could just do:
 
@EBrown compile-time overhead, no?
 
const int DEBUG = 0;
 
#if !DEBUG
    try
    {
#endif
        //code
#if !DEBUG
    }
    catch
    {
    }
#endif
 
At the global level
 
@Mat'sMug It has runtime overhead.
 
11:24 PM
Hmm
 
@EthanBierlein Except in #define can't actually define a value on a variable, only that the variable exists.
You can't do #define Thing 1 or #define Thing = 1.
 
@EBrown Some IDEs do things like this where they will purposely set #define DEBUG ... when you are using the debug function of the IDE in the case that you need to do something different with your code.
 
@SirPython Visual Studio does so.
But without the #define directive, you could never do that code I just showed.
 
See, I just wish that C# supported top-level variables so that there'd be no need for this #define nonsense, like this:
using System;

int DEBUG = 0;

public class Test
{
	public static void Main()
	{

	}
}
 
@EthanBierlein C# is not a functional language, which is probably why it doesn't support it.
 
11:26 PM
I know that, but it's still fairly useful in some cases
 
Huh, Globals FTW??
 
@Mat'sMug No, a global singleton that is static FTW.
 
Geez, how many OOP anti-patterns can you cram into one sentence?! lol
 
public static class Constants
{
    public static const int MaxClient = 6;
}
@Mat'sMug As many as I can cram into 4 lines of code. ;)
 
Make that a static class
 
11:31 PM
@Mat'sMug My bad.
Declare that at the root level of the namespace and life is good. :)
 
Remove the property and make it a const variable
#uppedyouall
3
#jeroen4life
3
 
There we go.
 
Because it's a constant, you don't need validation
It's in a class called Constants
#2cool4school
3
 
Max anti-patterns in one little section.
 
11:33 PM
Missing comment "//happy maintenance suckers"
 
Also, I've pulled code worse than this from code I've had to deal with before.
 
That doesn't entitle you to write even worse code ;-)
2
 
@Mat'sMug YES IT DOES
I am entitled to use all the anti-patterns.
2
 
Meh, I know that I explain things badly sometimes..... but, thanks, for nothing, BRZ
 
static const is pointless
 
11:35 PM
^^
 
I think we need to intervene @EBrown
 
@rolfl Is that sentence even coherent?
I will not submit to an intervention.
 
@EBrown as far as I can tell, no, no and no.
 
Probably not.... but, my torrent is just down... so.... Samoa vs. USA ... we will see who wins
 
That entire comment is one run-on run-on run-on sentence.
 
Zak
11:37 PM
So I just logged on 60 seconds ago and I went through 12 stars :)
4
 
@JeroenVannevel Also, I forgot to get the "V-anti-pattern" in there.
 
the wot?
2
 
@Zak reload is imminent, it's sprinklin' time
 
if (condition)
{
    if (condition2)
    {
        if (condition3)
        {
            while (condition4)
            {
                for (;;)
                {
                }
            }
        }
    }
}
The "V-anti-pattern."
 
It's called arrow code
 
11:39 PM
Right, well I always used "V" for it, but that works too.
 
bbl
 
@Mat'sMug It's called migratory code. It's migrating right for the winter.
 
Zak
Anyway, TTGTB, see you guys in a few hours
 

« first day (870 days earlier)      last day (3157 days later) »