Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 06:28:03 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)The "Elegant" Code Thread
Pages: 1 [2] 3 4
Print
Author Topic: The "Elegant" Code Thread  (Read 5773 times)
Ordnas
Level 10
*****



View Profile WWW
« Reply #20 on: November 04, 2017, 01:04:18 AM »

I remember Bjarne Stroustrup mentioning in his book his ideal lines per function of around 10, against the 40 lines per function that much everyone else consider the optimal one. I am confortable with that, it is more probable that it will come out a more modular code.

Opinions differ on whether one should listen to Bjarne Stroustrup when it comes to programming but regardless of whoever says it I highly disagree with that and prefer this as external advice if any should be given:

http://number-none.com/blow/john_carmack_on_inlined_code.html

In my code you will find super short functions but you'll also find one or two that are a few hundred lines of code - my recent prototyping playground has like three 1k+ line functions. Now I'd say that's probably too much but I don't think you can always chunk up code into 10, 20, 30, 40 or even 100 line segments without hurting readability.

As with most things I agree entirely with John C. here. I generally only break code up into functions if there's repeated code. If a function is doing the one thing it's designed to do, and there's no repetition in the code then I really don't care how long it gets. There's nothing I hate more than having to jump all over a file just to follow a straight line of logic. Coffee

Very interesting the link to the page. I agree to do not create a function if that do not solve repeated code. Jumping from function to function can break the readability, but I suspect that if there are too many jumps from different source files, could that be a sign of a bad design choice? I do not expect more that 10 function jumps to accomplish the result you need.
Logged

Games:

J-Snake
Level 10
*****


A fool with a tool is still a fool.


View Profile WWW
« Reply #21 on: November 05, 2017, 04:21:30 PM »

It actually doesn't matter how long the code is and whether it is repeated or not. It all comes down to this: When a code sequence forms a significant logical unit with respect to your point of view, put it in a sound function.
Logged

Independent game developer with an elaborate focus on interesting gameplay, rewarding depth of play and technical quality.<br /><br />Trap Them: http://store.steampowered.com/app/375930
DireLogomachist
Level 4
****



View Profile
« Reply #22 on: November 05, 2017, 05:08:07 PM »

It actually doesn't matter how long the code is and whether it is repeated or not. It all comes down to this: When a code sequence forms a significant logical unit with respect to your point of view, put it in a sound function.

Hear hear!
Logged


Living and dying by Hanlon's Razor
oahda
Level 10
*****



View Profile
« Reply #23 on: November 05, 2017, 10:29:12 PM »

I mean, number of lines is really arbitrary depending on one's style too. I try to keep things readable and therefore avoid having to scroll horizontally as much as possible, and if a function (like some OpenGL functions for example) take a lot of parameters, I'll split them up on one line each and suddenly we go from one line to 10+.

Code:
glTexImage2D(GL_TEXTURE_2D, 0, mode, static_cast<GLsizei>(width()), static_cast<GLsizei>(height()), 0, mode, GL_UNSIGNED_BYTE, pixels());



Code:
glTexImage2D
(
GL_TEXTURE_2D,
0,
mode,
static_cast<GLsizei>(width()),
static_cast<GLsizei>(height()),
0,
mode,
GL_UNSIGNED_BYTE,
pixels()
);

But maybe that counts as one line according to such measurements because there is only one semicolon?
Logged

JWki
Level 4
****


View Profile
« Reply #24 on: November 05, 2017, 11:53:41 PM »

I mean, number of lines is really arbitrary depending on one's style too. I try to keep things readable and therefore avoid having to scroll horizontally as much as possible, and if a function (like some OpenGL functions for example) take a lot of parameters, I'll split them up on one line each and suddenly we go from one line to 10+.

Code:
glTexImage2D(GL_TEXTURE_2D, 0, mode, static_cast<GLsizei>(width()), static_cast<GLsizei>(height()), 0, mode, GL_UNSIGNED_BYTE, pixels());



Code:
glTexImage2D
(
GL_TEXTURE_2D,
0,
mode,
static_cast<GLsizei>(width()),
static_cast<GLsizei>(height()),
0,
mode,
GL_UNSIGNED_BYTE,
pixels()
);

But maybe that counts as one line according to such measurements because there is only one semicolon?

You should get a wider screen. :p  also yeah that only counts as one in my book.
Logged
oahda
Level 10
*****



View Profile
« Reply #25 on: November 06, 2017, 12:19:44 AM »

I actually have a high resolution even in full laptop mode, but as a result I cram a lot of stuff onto one screen instead, like multiple code editors side by side, and I want to be able to read them all at the same time without scrolling or resizing too much. Tongue

Logged

Ordnas
Level 10
*****



View Profile WWW
« Reply #26 on: November 06, 2017, 01:01:26 AM »

I mean, number of lines is really arbitrary depending on one's style too. I try to keep things readable and therefore avoid having to scroll horizontally as much as possible, and if a function (like some OpenGL functions for example) take a lot of parameters, I'll split them up on one line each and suddenly we go from one line to 10+.

Code:
glTexImage2D(GL_TEXTURE_2D, 0, mode, static_cast<GLsizei>(width()), static_cast<GLsizei>(height()), 0, mode, GL_UNSIGNED_BYTE, pixels());



Code:
glTexImage2D
(
GL_TEXTURE_2D,
0,
mode,
static_cast<GLsizei>(width()),
static_cast<GLsizei>(height()),
0,
mode,
GL_UNSIGNED_BYTE,
pixels()
);

But maybe that counts as one line according to such measurements because there is only one semicolon?

You should get a wider screen. :p  also yeah that only counts as one in my book.

Usually if I need to pass a lot of arguments I pack everything in a struct.
Logged

Games:

JWki
Level 4
****


View Profile
« Reply #27 on: November 06, 2017, 01:46:26 AM »

I mean, number of lines is really arbitrary depending on one's style too. I try to keep things readable and therefore avoid having to scroll horizontally as much as possible, and if a function (like some OpenGL functions for example) take a lot of parameters, I'll split them up on one line each and suddenly we go from one line to 10+.

Code:
glTexImage2D(GL_TEXTURE_2D, 0, mode, static_cast<GLsizei>(width()), static_cast<GLsizei>(height()), 0, mode, GL_UNSIGNED_BYTE, pixels());



Code:
glTexImage2D
(
GL_TEXTURE_2D,
0,
mode,
static_cast<GLsizei>(width()),
static_cast<GLsizei>(height()),
0,
mode,
GL_UNSIGNED_BYTE,
pixels()
);

But maybe that counts as one line according to such measurements because there is only one semicolon?

You should get a wider screen. :p  also yeah that only counts as one in my book.

Usually if I need to pass a lot of arguments I pack everything in a struct.

Yeah same, but I guess this was also including library functions you have to call.
I have lots of functions that would take quite a lot of arguments so I too just pass a struct - similiar to what Vulkan or most Windows/D3D APIs do, but I usually initialize the struct with sensible defaults. One of the advantages of C++.
Logged
qMopey
Level 6
*


View Profile WWW
« Reply #28 on: November 06, 2017, 01:55:49 AM »

Never really liked C++ style casts. To this day still just use C casts (except at work, they have a policy to use C++ casts). Personally I find the C++ casts verbose and sort of pointless.
Logged
oahda
Level 10
*****



View Profile
« Reply #29 on: November 06, 2017, 02:15:07 AM »

Sure, I try to write my own functions so that they don't need to be split up like that but with external API's I have to play by their rules. Tongue

As for casts, I think it's nice to declare the purpose of each cast a little more explicitly.
Logged

qMopey
Level 6
*


View Profile WWW
« Reply #30 on: November 06, 2017, 02:43:53 AM »

As for casts, I think it's nice to declare the purpose of each cast a little more explicitly.

Oh definitely. It's too bad static_cast<thing>(other_thing) and friends are so verbose  Cry
Logged
powly
Level 4
****



View Profile WWW
« Reply #31 on: November 06, 2017, 04:41:01 AM »

The only casts I’ve done that verbosely have been reinterpret_casts for CRTP (which btw usually results in beautifully elegant user code and absolutely terrible library code), which unfortunately is even longer than static_cast No No NO

Is casting from unsigned to GLuint even an actual cast though? I’m relatively confident they should be the same type in a sane setting (no embedded systems involved)
Logged
Ordnas
Level 10
*****



View Profile WWW
« Reply #32 on: November 07, 2017, 12:56:57 AM »

Is casting from unsigned to GLuint even an actual cast though? I’m relatively confident they should be the same type in a sane setting (no embedded systems involved)

In Windows, it is just a typedef, gl.h line 149: https://stackoverflow.com/questions/8932912/whats-the-advantage-of-using-gluint-instead-of-unsigned-int
then on different systems unsigned int has different sizes.
Logged

Games:

oahda
Level 10
*****



View Profile
« Reply #33 on: November 07, 2017, 03:15:44 AM »

It was, because for some reason, my width and height weren't unsigned, but of course they should be. Fixed now. Tongue
Logged

Photon
Level 4
****


View Profile
« Reply #34 on: November 08, 2017, 01:46:34 PM »

Sorry guys! I kinda disappeared for a bit because I was moving homes. Thanks for some interesting conversation!

Quote
1. Rule of Modularity: Write simple parts connected by clean interfaces.
2. Rule of Clarity: Clarity is better than cleverness.
3. Rule of Composition: Design programs to be connected to other programs.
4. Rule of Separation: Separate policy from mechanism; separate interfaces from engines.
5. Rule of Simplicity: Design for simplicity; add complexity only where you must.
6. Rule of Parsimony: Write a big program only when it is clear by demonstration that nothing else will do.
7. Rule of Transparency: Design for visibility to make inspection and debugging easier.
8. Rule of Robustness: Robustness is the child of transparency and simplicity.
9. Rule of Representation: Fold knowledge into data so program logic can be stupid and robust.
10. Rule of Least Surprise: In interface design, always do the least surprising thing.
11. Rule of Silence: When a program has nothing surprising to say, it should say nothing.
12. Rule of Repair: When you must fail, fail noisily and as soon as possible.
13. Rule of Economy: Programmer time is expensive; conserve it in preference to machine time.
14. Rule of Generation: Avoid hand-hacking; write programs to write programs when you can.
15. Rule of Optimization: Prototype before polishing. Get it working before you optimize it.
16. Rule of Diversity: Distrust all claims for “one true way”.
17. Rule of Extensibility: Design for the future, because it will be here sooner than you think.

Oof, ignoring the "Rule of Economy" is probably something that shoots me in the foot way more than it should.

As another aside, I'm one of those people who has never really latched onto the "only x lines per function" idea. I would side with J-Snake on this one: you write a function as a cohesive unit of logic.
Logged
Ordnas
Level 10
*****



View Profile WWW
« Reply #35 on: November 09, 2017, 12:51:29 AM »

Sorry guys! I kinda disappeared for a bit because I was moving homes. Thanks for some interesting conversation!

Quote
1. Rule of Modularity: Write simple parts connected by clean interfaces.
2. Rule of Clarity: Clarity is better than cleverness.
3. Rule of Composition: Design programs to be connected to other programs.
4. Rule of Separation: Separate policy from mechanism; separate interfaces from engines.
5. Rule of Simplicity: Design for simplicity; add complexity only where you must.
6. Rule of Parsimony: Write a big program only when it is clear by demonstration that nothing else will do.
7. Rule of Transparency: Design for visibility to make inspection and debugging easier.
8. Rule of Robustness: Robustness is the child of transparency and simplicity.
9. Rule of Representation: Fold knowledge into data so program logic can be stupid and robust.
10. Rule of Least Surprise: In interface design, always do the least surprising thing.
11. Rule of Silence: When a program has nothing surprising to say, it should say nothing.
12. Rule of Repair: When you must fail, fail noisily and as soon as possible.
13. Rule of Economy: Programmer time is expensive; conserve it in preference to machine time.
14. Rule of Generation: Avoid hand-hacking; write programs to write programs when you can.
15. Rule of Optimization: Prototype before polishing. Get it working before you optimize it.
16. Rule of Diversity: Distrust all claims for “one true way”.
17. Rule of Extensibility: Design for the future, because it will be here sooner than you think.

Oof, ignoring the "Rule of Economy" is probably something that shoots me in the foot way more than it should.

As another aside, I'm one of those people who has never really latched onto the "only x lines per function" idea. I would side with J-Snake on this one: you write a function as a cohesive unit of logic.

The list of rules are very inspiring to write better code for sure. I am wondering if that would be always possible, mainly in game programming, where the time constrain invites you to write fast-not reusable code.
Logged

Games:

Photon
Level 4
****


View Profile
« Reply #36 on: November 09, 2017, 06:40:07 AM »

Sorry guys! I kinda disappeared for a bit because I was moving homes. Thanks for some interesting conversation!

Quote
1. Rule of Modularity: Write simple parts connected by clean interfaces.
2. Rule of Clarity: Clarity is better than cleverness.
3. Rule of Composition: Design programs to be connected to other programs.
4. Rule of Separation: Separate policy from mechanism; separate interfaces from engines.
5. Rule of Simplicity: Design for simplicity; add complexity only where you must.
6. Rule of Parsimony: Write a big program only when it is clear by demonstration that nothing else will do.
7. Rule of Transparency: Design for visibility to make inspection and debugging easier.
8. Rule of Robustness: Robustness is the child of transparency and simplicity.
9. Rule of Representation: Fold knowledge into data so program logic can be stupid and robust.
10. Rule of Least Surprise: In interface design, always do the least surprising thing.
11. Rule of Silence: When a program has nothing surprising to say, it should say nothing.
12. Rule of Repair: When you must fail, fail noisily and as soon as possible.
13. Rule of Economy: Programmer time is expensive; conserve it in preference to machine time.
14. Rule of Generation: Avoid hand-hacking; write programs to write programs when you can.
15. Rule of Optimization: Prototype before polishing. Get it working before you optimize it.
16. Rule of Diversity: Distrust all claims for “one true way”.
17. Rule of Extensibility: Design for the future, because it will be here sooner than you think.

Oof, ignoring the "Rule of Economy" is probably something that shoots me in the foot way more than it should.

As another aside, I'm one of those people who has never really latched onto the "only x lines per function" idea. I would side with J-Snake on this one: you write a function as a cohesive unit of logic.

The list of rules are very inspiring to write better code for sure. I am wondering if that would be always possible, mainly in game programming, where the time constrain invites you to write fast-not reusable code.
They'd probably be better as something you reflect on every once in awhile. Ironically, I'd guess the "Rule of Economy" works against several of these rules from time to time.
Logged
bateleur
Level 10
*****



View Profile
« Reply #37 on: November 09, 2017, 08:17:18 AM »

Ironically, I'd guess the "Rule of Economy" works against several of these rules from time to time.
Which is good, because that reflects the nature of programming as an activity. No style guide can tell you how to resolve tradeoffs in specific cases.
Logged

Photon
Level 4
****


View Profile
« Reply #38 on: November 09, 2017, 09:06:23 AM »

Ironically, I'd guess the "Rule of Economy" works against several of these rules from time to time.
Which is good, because that reflects the nature of programming as an activity. No style guide can tell you how to resolve tradeoffs in specific cases.
As someone who has been trying to incorporate ECS into nearly everything for the last year, I can attest to this. Giggle
Logged
popawheelie
Level 0
***


View Profile
« Reply #39 on: November 21, 2017, 09:59:09 AM »

My go to rule has always been that you should be able to understand what the code is doing in less than 15 seconds. If it takes longer: rewrite, improve formatting or comment.

Sometimes Ill take a slightly less efficient route if it's easier to understand down the line.
Logged

Pages: 1 [2] 3 4
Print
Jump to:  

Theme orange-lt created by panic