Skip to content

Coding Style #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 25, 2022
40 changes: 22 additions & 18 deletions 1-js/03-code-quality/02-coding-style/1-style-errors/solution.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@

You could note the following:
Man kann Folgendes anmerken:

```js no-beautify
function pow(x,n) // <- no space between arguments
{ // <- figure bracket on a separate line
let result=1; // <- no spaces before or after =
for(let i=0;i<n;i++) {result*=x;} // <- no spaces
// the contents of { ... } should be on a new line
function pow(x,n) // <- kein Leerzeichen zwischen Parametern
{ // <- Klammer auf in eine zweite Zeile
let result=1; // <- keine Leerzeichen vor und nach =
for(let i=0;i<n;i++) {result*=x;} // <- keine Leerzeichen
// the contents of { ... } sollte in eine neue Zeile sein
return result;
}

let x=prompt("x?",''), n=prompt("n?",'') // <-- technically possible,
// but better make it 2 lines, also there's no spaces and missing ;
if (n<=0) // <- no spaces inside (n <= 0), and should be extra line above it
{ // <- figure bracket on a separate line
// below - long lines can be split into multiple lines for improved readability
alert(`Power ${n} is not supported, please enter an integer number greater than zero`);

let x=prompt("x?",''), n=prompt("n?",'') // <-- theoretisch möglich,
// aber besser wenn man es auf zwei Zeilen aufteilt. Es fehlen auch die Leerzeichen und das Semikolon ;
if (n<0) // <- keine Leerzeichen in den Klammern (n < 0), davor sollte auch eine leere Zeile sein
{ // <- Klammer auf in eine neue Zeile
// unten - lange Zeilen können aufgeteilt werden um die Lesbarkeit zu verbessern
alert(`Exponent ${n} wird nicht unterstützt, bitte geben Sie einen Integerwert ein, der größer ist als null`);

}
else // <- could write it on a single line like "} else {"
else // <- man könnte es in einer einzigen Zeile schreiben "} else {"
{
alert(pow(x,n)) // no spaces and missing ;
alert(pow(x,n)) // keine Leerzeichen und kein ;
}
```

The fixed variant:
Die Verbesserte Variante:

```js
function pow(x, n) {
Expand All @@ -39,9 +41,11 @@ function pow(x, n) {
let x = prompt("x?", "");
let n = prompt("n?", "");

if (n <= 0) {
alert(`Power ${n} is not supported,
please enter an integer number greater than zero`);

if (n < 0) {
alert(`Exponent ${n} wird nicht unterstützt,
bitte geben Sie einen Integerwert ein, der größer ist als null`);

} else {
alert( pow(x, n) );
}
Expand Down
8 changes: 4 additions & 4 deletions 1-js/03-code-quality/02-coding-style/1-style-errors/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 4

---

# Bad style
# Schlechter Stil

What's wrong with the code style below?
Was ist an dem Codestil unten falsch?

```js no-beautify
function pow(x,n)
Expand All @@ -17,12 +17,12 @@ function pow(x,n)
let x=prompt("x?",''), n=prompt("n?",'')
if (n<=0)
{
alert(`Power ${n} is not supported, please enter an integer number greater than zero`);
alert(`Exponent ${n} wird nicht unterstützt, bitte geben Sie einen Integerwert ein, der größer als 0 ist`);
}
else
{
alert(pow(x,n))
}
```

Fix it.
Verbessere ihn.
Loading