Home » U++ Library support » U++ Widgets - General questions or Mixed problems » Overriding Display methods too complicated due to high amount of arguments (Making Display class easier to use)
Overriding Display methods too complicated due to high amount of arguments [message #55204] |
Sun, 18 October 2020 00:01  |
 |
Klugier
Messages: 1099 Registered: September 2012 Location: Poland, Kraków
|
Senior Contributor |
|
|
Hello,
I would like to tell you what I don't like about Display class (CtrlLib). It's two virtual methods are complicated to override due to the need to explicitly repeat the parameter list. It is exactly 6 parameters. Let's take a look at what we have now:
class Display {
public:
...
virtual void PaintBackground(Draw& w, const Rect& r, const Value& q,
Color ink, Color paper, dword style) const;
virtual void Paint(Draw& w, const Rect& r, const Value& q,
Color ink, Color paper, dword style) const;
The solution for that is to introduce simply DisplayPaintContext that will contain all parameters variables:
class Display {
public:
...
virtual void PaintBackground(const DisplayPaintContext& ctx) const;
virtual void Paint(const DisplayPaintContext& ctx) const;
// Or
virtual void PaintBackground(Draw& w, const DisplayPaintContext& ctx) const;
virtual void Paint(Draw& w, const DisplayPaintContext& ctx) const;
Of course, it can be implemented in alternative way for example by providing first Draw parameter and rest as a context. Of course we can not simply change the declaration of this method. To fix the problem we should introduce the new one and deprecate the old.
Sample replacment in Display and Array examplex:
struct FontFaceDisplay : Display {
virtual void Paint(Draw& w, const DisplayPaintContext& ctx) const override {
auto r = ctx.GetRect();
Font fnt = Font(ctx.GetValue(), r.Height() - 2);
String txt = Font::GetFaceName(ctx.GetValue());
w.DrawRect(r, ctx.Paper());
w.DrawText(r.left + 2, r.top + (r.Height() - GetTextSize(txt, fnt).cy) / 2, txt, fnt, ctx.GetInk());
}
};
struct MyDisplay : public Display {
virtual void Paint(Draw& w, const DisplayPaintContext& ctx) const override
w.DrawRect(ctx.GetRect(), paper);
w.DrawEllipse(ctx.GetRect(), ctx.GetValue());
}
};
Reference:
- Coding Revolution - Long Parameter List
- Refactoring Guru - Long Parameter List
- SonarSource - Static analyzer - Functions should not have too many parameters (4 is maximum limit)
Klugier
U++ - one framework to rule them all.
[Updated on: Sun, 18 October 2020 01:03] Report message to a moderator
|
|
|
 |
|
Overriding Display methods too complicated due to high amount of arguments
By: Klugier on Sun, 18 October 2020 00:01
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: Oblivion on Sun, 18 October 2020 19:16
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: mirek on Mon, 19 October 2020 15:59
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: Oblivion on Mon, 19 October 2020 16:53
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: mirek on Sat, 14 November 2020 10:02
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: Oblivion on Sat, 14 November 2020 10:14
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: Klugier on Sat, 14 November 2020 14:23
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: mirek on Sat, 14 November 2020 14:33
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: mirek on Sat, 14 November 2020 14:38
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: Klugier on Sat, 14 November 2020 20:29
|
 |
|
Re: Overriding Display methods too complicated due to high amount of arguments
By: mirek on Sun, 15 November 2020 00:03
|
Goto Forum:
Current Time: Tue May 13 18:29:49 CEST 2025
Total time taken to generate the page: 0.03515 seconds
|