Thoughts on Generics

Hello 0x00sec!

For some reason, I currently feel like posing a question: What are your thoughts on Generic Programming?

If you don’t already know, Generic Programming is when the programmer writes a single function or set of functions capable of being used with a number of then unspecified types.

An example in pseudo-code:

void print_array(T array):
    for_each element in array:
         print element + " "

Note: The syntax is not language-specific. This is pseudo-code. Don’t compile this!

So, for the C++ programmers out there, or anyone else with an opinion on the Generics, what are your thoughts?

Personally, I find it convenient, but it can easily be a waste of time if you know your code will only use, for example, a specific type, like an array of floats.

1 Like

Hey can you help me out…? The code doesn’t seem to be running on my Windows environment.

Just kidding. This seems pretty good for certain programs. Let’s say you write a function that creates an ordered list.

You then add a lot of names to it and order them.
Next up you want to add a ton of prices and order those.
Then you wanna go create some IDs for people and so you use whole numbers.

2 Likes

Ahahaha! I was ready to hit the reply button on your comment and explain, then I read “just kidding” :joy: nice one :wink:

It looks like Java, C, Python and Ruby all had a baby.

I had Python, Ruby, and C++ in mind… No Java; never Java!

I agree it can be a waste of time if your code uses a specific type, as well as that it can also be a it confusing writing generic code. In my case, I think I’ll write specific code, until I get the hang of it :stuck_out_tongue:

It was the void prefix on the function that thew me for java. public static void print_array() etc.

Just like @Sea said this seems like something that would be really good if your doing a lot of sorting or searching especially if there are varied data types, and would help reduce the amount of code that you actually have in your final program.
But there seems very little point to do it if there is only ever going to be one data type passed to it other than probably making any debugging a bit harder to do.

1 Like

Yup, pretty much. I agree.

My thoughts…

As any single programming language feature out there, there is a situation or problem where they will help. Said that, I think generics are overall a great thing.

From a SW development point of view they provide what every programming language aims to provide; a simplified way to write code and also a way to maintain groups of semantically equivalent functions in one just place. Simplification reduces complexity, and the lower the complexity the lower the number of bugs.

In general, you, as a programmer, want a higher level of abstraction to talk to your computer. Otherwise all of us will be typing binary data to write our program… don’t we?. So, anything that helps to deal with problems at a higher level of abstraction is good. Specially if it is a feature that you can chose to use or not.

Then again, it all depends on what you have to do. That is one part of what programming is about; to chose the right tool to solve your problem. There are a couple of fields where you will really benefit from using generic programming. Other cases where there will be no benefit.

So, you need to know your tools and chose the right one at the right time… or at least try your best :wink:

2 Likes

Can’t agree with this more. However, there tends to be a trade-off with high-level abstraction and speed/efficiency. Since a higher level language won’t let you tinker under the hood, you have to use what you’ve been given.

Give for example pythons .sort() function. Now I don’t know how this function operates, but it sorts a list. If I was sorting a special data type, that required an exclusive algorithm, then I couldn’t use the .sort() function, and I’d have to write my own function. This means that although I’ve been offered the opportunity for higher-level abstraction, it’s no good to me because it doesn’t operate efficiently enough for my needs. In which case I may as well write the program in C.

But you’re right, if we can use a higher-level language, and it will suit our needs, then we’d be silly not to.

Soon enough, it won’t matter. Computers and high level language interpretation will become faster and optimized. CPU speeds are peaking and it won’t be able to get any faster with the conventional computer. The only purpose that low level languages will probably have is for low level tasks in embedded devices or operating system management.

1 Like

So are you saying that code efficiency won’t matter, because the computers will be so fast?

Probably negligible, unless you have no idea what you’re doing and fill your code with redundancy. Nevertheless, compilers and optimizers are there to remove such junk. C compilers make much more efficient assembler code than humans could.

I understand that the differences may be negligible; but I would say that clean-efficient & code is important by principle. What if somebody needs to refactor the code somewhere else? What if somebody runs your program on a legacy machine? Anything that is wasted, and could be saved (such as electricity and the odd bit), should be saved.

Sure, if you want to maintain something, have all the clean code you want but we’re not discussing about how well something can be managed. As technology evolves, more and more things will become outdated and replaced and I understand your concern about saving resources however, as software features start to grow in size, so too will the complexity and the time and space requirements. It’s inevitable that more things will become available to us and as a result, we just can’t continue support for technology that belongs in a museum. It doesn’t make an sense and it will only hinder our advancement.

2 Likes

I do have to agree with you on that. Whats your view on the direction of platforms? My personal view is that everything will start to go web-based (as it is already), and platforms like the Chromebook will rule. HTML5, JS, CSS3 and countless javascript libraries in my opinion will mark the building blocks for many of our most used tools. This platform for example is Ruby on Rails with a Javascript front end. And it is capable of a lot, especially when you link it with other people.

1 Like

Well said, @0x00pf

You can’t pass a block to .sort() in Python? You can in Ruby, and it makes manipulating complex data types very simple.

@dtm I agree with your point that we shouldn’t maintain tech that belongs in a museum. But, if programming becomes particularly easy because of compiler optimizations, and that somehow leads to an abundance of supbar software engineers, we’re going down a bad path. Sure, the compiler can take out a bunch of redundant code or make a small function inline to avoid the overhead, but the compiler can’t fix all of the logic errors produced by humans. So if we create an atmosphere in which the programmer doesn’t have to think that much, and can write inefficient code with the expectation of it being optimized by the compiler, we create an atmosphere in which much of our software will be designed mediocrely.

Oh! My bad :stuck_out_tongue: I guess python isn’t as good as ruby xD

1 Like

I don’t recall saying anything about compilers simplifying the programming process nor say that they can fix errors.