M
Martin Ba
C(++) allows for a simple variable declaration+init in the condition of
an if or while:
while (T t = x) {
}
if (int x = f()) {
}
I find this rather useful for things like:
if (const T* pThing = GetThing()) {
...
} else {
// Handle No-Thing case
}
However, I have been told by a colleague that this is little used and
would surprise and confuse a lot of developers and therefore it's better
to use the form:
const T* pThing = GetThing();
if (pThing) {
...
} else {
// Handle No-Thing case
}
Opinions?
cheers,
Martin
an if or while:
while (T t = x) {
}
if (int x = f()) {
}
I find this rather useful for things like:
if (const T* pThing = GetThing()) {
...
} else {
// Handle No-Thing case
}
However, I have been told by a colleague that this is little used and
would surprise and confuse a lot of developers and therefore it's better
to use the form:
const T* pThing = GetThing();
if (pThing) {
...
} else {
// Handle No-Thing case
}
Opinions?
cheers,
Martin