FC Code Quality

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
berniev
Posts: 247
Joined: Wed Apr 13, 2022 10:45 pm
Location: Oz

FC Code Quality

Post by berniev »

I see kick-back here to any negative comments about FC code quality. Perhaps this goes some way to an explanation.

I just happened to be looking at /src/Base/Builder3D.h and its mates and started to play. Nothing here requires a detailed understanding of the intricacies of what the code does, beyond refactoring it to do the same.

Note: There is nothing here that has not been thoroughly documented in every programming textbook for the last 20+ years. The principles don't apply to just C++.

Many clang-tidy warnings. Code smell: Unresolved issues. Might be hidden issues. Looks like old C programmers have been here.

Often no curly braces. Code smell: Apple goto fail (2014?) freaked out many.

Many format inconsistencies. Code smell: Difficult to maintain.

There are several classes. We select Builder3D and InventorBuilder. There is much shared code. Code smell: Classes are very similar.

Both classes make items, generate stuff and send to a designated target.

Both classes have a lot of methods. Code smell: Maybe too many responsibilities.

Both classes share a number (but not all) item types. Code smell: This class concerning itself with the intricacies of generating these items and bloating itself with a bunch of very specific methods to do so? If you want to change items the class must be refactored. Bad.

Every item becomes a string and appended to the output. And that pretty much defines the class. The class should care not what exactly it is sending.

Looking at the methods there is an immediate code smell: Too many parameters. Either the method is too complex or it has too many responsibilities. e.g.:

Code: Select all

void addText(float pos_x, float pos_y , float pos_z,const char * text, float color_r=1.0,float color_g=1.0,float color_b=1.0)
Pardon the French, but you've got to be f'ing kidding me! I can't even read that let alone comprehend it. And there's lots of this. Turns out these are basically three groups: Color, position, and item specific stuff.

Looking at this example we see that the first three are position, the next is the item content, and then follows three more for color, sometimes four.

Code: Select all

void addText(Position position, std::string text, Color color)
would be infinitely more understandable and clearly expresses simple concepts.

Position and color are used often (the long way) so would benefit immeasureably from being defined as such. They become simple to define and pass around.

Color is immediately recognisable, whereas a list of strings or numbers is indecipherable (could be anything, never mind what they are called).

When a color or position is passed in this long horrible way, who's to say it is valid? You get the idea.

The attached file is where I got to. Remember I am still coming up to speed with C++ so there are likely major fundamental mistakes. It is untested. It is complete only so far as to show some ideas.

Firstly we make a base class and subclass off it.

We establish a color class, RGB and RGBA. With these focused, the content can be checked and passed around easily.

Ditto position.

In each case with sufficient constructors to provide minimum flexibility.

Make a couple of interfaces to ensure classes adhere to requirements.

To add a new item type is as simple as defining a class for it and providing the two versions of output.

Make a class for each item. Include two methods: one for each class.

The code is now simpler, cleaner and easier to read AND TEST ( a bit of a novel concept around here - and far too difficult with the current structure). Taking it one small step at a time yields results.

Further refactoring would likely yield many more improvements.

So please don't shoot the messenger.

Suggested reading:

C++ Coding Standard: Sutter & Alexandrescu
Refactoring: Fowler
A Philosophy of Software Design: Ousterhout
Effective Modern C++: Meyers
Effective C++ 3rd Edition: Meyers
Modern C++ Design: Stroustrup
Object Oriented Software Construction: Meyer (Many really old ideas. Beware)
Your Code as a Crime Scene: Tornhill
Large Scale C++ Vol 1: Lakos
The C++ Programming Language: Stroustrup
Working Effectively with Legacy Code: Feathers
Test-Driven Development: Beck
Design Patterns: Gamma,Helm, Johnson, Vlissides
Attachments
refactor.rtf
(6.89 KiB) Downloaded 16 times
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: FC Code Quality

Post by openBrain »

Why create new classes for point and color rather than using QVector3D and QColor?
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: FC Code Quality

Post by Zolko »

openBrain wrote: Fri Sep 23, 2022 10:09 am Why create new classes for point and color rather than using QVector3D and QColor?
please clearly indicate sarcasm to avoid further confusion
try the Assembly4 workbench for FreCAD — tutorials here and here
User avatar
onekk
Veteran
Posts: 6205
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: FC Code Quality

Post by onekk »

I see the point.

And for sure it will be a good thing to modernize the code and make it more compliant with more modern standard.

But if the function as in this case is expecting three floats for coordinates and you modify it with a single Vector() (or similar construct), what happen to the code exposed that will use it?

I don't know if this is a concern only for developers or if it affects scripts, macro or other code that is "around", in this case you have to modernize the code but leave an entry point (maybe emitting a "deprecation warning") and wait some time to "not break anything".

It is a very good thing that code is modernized, so it is not a critics, it is only a caveat.

Something similar is usually found as example in Draft WB where some old deprecated methods are retained but the "operating code" is used instead of the old code but you could use "old API" until is "enough old" to be rarely used.

Regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: FC Code Quality

Post by Zolko »

berniev wrote: Fri Sep 23, 2022 9:13 am I see kick-back here to any negative comments about FC code quality.
And I see a wannabe white-space cleaner. Please tell first: what's the problem you're pretending to solve ? If it's : "Oh, you're not complying with this favorite book of mine " then just go away. Simply put. Most of us are engineers here and not some marketing droids. If you don't like the code don't look at it.

Therefore: clearly state the problem and clearly give your solution. Else you're just (another) troll
try the Assembly4 workbench for FreCAD — tutorials here and here
User avatar
onekk
Veteran
Posts: 6205
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: FC Code Quality

Post by onekk »

Zolko wrote: Fri Sep 23, 2022 11:32 am ...
Therefore: clearly state the problem and clearly give your solution. Else you're just (another) troll
Hello, I see your point, but from the little I know @berniev is not a troll.

He probably is posting his thoughts here to try to improve code. ( a quick glance of his past post, will reveal this).

But as you see many code sorces (And I know that you have a good knowledge of FC source code) it is easy to see that some part if the code are quite old, and some modernization and maintenance is necessary.

But as if some reworking if code will involve to modify and modernize many things, probavly he want to nit waste his effort prior to be sure of a general agreement on modifications.

Or this is what I've guessed from his posts.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
adrianinsaval
Veteran
Posts: 5551
Joined: Thu Apr 05, 2018 5:15 pm

Re: FC Code Quality

Post by adrianinsaval »

Zolko wrote: Fri Sep 23, 2022 11:32 am And I see a wannabe white-space cleaner. Please tell first: what's the problem you're pretending to solve ? If it's : "Oh, you're not complying with this favorite book of mine " then just go away. Simply put. Most of us are engineers here and not some marketing droids. If you don't like the code don't look at it.

Therefore: clearly state the problem and clearly give your solution. Else you're just (another) troll
so you have complaint about bad code in the past but when someone comes along and starts working on improving code you call him a troll? Take a look in the mirror...

berniev I recommend to just ignore Zolko here, sometimes he's unreasonably stubborn and confrontational.
chrisb
Veteran
Posts: 54201
Joined: Tue Mar 17, 2015 9:14 am

Re: FC Code Quality

Post by chrisb »

FreeCAD has become a big program with more than one person modifying the code if necessary. Having a common standard - whichsoever - makes it easier to concentrate on the semantics rather than the syntactical sugar. This even includes spaces, at least when it comes to indentations and recognizing corresponding brackets at a glance.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Zolko
Veteran
Posts: 2213
Joined: Mon Dec 17, 2018 10:02 am

Re: FC Code Quality

Post by Zolko »

adrianinsaval wrote: Fri Sep 23, 2022 12:22 pm so you have complaint about bad code in the past
yes I have, for example, when vox (if I remember right) wanted to re-factor all the Draft code to be more Python compliant, exactly like this white-space cleaner: now the code is even more unreadable and un-usable. He duplicated ALL and EVERY function !!! Like rice_cooker and riceCooker. For absolutely NOTHING except to be compliant to some arbitrary definition. Did you read the Draft source code before and after vox ? If this is the sort of idiot that we're seeing here I can't be harsh enough
try the Assembly4 workbench for FreCAD — tutorials here and here
User avatar
mfro
Posts: 666
Joined: Sat Sep 23, 2017 8:15 am

Re: FC Code Quality

Post by mfro »

Zolko wrote: Fri Sep 23, 2022 12:37 pm... If this is the sort of idiot that we're seeing here I can't be harsh enough ...
If you had followed what @berniev pushed during the last few weeks and months, you'd likely speak otherwise.

Lots of cleanups and - although cosmetics to a large degree - highly appreciated at least by myself (as I can read "clean" code much easier as well).

The only complaint I might have is (although necessary) why that needs to happen during @realthunder 's toponaming integration activities that are probably (IMO) unnecessarily hampered due to lots of cosmetic changes.
Cheers,
Markus
Post Reply