|
In the C++ community there is certain unhappy feeling towards the use of MACROs.
In a post someone was given the advice to use a inline function instead of a MACRO.
This is certainly good advice.
Now my question is the following:
Are there any reasons/issues or other situations where a MACRO can be preferred over another solution.
Or should one 'try' to find a better C++ approach.
One thing could be to decrease the amount of code, so it becomes more readable and less complex.
Any thoughts on this?
codito ergo sum
|
|
|
|
|
BadKarma wrote: One thing could be to decrease the amount of code, so it becomes more readable and less complex.
No, that is hiding complexity, not decreasing it. MACROS are a bad substitute for redesigning bad, unnecessarily complex code. They also make debugging more difficult. I can't think of any situation where a MACRO would be the preferred solution.
|
|
|
|
|
The only time that it's acceptable is if you are deliberately attempting to create bad code where you are the only person who can maintain it. Hmmm - oh wait - that's the sign of a really bad app.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Actually MACROs are widely used in the ATL library, that is no such bad design example.
MACROs are also used in MFC, and, for instance, to make more readable operations on ListView, ecc..., DirectX often hides COM complexity using macros.
MACROs are hazardous of course and there are arguments against their use. But by no means MACROs are the origin or the signature of a bad design.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
ASSERT & HIWORD/LOWORD are just two examples that came to mind.
I certainly wouldn't use a inline function for either of those.
I'm finding the only constant in software development is change it self.
|
|
|
|
|
The only time I would see as an advantage of using macros is when programming in C (I haven't used C++), an instance of this was some code I wrote for a company who shall remain anonymous where the biggest emphasis was on speed because it was performing rainflow analysis of vast quantities of data, all on a micro. God forbid but most functions were eliminated in favour of copy and paste and gotos were used because the original idea was then to use an FPGA to do the analysis, i.e. perform it in hardware so because they don't have a concept of functions etc these were copied in and gotos were used because they're nice and fast, especially in hardware.
In the end the other company decided to use a micro and refactored back my optimisations, then hit a problem, it was taking 4 times too long. I was called in and noticed they were doing stupid things like calling these tiny little functions, if it the micro had been able to manage it it would have had to call this routine 1,000 / second (note this was only a tiny part of the entire, complex algorithm) so I changed the function into a macro so it "looked" like a function to them but actually wasn't.
Of course I guess this is exactly what inline functions are for in C++ but most if not all C compilers don't support them. So no I can't think of any good reason
|
|
|
|
|
In general macros are bad and should be avoided. I don’t believe this “macros are bad full stop” line however; macros have their uses. And I'm not alone: The Boost libraries are highly acclaimed C++ libraries and they use macros when needed; Andrei Alexandrescu says he’s not afraid to use macros here[^].
Steve
|
|
|
|
|
Macros are processed at pre-compile time, so they serve a purpose of course. Think the ASSERT in MFC. Can't do that with inlined functions. Well you could, but what a waste.
MACROS some might argue, are bad practice. Personally, there have been many times while programming in JS or PHP where I have considered running my code through a pre-processor to gain the benefits of macro expansion.
Ask any interpreted language developer with experience in C/C++ and they'd likely agree.
When you program OOP, everything is an object, at least in a purists eyes. Using inline functions would seem messy and pointless. Are you going to throw all generic functions inside a class named CGeneric and call them statically?
That would be hack, IMHO.
In those cases where you need some simple code, that is common in more places than a single class, Macros are a good thing. Most Macros that I have used, such as LOWORD, etc...make sense as a macro, not an inlined function...
So to answer your question, no Macros are not evil, the developers who use them inappropriately are.
There are times when I write code, that is clean and effective but could be slightly more elegant or efficient if I could use a GOTO (no longer the case in most languages). I think macros, globals, goto's and even multiple inheritence can still be useful, but their use has to be completely understood and justified, otherwise they promote bad coding practice and you end up with crap.
Then again, I've seen purist OO code with-out macros, globals, goto, or MI and it was crap too.
Cheers
I'm finding the only constant in software development is change it self.
|
|
|
|
|
When the intention is to abstract away repeating blocks of code which include things like "return", the only option is to use a macro.
#define RETURN_IF_NULL(ptr) if(ptr==NULL) return FAILURE_NULL
now, instead of the repetitively doing that null pointer check using three lines of code space, we can do it in one line, enabling us to focus on the "real" code.
RETURN_IF_NULL(param1)
RETURN_IF_NULL(param2)
RETURN_IF_NULL(param3)
This cannot be done using inline functions.
|
|
|
|
|
Actually just saying that MACROs are bad, is half knowledge. Many many platforms like MFC and ATL use MACROs extensively.
The platform(Architecture) I work on is OS independent. In that case MACRO work as saviour. We can't use standard template because that would make code platform dependent so we have to USE MACRO to simulate templates. MACROs are an effective way to use the strength of preprocessor, if you use them correctly then they can really be useful.
When some one says that avoid MACROs then the main intention is to avoid using it at the place of function. But the usage of MACROs are beyond that also where they would be indispensable.
|
|
|
|
|
I have been into C# development for the past 2 years but now i feel i wanna get away from development and analysis is sort of something i want to get into. Can anybody help me by giving tips on what and all should i know to get into analyst job.....Plssssssssssssssss helpppppppp !!!
|
|
|
|
|
Have you found an analyst job? In the past I've looked at dice.com and monster.com, or any other agency out there. Pretty much gave up and went freelance.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hi,
I am trying to attach CRUD operations with my classes. What are the best method that I can follow in VB.NET 2005
Thank you for your help...
Akbar
|
|
|
|
|
I suggest reading this[^]. I also suggest reading some of the other articles around centering around data and Business objects in this list[^].
|
|
|
|
|
thank you for your help.... I expect some other design patters others than from Microsoft.
Akbar
|
|
|
|
|
I didn't post it saying it was the "end-all be-all" of how to do it. It was posted as a suggestion and a hint of how to start. You can find other patterns all you want, but they'll follow about the same layout as the ones I posted. It's standard business and data layer stuff.
I suggest reading those links and then Googling for whatever else you're looking for.
|
|
|
|
|
Hi,
See my code below.
Public Class OOPS
Public Function Check(ByVal a As Int32, ByVal b As Int32) As Int16
Return a + b
End Function
Public Function Check(ByVal a As Int16, ByVal b As Int16) As Int16
Return a - b
End Function
End Class
Dim obj As New OOPS
Dim retVal As Int16
retVal = obj.Check(1, 2)
Which function will be called the first one or the second one and why.
Thanks in advance for your help
Rohan
|
|
|
|
|
rohan1981 wrote: Which function will be called the first one or the second one
Neither
rohan1981 wrote: and why.
Because I won't use Visual Basic
|
|
|
|
|
Thats a good one!!
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
|
|
|
|
|
This is a VB.NET specific question and one that is quite easy to test on your own. All you 'd have to do is put
Debug.WriteLine("Int32 version")
before the Return statement in the Int32 version.
The default data type for both arguments in your Check call would be Int32. The compiler never assumes that a integer would be an Int16. From there, it's easy to figure out that the Int32 version of your method would get called. The Int16 version would never be called unless you passed in two arguments that were specifically defined as Int16's.
|
|
|
|
|
Thanks Dave. This is not actually a VB.NET specific question.
I mean because I have taken an example with VB.net syntax it doesn't make this problem VB specific.
Anyways, thank you for your reply it is really helpful for me.
Rohan
|
|
|
|
|
Thannks for the detailed description.
I think, I mae a mistake using vb.net code syntax what if i would have put C# syntax to make my point clear?
Rohan
|
|
|
|
|
Either way, the language you choose is going to dictate which forum the question should go in. Even though both C# and VB.NET both target the .NET CLR, there are differences between the languages. For instance, VB.NET supports optional parameters in methods, where C# doesn't. C# supports pointers, where VB.NET does not.
|
|
|
|
|
It would depend on what the compiler (VB.NET or C#) decided to classify 1 or 2 as. As Dave pointed out it just so happens that both decide to call it Int32 but one could potentially decided to place 1 and 2 in bytes for they cared.
|
|
|
|
|
Hi,
See my code below.
Public Class OOPS
Public Function Check(ByVal a As Int32, ByVal b As Int32) As Int16
Return a + b
End Function
Public Function Check(ByVal a As Int16, ByVal b As Int16) As Int16
Return a - b
End Function
End Class
Dim obj As New OOPS
Dim retVal As Int16
retVal = obj.Check(1, 2)
Which function will be called the first one or the second one and why.
Thanks in advance for your help
Rohan
Rohan
|
|
|
|