Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
"Backwards" Conditionals in C

I'm looking through some code and I found some strange conditionals, namely:

if (NULL != buf) {...}

I was wondering if there was a particular reason for writing the conditional like this, instead of

if(buf != NULL){...}

I can't see any reason to do it the first way off the top of my head, but I don't think it was a mistake. To me, it seems like they accomplish the same thing, but the second way is way more intuitive. Is there some specific reason to use the first conditional?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Yes, it's called "Yoda conditions". The idea is to not accidentally assign a value when you mean to be doing a check. Most modern compilers should catch it.

over 4 years ago · Santiago Trujillo Relatório

0

It is to avoid newbie typo like if (buf = NULL).

if (NULL = buf) leads to compilation error, while if (buf = NULL) is totally correct with undesired semantic.

over 4 years ago · Santiago Trujillo Relatório

0

The concept is basically this:

Often new learners miss the double equals sign == and use a single = instead. Instructors therefore teach them this method so that they are not stuck at simple programs.

With no compiler warnings enabled, if you do this:

if(buf = NULL)

You are basically assigning NULL to buf, which is not syntactically wrong, and you would not get any warnings, but when you do this:

if(NULL = buf)

the compiler throws error because it knows you cannot assign anything to NULL.

Why Yoda in particular?

It's because of the character Yoda, from Star Wars, whose dialogues were styled in reverse orders, like " Blue is the sky".

You can read more interesting coding terms here.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda