Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any advantage of such coding-style?

Tags:

c

coding-style

|There is this function in gestremer matroska demux plugin:

gboolean
gst_matroska_demux_plugin_init (GstPlugin * plugin)
{
  /* parser helper separate debug */
  GST_DEBUG_CATEGORY_INIT (ebmlread_debug, "ebmlread",
      0, "EBML stream helper class");

  /* create an elementfactory for the matroska_demux element */
  if (!gst_element_register (plugin, "matroskademux",
          GST_RANK_PRIMARY, GST_TYPE_MATROSKA_DEMUX))
    return FALSE;

  return TRUE;
}

Now gst_element_register() is type of

gboolean            gst_element_register                (GstPlugin *plugin,
                                                         const gchar *name,
                                                         guint rank,
                                                         GType type);
Returns :
    TRUE, if the registering succeeded, FALSE on error

Then why not write it in the following way?

 gboolean
    gst_matroska_demux_plugin_init (GstPlugin * plugin)
    {
      /* parser helper separate debug */
      GST_DEBUG_CATEGORY_INIT (ebmlread_debug, "ebmlread",
          0, "EBML stream helper class");

      /* create an elementfactory for the matroska_demux element */
      return gst_element_register (plugin, "matroskademux",
              GST_RANK_PRIMARY, GST_TYPE_MATROSKA_DEMUX))      
    }
like image 344
Jeegar Patel Avatar asked Dec 05 '11 05:12

Jeegar Patel


People also ask

Why is coding style important?

Coding standards help in the development of software programs that are less complex and thereby reduce the errors. If programming standards in software engineering are followed, the code is consistent and can be easily maintained. This is because anyone can understand it and can modify it at any point in time.

What is a good coding style?

Some general advice on good coding styleGood code is self-explanatory – the need for comments should be minimal. Comments should be added to those parts of the code where explanations are needed. With code one should not be clever if it is not necessary – do things in an obvious straightforward way.

Do people have different coding styles?

Coding style is extremely personal and everyone has their own preferred style. You can discover your own personal style by looking back over code that you've written when you didn't have a style guide to adhere to.


2 Answers

It's part of a pattern.

if (!some_function(...))
    return false;
if (!other_function(...))
    return false;

return true;

Whoever wrote it decided not to change the pattern just because there's only one function call. Ultimately it's a matter of taste.

like image 152
Dietrich Epp Avatar answered Sep 26 '22 21:09

Dietrich Epp


As such there is no problem with the code. At least I wont penalize if anyone uses any of the mentioned snippets.

These are the reasons that I think are the cause:

  • The foremost is readability. Now looking at the section code, I know what is success and what is not. As there could be apis, which behaves inversely (even in C).
  • Second reason happens, when during testing you want to print some log statements. Now if you return directly you cannot log the return value or any debug info. You can say after code is freed, one should remove it, but then for next release you might have to put debug statements again. So it stays there.
  • Third reason could be that some code is removed during development.

Conclusion is what I said in the beginning. It does not matter as long as code is easy to understand. Regarding some optimization gains, I think for this compilers are intelligent enough to take care.

like image 34
havexz Avatar answered Sep 23 '22 21:09

havexz