What is "good code?"

What is "good code?"
What is "bad code?"

Other urls found in this thread:

people.mpi-inf.mpg.de/~uwe/misc/uw-ttyp0/
twitter.com/SFWRedditGifs

good code = my code
bad code = your code

Good code is readable, fast (for the language at least), and doesn't repeat itself.

Commenting = bad code. The code should speak for itself. You shouldn't need to walk people through your code.

Commenting can help in a lot of cases. I wouldn't discount it

Good code = my current code
Bad code = my code last week or older

this

Pajeet code = bad code

kek, this sums it up pretty much

Great code: small, efficient. Doesn't take up much space visually or on disk but gives big results. Fairly clear how/why it works; can be extended/reworked/refactored without too much stress.

Bad code: doesn't work. If it does, it's not clear why (or, it's bad enough that it actually shouldn't work, and two or more errors cancelling out make it magically work). At its absolute worst, it cannot be extended to any other problems that might arise in your work.

>Commenting = bad code. The code should speak for itself. You shouldn't need to walk people through your code.

Incorrect, or at least inaccurate.

Comments exist to make life easier for the reader (which might simply be you in one week). There's no obvious guidelines for commenting, but good comments will efficiently answer any questions that might arise from your code. This involves getting out of the autism-sphere and actually putting yourself in another person's shoes, which is difficult for programmers in general, but it's why coding is difficult.

To put it another way: if your project is big enough, you WILL need comments and you WILL need to learn good commenting tendencies. Period.

good code == your code
bad code === my code

Good code = Yegor's code
Bad code = anybody else's code

Good code = has automated tests
Bad code = doesn't have automated tests

Absolutely not. However, comments should explain "why" the structure was created, but rarely (if ever) "how" the code itself works.

Bad code is code that uses a single letter, or even a single word, as identifiers. I'm not saying you should use Hungarian notation, but you should always name your variables with descriptive identifiers. Iterators would be the exception ofc

Bad code is also an algorithm that was approached iteratively, and didn't get the d&c it deserves due to lazy programmers. That is to say, if one is solving a problem and just writing away, after he/she solves the problem, one should go back through, and properly document and encapsulate reusable code, etc. People don't do that, and they end up with a fragile mess. The iterative, red-green-refector approach is good, but you can't be a lazy shithead.

fpbp

a rarity on Sup Forums

$array = array();
$number = 3;
$result = $db->select($string);
if($result == true)
{
return $result;
}
else
{
return false;
}

Linus says if your code has 5 levels of indentation it sucks

commenting is good, you are bad.

my actual mental commentary when reading this
>number signs for variables, wow shit lang
>we got an array
>we got a number
>oh look its accessing an db now? and a string? we dont know where either of these came from.
>what is string
>lmao not using a ternary statement. literally 8 lines for nothing.
>ugh wait what does this garbage do again?

if you had a comment saying
>Query db for (what ever $string is) and return, or else return false.
This would be so much easier for me

I thought about adding a comment, but I wanted to write something in the style of my colleagues at my current job. Kind of my way of venting.

>9 layers of scope
this would be unreadable regardless of language

good code is something that is easy for other programmers to comprehend
bad code is something that is difficult for other programmers to comprehend

Good code is KISS and DRY.

Bad code is not KISS and DRY.

Simple.

Good code; readable and understandable by even a retard, barely executes
Bad code; executes flawlessly, barely readable

You should be writing bad undocumented code especially in a job setting.

Make yourself unfireable.
Even if they do fire and replace you, the new guy doesn’t know how it works, so they either call you in to explain (which you can demand money for) or get re-hired.

Gotta think about the bottom line friends

...

>The code should speak for itself.

Kill yourself.

Good code is:

1) Easy to grok
2) Efficient
3) Easy to maintain / modify / refactor

The hard part is that it's pretty much impossible to maximize all three of those metrics at the same time. Depending on the type of the project, you might need to put emphasis one of them, but it will always be at the cost of the other two to some degree.

Good code is commented so that they can give it to pajeet and fire you.
I never comment code.

only do this if you don't like your boss/company.

i've worked at startups where everybody is awesome and I try to write good, clean code. then I worked at a medium sized company filled with assholes and dickheads. oh my fucking god, nobody will ever be able to maintain the garbage I wrote there. get. fucking. rekt.

>startups where everybody
Good try hr, this doesn't exist, not in 2017 with mandatory wymin and diversity.

>Good code is fast
>The code should speak for itself
These could very well contradict.

>Commenting = bad code.

fuck off, Uncle Bob

>grok

i know what it means, but using this word somehow feels redditish

/thread

this

Underrated.

Good code is self-documenting, efficient and concise. Bad code contains redundant or unnecessary lines, getters and setters for variables, lots of comments, makes everything a class and/or does stupid things such as using data structures that are prone to cache misses or wasting RAM.

Better than python where a simple white space means your program wont run.

Make it look like this. If you have lots of params tab them.

LRESULT CALLBACK. MainWndProc(HWND, hwnd,
UINT uMsg,
LPARAM lParam,
WPARAM wParam)
{
//do stuff
return 0;
}

OwO wat is dat phont?

Exactly this

winrar

Good code can be read and understood by people who don't know how to program.

(•◘•) Every Haskell program is bad code.

...

Don't do this:
switch (foo) {
case a:
do_a();
break;
case b:
do_b();
break;
case c:
do_c();
break;
}

Do this instead:
switch (foo) {
case a:
do_a();
break;
case b:
do_b();
break;
case c:
do_c();
break;
}

I use a language with a good compiler

This

>Good code
It works
It has documentation
it's persistent
>Bad Code
Opposite of good code

Good code is readable on 80x24

>small, efficient. Doesn't take up much space
like ur dick

Good code
>it works
>written completely in English
>commented and documented in English
>has an automated test suite
>uses a versioning system (git) to keep track of changes
>peer-reviewed by a group of independant software engineers

Bad code
>lacks any of the features above
E.g.
print("Hello, world.")

is bad code.

r8

>he doesn't use getters and setters
it's like you want to break abstraction barriers senpai

>Bad code contains redundant or unnecessary lines, getters and setters for variables
>Respecting encapsulation is bad code

In a few, select cases. I would discount it as a guideline.

You just haven't gitten good yet.

No, it's the other way around.

These are very good guidelines.

>not making your code as compact as it can be
pshhhh, nothin personnel... kid
: "SR" DUP PI NEG < IF 2PI + ELSE DUP PI > IF 2PI - THEN THEN ;
: SIN 2PI % "SR" >R 136D8E I F* 62F20 I F* I F* I 0 < IF + ELSE - THEN R> DROP ;
: COS 17F7E6 + SIN ;

...

Meta af

>he/she

Good code:
procedure Main is
begin
Put_Line("hello, world!");
end Main;

Medium good code:
int
main(void)
{
puts("hello, world!");
return EXIT_SUCCESS;
}

Medium code:
_start:
movl $4, %eax
movl $1, %ebx
movl $msg, %ecx
movl $14, %edx
int $0x80
movl $1, %eax
movl $0, %ebx
int $0x80

Medium bad code:
func main() {
fmt.Println("hello, world!")
}

Bad code:
(define (main) (display "hello, world!"))

Comedy code:
class Program {
void main() {
System.out.println("hello, world");
}
};

What if you have to send an arbitrary bit pattern to a peripheral? No comments?

>he likes useless abstraction

That's more for you when you get back to a certain point after working on something else for a week.
Unless you're some Hollywood-style super genius (which doesn't exist), you're just screwing yourself and slowing down the whole process because now you have to re-learn EVERYTHING you wrote rather than just skimming a quick descriptive comment or doc and instantly recollecting everything you did as you need it.

good code is benevolent
bad code is malevolent

Good code is code that completes the desired behavior in the most simple and comprehend able way.

Bad code is code that completes the desired behavior with unnecessary elements, not commented, and uncomprehendable identifiers.

...

...

...

This desu

I find that funny when these grads buying into this OOP bullshit
Have you tried to actually design programs rather than satisfy artificial paradigms?

>Commenting = bad code. The code should speak for itself. You shouldn't need to walk people through your code.
Go to school

>Go to school
if you had went to schol you wouldnt have to comment every single line to understand what you write yourself lmao

>Have you tried to actually design programs rather than satisfy artificial paradigms?
It's my job and yeah abstraction, encapsulation, and SoC are king in the real world. It's not just OOP bullshit.

"good code" is ANSI C
"bad code" is python or java

is C# good code?

>which might simply be you in one week
People say this all the time, but I call bullshit. I have zero trouble reading code I wrote 10+ years ago completely uncommented.

I do leave some comments around for really unobvious stuff, like if my code does something that interacts weirdly with external components or something, but those comments are few and very far between.

Moderately descriptively named constants for the bit patterns (I might even go so far as to use an entire word for this), along with the bitwise operators that describes how and why the are combined the way they are. If the reader needs more information than that, then he should read the docs for the peripheral.

good code works
bad code doesn't

>abstraction, encapsulation, and SoC are king in the real world
Of course they are, but only when they are useful. Everything else is merely imagined discipline.

I have nothing against abstraction, encapsulation and SoC.
Just OOP implements them not really well.

Maintenance is also a very important criterion.

I don't know what it is but seeing tons of ALL CAPS TYPES makes me feel like a real programmer.

I mean...that bracketing style just make it python "white space matters" style programming. This guy obviously has something against Python syntax.

Everytime someone mentions that they write self commenting code I know that they are retarded. It's actually a very convenient metric to apply.

who gives a shit, all code sucks a week from now

>he has to manually define getters and setters
>not simply call
(property obj)
(setf (property obj) x)

i shig at you blubfags

I think "I can't read this perl script" can also mean "I wrote this perl script"

or choose a language that breaks by default:
switch foo {
case a: do_a()
case b: do_b()
case c: do_b()
}

lrn j

Bad code is hard to scale up and maintain.

No.
It bugs the fuck out of me that gofmt forces this. It's retarded.

But each "case" is logically subordinate to the "switch", therefore they should be indented for consistency.

it is

good code doesn't exist

code that is the best approximation to "good" is the code best optimised for change:

change of other pieces of code
change of algorithm
change of business logic
change of platform
change of programmer

>Commenting = bad code
if you only write hobby projects for yourself, sure

...

General Rule of thumb:
If you can't explain the Top level Architecture of your Program in less than 5-10 minutes you have spaghetti code.

If you can explain it using nothing more than a few labeled boxes, then you have good code.

Creator of that screenshot here
I'm glad my shitty OC was put to good use

ttyp0
people.mpi-inf.mpg.de/~uwe/misc/uw-ttyp0/

Good code is any code
Bad code are people

i like