Moodle coding style - naming variables

Moodle coding style - naming variables

Tomasz Muras發表於
Number of replies: 4
Core developers的相片 Plugin developers的相片 Plugins guardians的相片 Translators的相片

Hello,

We name variables in Moodle by concatenating words together, e.g. $loggingquery, this is well documented. I have run a simple grep search on Moodle core code and found nearly 2k variable names that do not follow that convention and use underscore to separate words. Of course, we should not have core code not adhering to the standards we've set ourselves but maybe it's a good time to review that standard? Have a look at few examples of variable names we have now, and how will they look like if we rename them:

$used_for_db_sessions -> $usedfordbsessions

$fix_sql_params_i -> $fixsqlparamsi

$node_course_modules_quiz_question_instances -> $nodecoursemodulesquizquestioninstances

$node_course_question_categories_question_answer -> $nodecoursequestioncategoriesquestioncategory

It doesn't seem very readable but we have few "monsters" like this, e.g. $inorequaluniqueindex, $submissiongroupmemberswhoneedtosubmit, $origgradeincludescalesinaggregation .

I suggest that we either:

1. Change coding style.
2. Rename the variables to match existing coding style.

cheers,
Tomek

 

btw. here are the top 20 offenders with number of times they are used in the code:

   1050 $grade_item
    550 $xmldb_table
    351 $xmldb_field
    341 $grade_category
    210 $grade_grade
    152 $default_values
    147 $mnet_peer
    127 $file_record
    110 $grade_items
    106 $xmldb_key
     85 $replace_values
     80 $row_offset
     74 $find_tags
     72 $xls_formats
     70 $photo_id
     68 $home_url
     66 $xmldb_length
     65 $grade_grades
     64 $question_identifier
     64 $feedback_identifier

評比平均分數:Useful (2)
In reply to Tomasz Muras

Re: Moodle coding style - naming variables

Eloy Lafuente (stronk7)發表於
Core developers的相片 Documentation writers的相片 Moodle HQ的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片 Testers的相片

Hi Tomasz,

yes, there are a lot of code-style errors caused by variable names and everything else. Nice research.

Basically all them correspond to "old code" (code introduced before 2.x) where we did not have such wonderful rules nor a way to enforce them. Any "recent code" should be following the style since a couple of years ago.

And yes, it's a problem. And yes, we should discuss about the best way to, progressively, move that "incorrect" code to follow the rules. Surely the main problem here is that doing global changes to very active areas would lead to problems (conflicts or wrong mixes) when backporting issues, but I think that's something we should be able to control.

In the other side, there are some areas, like the sql generators (xmldb_table|field|key....) where I wouldn't expect many changes happening, so it should be safe to apply the corrections (only in master always, never in released stables).

In any case, I strongly oppose to "1) Change coding style" and would recommend to follow "2) Review and fix the style violations where possible". Indeed.

Let's hear some more developer's thoughts. Thanks for igniting this. Ciao 微笑

評比平均分數:Useful (2)
In reply to Eloy Lafuente (stronk7)

Re: Moodle coding style - naming variables

Tomasz Muras發表於
Core developers的相片 Plugin developers的相片 Plugins guardians的相片 Translators的相片

Option 2 it is - we should aim at making Moodle core code to respect our coding standard. I think the best way to track it these days would be to create an epic and link more issues to it?

I 100% agree that it should only be done in master - and now it's actually a good time, since 2.6 have just been released.

I will create an issue & do some initial work if nobody objects.

cheers,
Tomek

評比平均分數:Useful (1)
In reply to Tomasz Muras

Re: Moodle coding style - naming variables

Joseph Rézeau發表於
Core developers的相片 Particularly helpful Moodlers的相片 Plugin developers的相片 Testers的相片 Translators的相片

What's an "epic"?

Joseph

In reply to Joseph Rézeau

Re: Moodle coding style - naming variables

Matteo Scaramuccia發表於

Hi Joseph,
Epic is an Agile term used to indicate something greater than a User Story i.e. it needs to be reduced in several User Stories to let the Team identify the Points required to develop them and to complete each of them in an Iteration. Usually used to drop something into the Backlog without thinking first at its Size.

Refs.: https://confluence.atlassian.com/display/AGILE/Working+with+Epics.

HTH,
Matteo

評比平均分數:Useful (1)