Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulation of functions #529

Closed
wants to merge 2 commits into from
Closed

Encapsulation of functions #529

wants to merge 2 commits into from

Conversation

thegreatgunbantoad
Copy link
Contributor

tempScaleLabel updated to use parameters rather than refer to external variables
scaleTemperature updated to use parameters rather than refer to external variables

Note change of name of scaleTemperature to scaleTemperatureFromC

@thegreatgunbantoad
Copy link
Contributor Author

Again not a functional update. Not sure why linting is failing though.

@pppedrillo
Copy link
Contributor

Why?

@thegreatgunbantoad
Copy link
Contributor Author

Why?

It makes code easier to reuse and understand. Would also allow you to to pull the functions out of the main file and put them in a library. A task it a bit different, like an initialisation task, but functions should generally be properly parameterised.

@pppedrillo
Copy link
Contributor

pppedrillo commented Aug 5, 2021

It makes code easier to reuse and understand. Would also allow you to to pull the functions out of the main file and put them in a library. A task it a bit different, like an initialisation task, but functions should generally be properly parameterised.

"Parameterization" != "to duplicate all the used global/static variables using arguments"
And those functions still not in the library or taken out of main file.

Generally why only those two functions were picked? Follow your logic, you'll need to refactor most of the rest of the functions which also use global variables (voltage, tilt etc etc) in the same manner, no?
And what if function use 125 global vars? Should it have 125 arguments?

I'd also advise to pass args by reference whenever it makes sense (like here, myData.my_tempscale) and master a switch statement (instead of the if-else-if-else... spaghetti as soon as it is refactoring)

Renaming a function to "FromC" looks good.

{
if (myData.my_tempscale == TEMP_CELSIUS)
if (tempscale == TEMP_CELSIUS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do not use switch here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do, I wasn't intending to make great swathes of changes all at once.

{
if (myData.my_tempscale == TEMP_CELSIUS)
if (tempscale == TEMP_CELSIUS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above - why not switch?

@thegreatgunbantoad
Copy link
Contributor Author

It makes code easier to reuse and understand. Would also allow you to to pull the functions out of the main file and put them in a library. A task it a bit different, like an initialisation task, but functions should generally be properly parameterised.

"Parameterization" != "to duplicate all the used global/static variables using arguments"
And those functions still not in the library or taken out of main file.

Generally why only those two functions were picked? Follow your logic, you'll need to refactor most of the rest of the functions which also use global variables (voltage, tilt etc etc) in the same manner, no?
And what if function use 125 global vars? Should it have 125 arguments?

I'd also advise to pass args by reference whenever it makes sense (like here, myData.my_tempscale) and master a switch statement (instead of the if-else-if-else... spaghetti as soon as it is refactoring)

Renaming a function to "FromC" looks good.

Only picked those two as it was just a start at getting the functions parametrised. If a function uses lots of global variables I think it might need re-writing.
Note the function that writes to the JSON could now just be passed the myData struct. I was really hoping C++ had an iterator like Python that would have made writing to and reading from the JSON slicker.

Yes the plan was to refactor as many functions as is practical. Globals are generally best avoided if reasonable, they can make code very hard to follow.

I sort of prefer pass by value as you know the variable can't change while the function is operating and the function can't alter the inputs. But if you wanted several outputs then pass by ref is handy. Is there a processor advantage to ref over val?

Agree with using a switch.

@pppedrillo
Copy link
Contributor

pppedrillo commented Aug 7, 2021

I was really hoping C++ had an iterator like Python that would have made writing to and reading from the JSON slicker.

There is nothing to do with C++ language, but rather the concrete implementation of JSON (de)/serializer which is not a part of C++ standards.

Yes the plan was to refactor as many functions as is practical.

Thats my main concern.
It can be a pull request then all the changes are done, not while work in progress.

Globals are generally best avoided if reasonable, they can make code very hard to follow.

There are some more serious problems with globals and statics, not the only code readability.

I sort of prefer pass by value as you know the variable can't change while the function is operating and the function can't alter the inputs. But if you wanted several outputs then pass by ref is handy. Is there a processor advantage to ref over val?

Passing by val not always guarantees what some data can't be changed.
If you pass an object by value and this object has a pointer to data, you still can change a data by this pointer.

Omitting an extra copy is one of advantages. Not a huge deal with one shot functions and PODs, but may cause performance issues if function gets called frequently and you pass a huge object(s) to it.

@PjotrekSE
Copy link

PjotrekSE commented Aug 7, 2021 via email

Copy link
Owner

@universam1 universam1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍🏻

@universam1
Copy link
Owner

@thegreatgunbantoad there are merge conflicts, mind rebasing please?

@thegreatgunbantoad
Copy link
Contributor Author

@universam1 I'll have look later, not great at the git thing yet, so might need a hand.

@pppedrillo
Copy link
Contributor

@universam1 I'll have look later, not great at the git thing yet, so might need a hand.

I rebased those changes, #543 @universam1

@thegreatgunbantoad
Copy link
Contributor Author

@pppedrillo does that mean I can just close this request then? Sorry full of a cough and such so my brain is struggling.

@pppedrillo
Copy link
Contributor

pppedrillo commented Oct 2, 2021

@pppedrillo does that mean I can just close this request then? Sorry full of a cough and such so my brain is struggling.

@thegreatgunbantoad Yes, if you are ok with changes from #543. Do get better!

@thegreatgunbantoad
Copy link
Contributor Author

@pppedrillo frazzled brain thinks looks good.

universam1 pushed a commit that referenced this pull request Oct 6, 2021
* Pull Request #529 rebase

* Rebase leftovers fix

Co-authored-by: Me <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants