DEV Community

Cover image for Don't be a Bracist

Don't be a Bracist

Johannes Vollmer on November 10, 2017

Trigger warning: This post assumes that racism should be avoided. Despite being a student, I already know how time-consuming it can be to dig thro...
Collapse
 
jasongabler profile image
Jason Gabler • Edited

Awesome title :D

Over the years I've relaxed my brace and paren attitudes. If someone has been consistent, after a few minutes I can get the feel of their style and focus on the logic of their code and get hacking.

What still ties me up in knots, and this is about my own coding habits, not other people's code, is long lists of function parameters. I can never decide which is more appealing to the eyes, easier to read, and all while conserving vertical space. So...

public function someBigLongFunctionNameThatTakesLotsaSpace( $paramOne, $paramTwo, $paramThree, $paramFour) {
...
...
}

or

public function someBigLongFunctionNameThatTakesLotsaSpace( $paramOne,
                                                            $paramTwo, 
                                                            $paramThree,
                                                            $paramFour) {
...
...
}

or

public function someBigLongFunctionNameThatTakesLotsaSpace( $paramOne, $paramTwo, 
                                                            $paramThree, $paramFour) {
...
...
}

or

public function someBigLongFunctionNameThatTakesLotsaSpace(
    $paramOne,
    $paramTwo, 
    $paramThree,
    $paramFour) {
...
...
}
Collapse
 
paayaw profile image
Derek Owusu-Frimpong

Probably the last one is better. For me, I try to prevent my eyes travelling far to the right. That makes me lose focus easily.

Collapse
 
johannesvollmer profile image
Johannes Vollmer

Agreed. Also, I don't mind having multiple parameters in one line, but I really need them to be on the left side.

Collapse
 
johannesvollmer profile image
Johannes Vollmer

Thanks :D

Collapse
 
maxx0r profile image
Max

Another readability hint, don't pass operations as arguments (the glovesAreAvailable). I've stopped doing this a long time ago and it really helps maintenability.

It's nitpicking, I know. But it really helps writing clearer code

Collapse
 
johannesvollmer profile image
Johannes Vollmer

I agree for languages where named arguments are not available.

Collapse
 
bgadrian profile image
Adrian B.G.

In Go we don't have problems like these, we're Zen ๐Ÿ˜Ž.

Collapse
 
johannesvollmer profile image
Johannes Vollmer

I'll not that familiar with go - could you please explain how go solved that syntax problem? :)

Collapse
 
bgadrian profile image
Adrian B.G.

Sure, the core dev team, who made the language, also made tools for static analysis,linter, code formatting and incorporated some of the rules in the compiler too, this you get these advantages:
blog.golang.org/go-fmt-your-code

easier to write: never worry about minor formatting concerns while hacking away,
easier to read: when all code looks the same you need not mentally convert others' formatting style into something you can understand.
easier to maintain: mechanical changes to the source don't cause unrelated changes to the file's formatting; diffs show only the real changes.
uncontroversial: never have a debate about spacing or brace position ever again!

A Go "proverb" is "Gofmt's style is no one's favorite, yet gofmt is everyone's favorite", meaining each dev will not like something about the Go standard way, but overall you like it's benefits, because everyone obeys it.

Thread Thread
 
johannesvollmer profile image
Johannes Vollmer

Cool. But how does it indent function calls with many parameters? :D

The rust language has a similar thing, but unfortunately, it will indent many arguments far to the right.

Thread Thread
 
bgadrian profile image
Adrian B.G.

That's the beauty, it doesn't matter and we do not care. We solve problems, we are engineers, coding is just a 20% of our jobs and we shouldn't focus on things that the IDE should do for us.

Unless someone makes a PR to the official Go tool and convince the community that is better his way.

Collapse
 
antero_nu profile image
Antero Karki

I tried closing braces at the end of the line, 1st alternative syntax, made the code look more "pythonic". Soon gave it up though because of the inconvenience of always having to fix code when removing or adding lines, and removing lines with braces wasn't just a mark line then push backspace anymore if you still needed the braces.

Collapse
 
timsev profile image
Tim Severien

Thankfully, I've rarely seen the parenthesis indentation you consider common, so hopefully I won't have to share this article too often ;)

Collapse
 
johannesvollmer profile image
Johannes Vollmer

That's great :)