4

I've become a bit of pain when it comes to code styling these days. Things like:

  • Ensuring indents are used with tabs and aligned correctly
  • Ensuring control structure opening and closing brackets are on new lines
  • Use single quotes when possible rather than double quotes
  • Variables are defined like so:

Correct:

$app    = JFactory::getApplication(); 
$jinput = $app->input;

Incorrect:

$jinput = JFactory::getApplication()->input; 

I deem the second one incorrect as $jinput has been defined, however if you need to use $app again, then you will need to call JFactory::getApplication() again which is pointless. So simply define it once, then anything after it can be based on the $app variable.

I find it simply looks much cleaner and it better coding practice.

So now to the question bit. When we (those who agree with the above) see answers that do not adhere to these types of coding practices, do we (can we) edit them accordingly?

Some may not be bothered which is perfectly fine but for those that are, would everyone agree that it's fine to edit these answers?

5
  • I'd prefer the original code to be left and instead a comment made containing the better code and a short explanation of why it was better. I feel I would learn more from it this way.
    – TryHarder
    Dec 18 '14 at 22:04
  • @moomoochoo - Fair enough. And what are your opinions on coding style? Cause we cannot post better coding styles as a comment. Only way is to edit the answer
    – Lodder
    Dec 19 '14 at 9:57
  • Generally, I would avoid changing the coding style, but I think it really depends on what you are changing. If you are changing it for legibility then I think no problem, but if you are changing it because you think one style is better than another than that probably isn't a good idea. In my case, I know I wouldn't appreciate someone changing my javascript snippets from Allman style to K&R.
    – TryHarder
    Dec 20 '14 at 6:05
  • If I won't use $app once again, I prefer to define $jinput directly.
    – Farahmand
    Dec 27 '14 at 13:29
  • @Farahmand - That fair enough in your case. The reason why I do this is because whenever I developer something, I always start adding new features to it which utilizes more of the Joomla API, thus $app for more normally gets used more than once
    – Lodder
    Dec 27 '14 at 13:51
2

Coding standards can quickly become a sore spot for many folks, so while I agree, I don't see the need to "enforce" them here. I think they're valid and good edits for JSE site, particularly if the edit falls in line with Joomla standards, or if it makes the code more readable, or if it's a "teaching moment" for someone that doesn't understand it.

But, at the same time, altering the question may not be such a great idea, so I think they should be technically left as asked, but answers should be improved as outlined.

2
  • Oh no, I don't mean enforce them as it's not something I expect at all. I'm simply referring to us editing these answers and of course, not editing questions as this could cause problems
    – Lodder
    Dec 17 '14 at 16:06
  • I don't think I really understood that you truly meant enforcing them, per so, so much as making such edits a "standard" of some kind - it just brought that aspect into my thinking while answering, so seemed worth mentioning :)
    – GDP
    Dec 17 '14 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .