[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
How do people learn to write professional looking C++ code ?
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: 74
Thread images: 9
File: cpp.jpg (45 KB, 340x255) Image search: [Google]
cpp.jpg
45 KB, 340x255
How do people learn to write professional looking C++ code ? Is there a template how to create directories full of stuff I'm not aware of?
>>
>>55189244
>professional looking C++ cod
Could you give examples?
>>
>>55189269
I'd guess no
>>
>Is there a template how to create directories full of stuff I'm not aware of?

Companies sometimes make their own templates.

Here's how google writes Java for example.

https://google.github.io/styleguide/javaguide.html#s4.1.2-blocks-k-r-style
>>
>>55189244
Look out for Carmack's Doom and Quake code.
>>
>>55189244
clang-format
>>
>>55189269
When I write code it's a few cpp and h files, maybe makefiles if I rally want to stretch it.

But when I browse github every little thing is like a forest of shit. That makes it look more complex than it is.

You know what I mean?
>>
>>55189449
each class gets it's own file

each class that is inherited from a class gets it's own file

there, you may now attend your designated shitting street

(multiple inheritance is slow trash btw)
>>
>>55189365
source code of decfon and uplik was released too
>>
>>55189484
Do you think they do it all by hand? How is that practical ?
>>
>>55189580
Fuck you we have a space program India will be a superpower by 2030
>>
>>55189580
>Do you think they do it all by hand?
They have their IDE that will do all of the getters and setters

Also, these projects probably have multiple people working on them
>>
>>55189484
lelele
>>
>>55189244
>template
hehehe
>>
>>55189484
>(multiple inheritance is slow trash btw)
what do you autists even mean when you say shit lik this?

it's probably a couple nanoseconds slower
no one outside the spectrum gives a shit
>>
>>55190636
>a table lookup of virtual functions plus the actual call is only a few nanoseconds slower

I bet you use exceptions too
>>
>>55190671
>is only a few nanoseconds slower
yes it is unless you're on a 20 year old spergbox
>>
I've seen code written by hundreds of different professionals and none of them have the same coding style.

The best way to make your code look professional is to avoid the most common coding mistakes, such as uninitialized variables, unhandled exceptions, bad variable names or very large methods.

It also helps if you write comments in your code, explaining the intention behind what every method or code section does, but most people don't bother writing that anyways.
>>
>>55190738
>It also helps if you write comments in your code
If there is a need for this then you're most likely already going the wrong way about it

Your variable, method and class names should give away that a piece of code is supposed to do, unless you're doing something really complex
>>
>>55189449
>When I write code it's a few cpp and h files, maybe makefiles if I rally want to stretch it.
Because you're most likely making little monolithic turds riddled with completely unreadable, obfuscated shitcode, just like most of /dpt/

Professional code is """complex""" (it isn't, you're just retarded) because it follows some set architectural and design principles, while also making it available for other people to read and extend/fork.
>>
File: Capture.png (5 KB, 168x302) Image search: [Google]
Capture.png
5 KB, 168x302
>>55190806
Anything more complex than this is unnecessary
>>
>>55190754
You have no idea of what you are talking about
>>
>>55190913
>video games
in the trash
>>
>>55190913
hehe have a look at some professionnal / commercial level software, like the Cryengine.
>>
>>55190671
Functions are not virtual unless you explicitly mark them to, Pajeet. Git gud.

Sure you can do a class that inherits from multiple other abstract classes, you can also eat your own shit but it doesnt necessary mean you HAVE to. But well, you probably do.
>>
>>55190913
Wow, you've got 10 additional files! Why dont you put all this shit into main!?
Anything more than 1source is too much
>>
The thing you need to learn is cmake.

.h/hpp files is mainly for documentation.
There you declare the functions and give doxygen documents.
Avoid putting the code here (although sometimes one liners are accepted here)
.cpp files is where you implement the functions.
If there is something weird or important, make a comment, but this part should be self documenting.

As for folder structure:
I usually create a lib, bin and build folder when I compile.
I use a cmakelists.txt for the overall project, which is in the root folder.
Then I create a lot of sub folders for different parts of the project.
Each of those sub folders have their own cmakelist.txt so it is easier to manage.

The overall cmake file has the settings etc.
Then the cmake files in the folders are the more specific stuff, where you include the files etc.
>>
>>55191816
It's funny how I've been programming in c++ for over ten years and never cared for making lib, bin, or build folders. I mean, Visual Studio did everything for me, so I guess it was something I never really cared for. Nor have I ever cared for making build or make batch files. It's the little things like this that have me realize sometimes that I'm not as knowledgeable as I should be when it comes to these things. But so far, I haven't really needed it in my professional life.
>>
File: hans and guenther.jpg (452 KB, 985x554) Image search: [Google]
hans and guenther.jpg
452 KB, 985x554
>>55189244
writing good code is more about design decisions.
and besides that, programmers spend very little time actually programming. most time is spent on reading documentations, source code and trying out solutions to problems until you have the best one.
actual development is mostly research and reading. the actual programming part takes up very little of that overall time because by the time you want to implement your solution to a problem you have (hopefully) found the best way to do it.
>>
>>55191639
Being this retarded.
>>
>>55190806
And this is overrated, because "professional" software is worked on by dozens, if not hundreds, of employees, some of them brand fucking new to the project, and your software is not.

Not everything associated with "profeshenullllls" is a good thing. Sometimes, it's a horrible compromise meant to cater to that retarded sperg they just hired to replace the skilled, socially normal programmer that retired last week.
>>
>>55191950
Just because your IDE does it for you, doesn't mean you shouldn't know how it should be done.
At least you should know the file structure.
>>
File: lol8 (2).jpg (24 KB, 150x178) Image search: [Google]
lol8 (2).jpg
24 KB, 150x178
>>55190913
Yeah, if your entire codebase is <1000 LOC.
>>
>>55193013
Creating sub folders for more complex things is allowed only if the structure stays the same. The other option is MVC everything else is shit
>>
>>55193069
>no tests
>Creating sub folders for more complex things is allowed only if the structure stays the same
>everything else is shit
Well done, Pajeet.
>>
>>55192112
>it's a horrible compromise meant to cater to that retarded sperg
that's not the goal at all, what the fuck

it's just that the codebase will turn into an incomprehensible shitfest if a bunch of people start doing things their own way
readability and other common standards also speed up development of projects for a developer of any skill level, because no one likes churning through a million lines of inconsistent spaghetti code
>>
>>55192112
>Sometimes, it's a horrible compromise meant to cater to that retarded sperg they just hired to replace the skilled, socially normal programmer that retired last week.
>the worst-case scenario is bad, therefore we should write minimalistic and unmaintainable code
>>
>>55193110
>TFW working for multi million banking company as software engineer and some random 16 y/o 1337 h4x0r is calling me Pajeet over the internet
>>
>>55193438
>1) Given that the bank's worth is in 8 digits or less, it's a glorified piggy bank
>2) You're working for a banking company and asserting (pun intended) that having no tests is okay
>3) Calling the guy who supports comprehensive testing a teenager, are you for real?
I'm calling bullshit. BTW, I'm 27 and I work for a software company that makes $60mil a year.
>>
isocpp.org
>>
>>55191639
>Don't know if sarcastic or not
I thought this was bad practice, I also put all my code in main.cpp
>>
>>55189365
>>55189510

This. When learning c++ i got through the intro stuff and started reading carmacks code since everyone blows their load over it.
>>
>>55190738
>>55190754
>>55190921

Hes right shitter. The only comment you should write is a description at the start. If you feel the need to write in the middle.you should split shit up so it is simpler.

Go read Carmacks shit. He puts it this way commenting is doubling the work for you and whoever maintains it since you have to now write everything twice and someone has to read it all twice.

If its so complex it needs to be explained make it less complex.
>>
>>55192112
>Not everything associated with "profeshenullllls" is a good thing. Sometimes, it's a horrible compromise meant to cater to that retarded sperg they just hired to replace the skilled, socially normal programmer that retired last week.

This. Companies are not interested in the best way to do things. They want the cheapest. And that is why youll will come across so much shit in your life.
>>
>>55194260
>Not adding comments. You haven't done anything more complex than 8 queens
>>
>>55194299

If it needs to be explained it needs to be split up. Anything more complex will be a pain to maintain no matter how much you write. Computers are machines that execute the simplest commands imaginable. You have options to simplify your shit.
>>
>>55194343
Yeah computers execute simple commands.
Also try programming on assembler without comments
>>
>>55189449
>>55190913
>>55190921
>>55193069
>>55192112
>>55194299
so many shit-eaters and street-shitters on /g/ these days
>>
>>55194415
You would think they'd cancel each other out
>>
>>55194421
kek
>>
>>55190671
>MUH 40 CLOCK CYCLES
>>
>>55194397

You can split your shit up into statements that are immediately apparent without going assembly.

Writing comments is writing the same thing twice you dont need to do it if you wrote it write the first time.
>>
>>55189244
Fuck C++ and use Elixir.
>>
>>55195632
stop fucking posting this everywhere, shill
>>
>>55195822
Fine... use Rust.
>>
File: sepples casting.png (178 KB, 1352x1172) Image search: [Google]
sepples casting.png
178 KB, 1352x1172
>>55189244
>professional looking C++ code
What does this even mean?
>>
>>55193438
do you even know where you are?
>>
File: 1214267302955.jpg (6 KB, 186x200) Image search: [Google]
1214267302955.jpg
6 KB, 186x200
>>55195992
>That parenthetical spacing
>>
>>55195992
>butt_sp*
god that code is ugly
>>
>>55196150
Don't worry, doesn't look like that anymore. Actually, used to look worse.

>>55196162
:^)
>>
>>55196304
i'm not trolling, faggot, your code looks like ass.
and yes, i know that code interfacing with lua is going to look shitty, but at least clean that dogshit up a bit. why do you have a fucking POINTER to a shared_ptr? literally retarded.
>>
>>55196465
>fucking POINTER to a shared_ptr? literally retarded
I need a shared_ptr for compatibility, as the rest of the code references them by shared_ptr. lua_newuserdata() takes a Lua state and an allocation size, and returns a pointer to the allocated, managed memory, which I then placement construct. I then call the destructor manually in the GC function. How would you have done it?
>>
>>55196538
>How would you have done it?
i dunno, i'm just a shitposting /g/entooman. also i am a little drunk.

funny, but i am involved in a project that uses lua for scripting.

why the additional indirection? it looks like lua is returning a pointer to its own memory, and you want to cast that to a shared_ptr which points to a button.
>>
>>55196843
>cast
Nope, it's a pointer to space allocated for a shared_ptr. Essentially it's like this:
// Get enough Lua memory to hold shared_ptr to image_rsrc
std::shared_ptr< image_rsrc >* rsrc_sp = ( std::shared_ptr< image_rsrc >* )lua_newuserdata(
state,
sizeof( std::shared_ptr< image_rsrc > )
);

// Placement construct shared_ptr with a pointer to a new image_rsrc
new( rsrc_sp ) std::shared_ptr< image_rsrc >(
new image_rsrc(
lua_tostring( state, 1 ),
lua_tonumber( state, 2 ),
lua_tonumber( state, 3 ),
lua_tonumber( state, 4 ),
lua_tonumber( state, 5 )
)
);

// ... Later on during GC, manually call destructor
rsrc_sp -> ~shared_ptr< image_rsrc >();

I'm using shared_ptr to begin with because a given resource/element may be shared across several internal interfaces and/or be a child of more than one other element. Also C++11.
>>
>>55190754
Dumbshit.
>>
This is the directory of a small project I was working on:
├── CMakeLists.txt
├── cmake_modules
├── config
│ ├── config.hpp
│ └── config.hpp.in
├── COPYING
├── include
│ └── projectname
│ ├── common
│ ├── handlers
│ └── system.hpp
├── LICENSE
├── lua
│ └── projectname
├── README.md
└── src
└── projectname
├── bindings
├── main.cpp
├── handlers
└── system.cpp


It was for a small shitty game engine. All things considered I thought the structure was alright.
I guess just at key points in the project you'll realize that it makes sense to group some of the files and put them in a directory.

In mine for example, I had to create a bunch of .cpp files used to interface with various parts of SFML, so they all went in the bindings directory in src (i.e. keyboard.cpp, mouse.cpp, sprite.cpp, texture.cpp). It just made sense to group them at the time, and the result is a neat directory structure.
>>
bump?
>>
File: 1401556365194.png (69 KB, 225x225) Image search: [Google]
1401556365194.png
69 KB, 225x225
>>55189484
>there, you may now attend your designated shitting street
>>
>>55190636
multiple inheritance has no performance impact.
>>
File: brrrrrrrrrrrrrrrrt.webm (2 MB, 1280x720) Image search: [Google]
brrrrrrrrrrrrrrrrt.webm
2 MB, 1280x720
>>55189449
>>
>>55192037
Being ncapable of spotting simple sarcasm.
>>
>>55195425
Wow its almost like illiterate pajeet isn't a thing
People write comments to educate the uneducated or incompetent
>>
lot's of good ideas in this thread, keep them coming
>>
>>55201164
no, this thread is poor with poor suggestions for poor programmers.

the fact that you had to ask means that either there is no hope for you, or you just enjoy trolling /g/. for your sake, I hope it is the latter.
Thread replies: 74
Thread images: 9

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.