Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I created my helper classes, am I over designing?

Tags:

c++

c

I am a C++ programmer and recently joined a new company that uses a lot of C. When they reviewed my code, they were thinking I over-designed some of the things which I totally disagreed. The company is doing everything in embedded system, so my code needs to be memory efficient, but the stuff I am doing is not CPU intensive. I would like to know how you guys think what my design. Here is the list.

  1. I have some arrays that need to pass around and eventually need to pass to some C code. I could pass a pointer and a size all over of the place. But I chose to create a class that represents this -- a class that has a fixed size (we know the maximum size) buffer, and a length which should be always <= the size of the buffer, otherwise assert. In this way, I can pass the array around with only one variable instead of two, and if the maximum size changes in the future, I should be able to change it easily. I don't use dynamic allocation for the array because it is embedded system and memory allocation could potentially fail and we don't use exception. The class is probably less than 30 lines code, and I use it for quite a few places. They said I over-designed it.

  2. They have their own containers implementation in C. I needed to use one of them, but I wanted to hide all the detailed code away from my main logic, so I created a wrapper class for it. The wrapper class is similar to stl, so I have iterators and it manages the memory allocation internally, but unlike stl, it returns a error code when it can't allocate more memory. Their argument on this one is that I am the only one uses it, so they don't want it to be in the repository. I found it stupid to be honest.

EDIT: The following class is more or less that I used for point 1. All I wanted to do is to have something to pass around without carrying the length all the time.

class A
{
  static const MAX_SIZE = 20;
  int m_Array[MAX_SIZE];
  size_t m_Len;

public:
  A(const int* array, size_t len)
  {
    assert(len <= MAX_SIZE);
    memcpy(m_Array, array, len);
    m_Len = len;
  }

  size_t GetLen() const { return m_Len; }
  const int* GetArray() const { return m_Array; }
};
like image 357
leiz Avatar asked May 22 '09 23:05

leiz


People also ask

When should you create a helper class?

Helper classes are often created in introductory programming lessons, after the novice programmer has moved beyond creating one or two classes. A utility class is a special case of a helper class in which the methods are all static.

Is a helper class a code smell?

A Helper class is a lesser known code smell where a coder has identified some miscellaneous, commonly used operations and attempted to make them reusable by lumping them together in an unnatural grouping.

What is helper method?

A helper method is a small utility function that can be used to extract logic from views and controllers, keeping them lean. Views should never have logic because they're meant to display HTML. In addition, there's no way to test view logic, but if we extract this logic into methods it can then be tested.

What are helper classes in CSS?

Helper classes are reusable rules that use class selectors (sometimes combined with type/ID selectors). Helper classes are designed to let you: Apply style to an element without having to create a special rule for that element: This means you can write less CSS.


1 Answers

First things first: You joined a new company, so you can expect that you need to learn to play by their rules. You're still "the new guy" and there will be some resistance to "your way" of doing things, even if it is better. Get used to them and slowly integrate yourself and your ideas.

As to #1, passing a pointer+size is certainly a pain for the programmer, but it's the most memory-efficient way of doing things. Your class is not "over"-designed, but what happens if MAXSIZE becomes really large at some point in the future? All of your instances will take that much space each, even when they don't need to. You could wind up running out of space simply because the MAXSIZE changed, even if nothing needed that much space.

As to #2, it's a tossup on whether it's an unnecessary layer (perhaps it would be better suited to improve their wrapper instead of simply wrapping it again?), but this will come down to how well you integrate with them and make suggestions.

In summary, I wouldn't call it "overdesigned", but in an embedded situation you need to be very wary of generalizing code to save yourself effort vs saving memory..

like image 71
Andrew Coleson Avatar answered Nov 15 '22 16:11

Andrew Coleson