[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Procedural C Superior to OO?
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 69
Thread images: 6
C was intended to be used procedurally, however, it's been shown that one CAN code in an object-oriented fashion if they follow certain guidelines. My question is: is it better to code procedurally in C, or is it acceptable to use it as an object-oriented language?

The first language I became proficient in was object-oriented, and thus, I am most familiar with that paradigm. I'm looking to learn more about C and start some projects with it, but I feel I need to understand how to attack problems using procedural logic, as it's natural for me to think of solutions in terms of objects.
>>
>better

what the fuck does this even mean?
>>
How much of the power or OO do you even use? Pretty much everything I've seen people use OO for would be expressed just as well (if not better) using generics instead. OO can be very powerful but I feel like its use today is like using a jackhammer to hammer a nail in.
>>
>>51515914
I guess I mean either in terms of efficiency or what is considered to be stupid.

Like, you COULD (kind of) program functionally in C, but why the fuck would you?

>>51515977
I'll look more into that. I'm still trying to find a convenient way to transition from an object-oriented mindset to something more procedural so that I can actually come up with design patterns that are efficient and maintainable. I don't want my different paradigm mindsets to bleed into one another and result in dumb software patterns.
>>
>>51515857

if you're going to learn c, learn it as it was intended before (if you must) you start bending it into oo.

even then i wouldnt, if you want to write oo c use c++
>>
>>51515977

uh generics are objects fool.
>>
>>51516118
>I'm still trying to find a convenient way to transition from an object-oriented mindset to something more procedural

wtf are you talking about. if you've only done oo then code something in c in a procdural manner and learn.

>so that I can actually come up with design patterns that are efficient and maintainable.

implying that cant be done in oo. you're an idiot.

>I don't want my different paradigm mindsets to bleed into one another and result in dumb software patterns.

jesus fucking christ. just stop programming, i think you're looking to do an mba.
>>
>>51516155
Generics are absolutely not objects. Objects are complex types that contain data and methods that operate on that data. Generics are just procedures with types that are not defined internally but given to it later on.
>>
>>51516203

List<Int> myList

is an object
>>
File: your logic.jpg (7 KB, 249x203) Image search: [Google]
your logic.jpg
7 KB, 249x203
>>51516183
>"I can do A, but I'm trying to learn how to do B"
>"omg wtf if you want to do B just do B, ur so dumb"
>>
OO is a shit meme and needs to die.
>>
>>51516232
Yes it is.
Are you being retarded on purpose?
>>
Thanks, guys. I'm probably just gonna try and fuck around with it and look at other people's work and critiques to try and get an idea of what is and is not a good practice
>>
>>51516155
OO programming and generic programming are orthogonal. Functional programming is yet again orthogonal to both of those.
>>
>>51516237

except that not what you said, you said...

>convenient way to transition from an object-oriented mindset to something more procedural

and who the fuck knows what that means.

if you had said, i'm trying to learn how to do b.

i would have said read the k&r book and do the exercises at the end of each capter.
>>
>>51516273
>Yes it is.

in that case we have no disagreement.
>>
>>51516682
Pretty sure we do because I disagree with the entirety of >>51516155
>>
>>51516682
An object may be generic, but generics are not objects. See >>51516580.

Technically, that's still wrong. A class may be generic, but an object is always specified.
>>
>>51515857
>it's been shown that one CAN code in an object-oriented fashion if they follow certain guidelines
yea like the '80s C++ ARM guidelines
>>
>>51516702

heh i think i see the confusion.

your initial statement was...

>OO for would be expressed just as well (if not better) using generics instead.

my background is c++, much of it before stl. and then java. "generics" as a term used to be (maybe still is) used as shorthand for a templated class. you in fact meant it as "generic programming" (something i loathe but thats by the by).
thus the confusion :^)
>>
>>51515857
>Procedural C Superior to OO?
I don't think there has been a study to prove this way or another. There is only anecdotal evidence.
My shitty opinion is that it doesn't matter, if there was a clear winner everybody would have naturally gravitated towards it, completely abandoning the other one.
>>
>>51517470
They're's at least one anon.

http://www.cs.ucr.edu/~neamtiu/pubs/icse11bhattacharya.pdf
>>
>>51517386
Generic classes and templates are a form of generic programming applied to OO programming. You can't possibly be proficient in C++ and the STL and loathe generic programming.

>>51517470
>My shitty opinion is that it doesn't matter
It's not that it doesn't matter, but that it's apples and oranges. There is no best, except maybe in the context of specific tasks.
>>
>>51517555
>There is no best, except maybe in the context of specific tasks.
like the long-term cost of code maintenance? a task like that anon?
>>
>>51517591
What is a StringBuilderStrategyAbstractFactoryManagerSingleton for 200, Alex?

What is properly supporting multithreading and improving cache access patterns?
>>
>>51517629
b-but java a shit anon.
>Sepples ftw Alex?
>>
>>51517651
Tell me how, even in C++, OOP serves to make multithreading and cache optimization easier.

It's the opposite, you need to stop thinking in such an OO mindset and start thinking procedurally, because it's not the patterns or human-understandable models or ways to be able to quickly replace that intern's shitty code that matter then, it's the way the data is laid out so that the execution model can use it with the least waste and tripping over its own feet.
>>
>>51517676
I'm not telling you any such thing anon. C++ is simply better all around, and is unbeatable in performance by any language when it comes to unifying complex disparate datasets and making them cache and stride-friendly using template metaprogramming.
>>
>>51517707
Alright, well I don't see what that has to do with OOP being a "universal truth" as >>51517591 is implying.
>>
>>51517729
Simply because the long term cost equation is much more favorable with that paradigm--at least with large scale projects.
>>51517530
>>
>>51517759
What makes OO more maintainable than well-designed modules?
>>
>>51517782
>well-designed modules
I suppose the fact that 'x' are hard to come by from within the purview of a typical C developers milieu. 'props 4rmuh clever coding bruh?' is all too commonplace, and seriously detracts from maintainability. OOP is a) is easier to come by and b) is automatically typed well from the beginning.
>>
>>51517782
My boss says so.
>>
Stupid thread full of stupid opinions by stupid people
>>
File: hqdefault.jpg (26 KB, 480x360) Image search: [Google]
hqdefault.jpg
26 KB, 480x360
>>51517884
Thanks for the solid contribution to the discussion friend.
>>
>>51517555
>be proficient in C++ and the STL and loathe generic programming.

yeah we're using the words differently, when i say i loathe generic programming i'm NOT referring to a templated or generic class. i'm referring to that style of programming which the stl uses. the one were you can copy one list to another, reverse it and change all occurrences of 3 to 4 except when there's an R in the month all in one line of code.
>>
>>51517986
>yeah we're using the words differently
I'm using the term the correct way.
>>
Ok how do you get polymorphism or inheritance without resorting to pig disgusting function pointers of writing lots of ifs that check a flag.

I'll wait.
>>
>>51516256
This way of thinking is suitable for some problems. For example, when programming a GUI, it can be convenient to represent all the scrollbars, buttons, checkboxes and textfields as classes with shared behaviors. But at other times, adding OOP may just make a trivial thing difficult.

It's really your call whether you should implement it or not.
>>
>>51518187
>polymorphism
Type quantification, type classes, dependent types

>inheritance
Just call a function from another function FFS

>my only experience in OOP is in contrast to C
>>
>>51518237
FYI, you may read "type quantification" as "generics"
>>
I thought OOP was a meme until I tried to write OO in a lisp. Holy shit was that wonderful
>>
>>51518002

well you just said "generics" to start with, not generic programming thus the confusion.
>>
>>51518261
Not him, I did. And I meant generics in the sense that Ada has them.
>>
>>51515857
I've found it's better to write procedurally. Structs are more than enough, you don't need object methods or inheritance.
>>
>>51518261
>I somehow think "generic programming" is different than "generics"
>>
>>51518002

I think it depends on the environment you are in, if you are a Java programmer, generics means something different than it does to a C++ programmer.
>>
>>51518284
>if you are a Java programmer, generics means something different than it does to a C++ programmer.
As a Sepples dev, I'm quite happy about that fact.
>>
embrace structs and procedural, your code will become much more elegant by pure necessity.
>>
>>51515857
Most larger C projects are done using patterns which mimic data abstraction by modularising and encapsulating with compilation units. For example something that might be a data abstraction or class would instead be defined as a series of functions in a header file, which would be defined in a separate .c file somewhere else.

It's more data abstraction than object oriented programming because it misses some of the integral features of OOP, such as inheritance and polymorphism, although these are provided in some larger projects like the Linux kernel through the use of e.g. compiler extensions or advanced use of preprocessor macros.

>is it better to code procedurally in C, or is it acceptable to use it as an object-oriented language?
Honestly the lines blur. After experience with data abstracted languages (or OOP) you will understand why it is often appropriate to abstract like this, and you will probably program like this. Most of the time it is definitely appropriate, and it's what most C programmers are used to. However, it has overhead. To pull it off people generally allocate all the data with the memory management library, and pass around pointers or other references to the data to use and possibly eventually delete the structures, which gets in the way of optimisations at times, and usually increases the amount of dereferencing required. In certain situations it can drastically increase overhead. In my opinion it's worth structuring heavily and using data abstraction anyway and then profiling and optimising only where needed later on. Usually choosing to structure a program with a lot of data abstraction does not affect the algorithmic complexity or ruin the efficiency of the implementation's logic.
>>
I'm no sjw and don't really give a fuck, but this turns out to be a reasonably well-turned discourse on topic. Well done, OP.
>>
>>51518269
If I'm making vidya and I want 2 very similar game objects(let's say 2 different types of zombie enemies), how do I avoid code duplication with structs?
>>
>>51515977
>>51516155
>>51516203
>>51516232
>>51516273
>>51516682
>>51516713
Generic programming has some overlap with other OOP techniques, arguing that one is better than the other doesn't make much sense since they're not solving the same problems. Comparing generic programming with dynamic dispatch is probably better, but sometimes the role of dynamic dispatch can't be replicated statically.
>>
File: 1436591670032.png (82 KB, 261x275) Image search: [Google]
1436591670032.png
82 KB, 261x275
It's all about organization of code... if you are grouping similar functions with your variables, you're essentially OO.

C is perfectly capable of doing OO. Even inheritance and stuff if you use it's pre-processor.

The first few generations of C++ compilers scrunched things down to C code.
>>
>>51518397
how about not having 2 very simialr game objects, just combine the best of each into one? then make a better second creature that isn't redundant?

don't you think redundancy is uninspring, as a player? it's boring, it's disappointing, and it doesn't add anything significant to the experience of playing the game.
>>
>>51518397

Create generic "base" struct, and have a pointer inside your zombie struct to it.

Or #include the lines you do not want to duplicate.
>>
http://yosefk.com/blog/oo-c-is-passable.html
Also
http://yosefk.com/c++fqa/index.html

Not trying to make a point really, it's just interesting reading.
>>
>>51518397
Composition.
Structs are really just public Objects. (that doesn't bundle memory alloc/init as "new" and the reverse as "delete")
>>
>>51520069
suuure. you've been trying to push these wares here for more than a year.
>>
File: 1427238221508.jpg (351 KB, 1200x900) Image search: [Google]
1427238221508.jpg
351 KB, 1200x900
>>51521072
Correct, and I think it's hilarious how C++ aficionados always (and I do mean always) ignore it whenever it's posted. It makes sense I guess. It's irrefutable; if you think anything he says is wrong then you A) don't know the language as well as you think you do, or B) are suffering from a mental disorder.
>>
Its very simple OP. Do you like doing boilerplate code? If yes, then sure, do it. If not, use something that helps you get rid of said boilerplate code
>>
>>51520370
>Structs are really just public Objects
>What the fuck am i reading.jpg
>>
I feel like OO only serves to make things into a clusterfuck at small levels. It's useful when working on large projects that need abstraction. If you are using OO for a small script or something that's under 1000 lines of code you're doing it wrong, unless it's serving to help readability.
>>
>>51520370
>>51522851
(sigh)
The problem for OP is that people who haven't don't low-level coding cannot conceive of the fact that their object oriented language had to be coded at one point as something else. They refuse to see the abstraction that was created in another language that becomes their language.

The OP is not articulating the fact that you can emulate object-oriented programming by coding programs in C using specific methodologies. It may not be "perfect" OOP but it will allow you to gain a portion of it. With a little more work - and a lot of supporting procedures - you can develop a framework that gains you the remaining percentage of functionality that is not present in C itself.

Remember, Python is written in C...how do they represent objects?

The JVM isn't 100% pure assembler, it was compiled from something else.

Ruby itself is written in...what?

C++ didn't start life as C++, it had to be written in...

Get the picture?

Now can we stop arguing in this thread and actually focus on shutting down the 10+ stupid gaming-based threads that should be on /v/?
>>
>>51522983
There's a pretty long way from classical C, to C with function pointers in structs, to the features in eg. Java. But yes
>>
>>51521346
Nah I've read it. He's just full of shit. I find it amusing you feel obligated to continually link his decrepit old hogwash as well. Come up with something original on you're own if you hate Sepples so much anon.
>>
File: 1447600266290.jpg (16 KB, 252x317) Image search: [Google]
1447600266290.jpg
16 KB, 252x317
>>51524867
>full of shit
Nope. Another C++ weenie in denial.
>>
>>51525568
>in denial.
Nah, just bankin it friend.

But you two enjoy each other's company though yeah?
>>
>>51516666
hi satan
Thread replies: 69
Thread images: 6

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.