📜 ⬆️ ⬇️

Code style, brevity and competitive advantage

In the new work (C ++), we had to change the usual style of the code, opening angle brackets {it was necessary to put it on the same line as the beginning of the if / for etc. blocks. At first it was unpleasant to give up old habits, but over the week I liked the new approach. Then I thought - what if there are other aspects of the style that need to be changed despite the fact that they still seem unusual? And despite the fact that most inertia programmers do not use them. It turned out that it is. For a couple of months I reworked the style a lot, the results below.

Important note
To change every aspect of the style is better to give at least a week to addiction. The brain is easier to relearn, if the changes are small, it is easy to find analogies with the old code. And if all changes are applied at once, the code will come out so unusual that the brain will overload and there will be aversion from too abrupt change.

Justification
It's not about "what's more beautiful." The brain will equally well get used to the style described below, but if the code is shorter, then one screen fits more. It is necessary to scroll the code less often - and this means that it is less likely to transfer the hand from the keyboard to the mouse, or to drive back and forth with the cursor. It is not necessary to switch activities to the “scroll” subroutine (how much to scroll is enough or should be even?), Even if it seems imperceptible, and takes only a second or two for each call. (Programmers should be horrified - a whole second for each challenge! How will you compete with robots?). Less have to print. A shorter program compiles faster (especially if you manage to take up one smaller disk cluster), is easier to see in version control systems, and so on.

Vertical dimensions
')
New function and indents
Between different functions usually put an empty line. But for the brain this is potentially redundant information: before the new function comes a line with a closing angle bracket, it can be considered as empty. And this closing bracket goes without extra indents. The new block (function) also goes without unnecessary indentation. In sum, this is enough for the brain to perfectly navigate after retraining without a blank line.
//++ 
	return 1;
}

int f() {
	return 0;
}

//++   
	return 1;
}
int f() {
	return 0;
}

//Python 
	return 1

def f():
	return 0

//Python   
	return 1
def f():
	return 0

— , . , - — , } ( — ). . , !
: \}\n\n -> \}\n.
. , . , — !



/* 

*/
/*  
 */

, . ( */) , . , , , */ . , , . , . //.
, .

Include guard
++ pragma once, include guard ifndef/define/endif . , 2-4 ( ), ( guard). , pragma once, , . , ( ) pragma once, . , // # pragma once.
, . :

\#pragma once\n\n -> \#pragma once\n
\n\n\#pragma once -> \n\#pragma once



— . . ( ) .
// 
int f()
{
	return 0;
}

//    ...
int f() {

	return 0;
}

//
int f() {
	return 0;
}


, : ? ? if ? - ? ? ?
: {\n\n -> {\n
, .

#include
#include ( ) , ( pragma once). include forward declarations, — , ( java-, include forward declarations). Forward declarations #include #. include/forward declarations — forward declarations, , , .


, . , , .

//file.cpp by AuthorName
//This file is licensed under GPL, www.gnu.org/copyleft/gpl.html


.


, public/private , — .
//
void f() {
	int ret = 0;
LabelBegin:
	...
	goto LabelBegin;
};
//
class A {
public:
	A();
	~A() {}
};
//
class A {
	public:
		A();
		~A() {}
};

? — , (« ?») — «public:» , (« ? ...»). ? , goto: «public:» goto, , . , , . - ( ). , 320*240, , goto.
: , ? , . . .


. , — « - ? ».


:
VeryLongClassName* veryLongClassName = new VeryLongClassName(params);

:
auto veryLongClassName = new VeryLongClassName(params);

:
New<VeryLongClassName> shortName(params);

, New ( ).
New.h

template<class Class>
class New {
	private:
		Class*d;
	public:
		New() {
			d = new Class();
		}
		template<typename T0>
		New(T0 t0) {
			d = new Class(t0);
		}
		template<typename T0, typename T1>
		New(T0 t0, T1 t1) {
			d = new Class(t0, t1);
		}
		template<typename T0, typename T1, typename T2>
		New(T0 t0, T1 t1, T2 t2) {
			d = new Class(t0, t1, t2);
		}
		template<typename T0, typename T1, typename T2, typename T3>
		New(T0 t0, T1 t1, T2 t2, T3 t3) {
			d = new Class(t0, t1, t2, t3);
		}
		virtual ~New() {}
		Class* operator->() { return d; }
		const Class* operator->()const { return d; }
		operator Class* () { return d; }
		operator const Class* ()const { return d; }
};

, , , . — ++11 VS2012.


Java/Qt: ClassName, functionName. : function_name — , , , ( ), . « » ( ) , ( ).

Tabs
, ! habrahabr.ru/post/118208


, :
//
Ctor::Ctor(): var1(1)
	, var2(2)
	, var3(3)
...
};
//
Ctor::Ctor(): var1(1),
	var2(2),
	var3(3)
...
};

, . ? , 1) — , , , , 2) , . 1 — , , — . : , , , , . , , — . : « , ! , ! ! , — , ? — ! , — ? , — ! , , ? , ! , !». , .


- , ( ). ++ — , ( linq C#). , !

Source: https://habr.com/ru/post/149210/


All Articles