這是一個或許對你有用的社群
《專案實戰(影片)》:從書中學,往事上“練” 《網際網路高頻面試題》:面朝簡歷學習,春暖花開 《架構 x 系統設計》:摧枯拉朽,掌控面試高頻場景題 《精進 Java 學習指南》:系統學習,網際網路主流技術棧 《必讀 Java 原始碼專欄》:知其然,知其所以然
這是一個或許對你有用的開源專案
國產 Star 破 10w+ 的開源專案,前端包括管理後臺 + 微信小程式,後端支援單體和微服務架構。功能涵蓋 RBAC 許可權、SaaS 多租戶、資料許可權、商城、支付、工作流、大屏報表、微信公眾號、ERP、CRM、AI 大模型等等功能:
Boot 多模組架構:https://gitee.com/zhijiantianya/ruoyi-vue-pro Cloud 微服務架構:https://gitee.com/zhijiantianya/yudao-cloud 影片教程:https://doc.iocoder.cn 【國內首批】支援 JDK 17/21 + SpringBoot 3.3、JDK 8/11 + Spring Boot 2.7 雙版本

-
使用多型代替“if/else”和“switch”; -
程式碼不應該知道使用物件的內部結構; -
嚴格控制函式的規模; -
函式應該只做一件事; -
“DRY”(Don’t Repeat Yourself):不要重複自己。
那些年我們見過的“整潔”程式碼
/* ========================================================================
LISTING 22
======================================================================== */
classshape_base
{
public
:
shape_base() {}
virtual f32 Area()
=
0
;
};
classsquare
:
publicshape_base
{
public
:
square(f32 SideInit) : Side(SideInit) {}
virtual f32 Area()
{
return
Side*Side;}
private
:
f32 Side;
};
classrectangle
:
publicshape_base
{
public
:
rectangle(f32 WidthInit, f32 HeightInit) : Width(WidthInit), Height(HeightInit) {}
virtual f32 Area()
{
return
Width*Height;}
private
:
f32 Width, Height;
};
classtriangle
:
publicshape_base
{
public
:
triangle(f32 BaseInit, f32 HeightInit) : Base(BaseInit), Height(HeightInit) {}
virtual f32 Area()
{
return0.5f
*Base*Height;}
private
:
f32 Base, Height;
};
classcircle
:
publicshape_base
{
public
:
circle(f32 RadiusInit) : Radius(RadiusInit) {}
virtual f32 Area()
{
return
Pi32*Radius*Radius;}
private
:
f32 Radius;
};
/* ========================================================================
LISTING 23
======================================================================== */
f32
TotalAreaVTBL(u32 ShapeCount, shape_base **Shapes)
{
f32 Accum =
0.0f
;
for
(u32 ShapeIndex =
0
; ShapeIndex < ShapeCount; ++ShapeIndex)
{
Accum += Shapes[ShapeIndex]->Area();
}
return
Accum;
}
/* ========================================================================
LISTING 24
======================================================================== */
f32
TotalAreaVTBL4(u32 ShapeCount, shape_base **Shapes)
{
f32 Accum0 =
0.0f
;
f32 Accum1 =
0.0f
;
f32 Accum2 =
0.0f
;
f32 Accum3 =
0.0f
;
u32 Count = ShapeCount/
4
;
while
(Count--)
{
Accum0 += Shapes[
0
]->Area();
Accum1 += Shapes[
1
]->Area();
Accum2 += Shapes[
2
]->Area();
Accum3 += Shapes[
3
]->Area();
Shapes +=
4
;
}
f32 Result = (Accum0 + Accum1 + Accum2 + Accum3);
return
Result;
}

基於 Spring Boot + MyBatis Plus + Vue & Element 實現的後臺管理系統 + 使用者小程式,支援 RBAC 動態許可權、多租戶、資料許可權、工作流、三方登入、支付、簡訊、商城等功能
專案地址:https://github.com/YunaiV/ruoyi-vue-pro 影片教程:https://doc.iocoder.cn/video/
違反“程式碼整潔之道”的第一條規則後
/* ========================================================================
LISTING 25
======================================================================== */
enum
shape_type : u32
{
Shape_Square,
Shape_Rectangle,
Shape_Triangle,
Shape_Circle,
Shape_Count,
};
struct shape_union
{
shape_type Type;
f32 Width;
f32 Height;
};
f32
GetAreaSwitch(shape_union Shape)
{
f32 Result =
0.0f
;
switch
(Shape.Type)
{
case
Shape_Square: {Result = Shape.Width*Shape.Width;}
break
;
case
Shape_Rectangle: {Result = Shape.Width*Shape.Height;}
break
;
case
Shape_Triangle: {Result =
0.5f
*Shape.Width*Shape.Height;}
break
;
case
Shape_Circle: {Result = Pi32*Shape.Width*Shape.Width;}
break
;
case
Shape_Count: {}
break
;
}
return
Result;
}
/* ========================================================================
LISTING 26
======================================================================== */
f32
TotalAreaSwitch(u32 ShapeCount, shape_union *Shapes)
{
f32 Accum =
0.0f
;
for
(u32 ShapeIndex =
0
; ShapeIndex < ShapeCount; ++ShapeIndex)
{
Accum += GetAreaSwitch(Shapes[ShapeIndex]);
}
return
Accum;
}
f32
TotalAreaSwitch4(u32 ShapeCount, shape_union *Shapes)
{
f32 Accum0 =
0.0f
;
f32 Accum1 =
0.0f
;
f32 Accum2 =
0.0f
;
f32 Accum3 =
0.0f
;
ShapeCount /=
4
;
while
(ShapeCount--)
{
Accum0 += GetAreaSwitch(Shapes[
0
]);
Accum1 += GetAreaSwitch(Shapes[
1
]);
Accum2 += GetAreaSwitch(Shapes[
2
]);
Accum3 += GetAreaSwitch(Shapes[
3
]);
Shapes +=
4
;
}
f32 Result = (Accum0 + Accum1 + Accum2 + Accum3);
return
Result;
}

基於 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實現的後臺管理系統 + 使用者小程式,支援 RBAC 動態許可權、多租戶、資料許可權、工作流、三方登入、支付、簡訊、商城等功能
專案地址:https://github.com/YunaiV/yudao-cloud 影片教程:https://doc.iocoder.cn/video/
違反“程式碼整潔之道”的更多條規則後
case
Shape_Square: {Result = Shape.Width*Shape.Width;}
break
;
case
Shape_Rectangle: {Result = Shape.Width*Shape.Height;}
break
;
case
Shape_Triangle: {Result =
0.5f
*Shape.Width*Shape.Height;}
break
;
case
Shape_Circle: {Result = Pi32*Shape.Width*Shape.Width;}
break
;
/* ========================================================================
LISTING 27
======================================================================== */
f32
const
CTable[Shape_Count] = {
1.0f
,
1.0f
,
0.5f
, Pi32};
f32
GetAreaUnion(shape_union Shape)
{
f32 Result = CTable[Shape.Type]*Shape.Width*Shape.Height;
return
Result;
}

/* ========================================================================
LISTING 32
======================================================================== */
classshape_base
{
public
:
shape_base() {}
virtual f32 Area()
=
0
;
virtual u32 CornerCount()
=
0
;
};
classsquare
:
publicshape_base
{
public
:
square(f32 SideInit) : Side(SideInit) {}
virtual f32 Area()
{
return
Side*Side;}
virtual u32 CornerCount()
{
return4
;}
private
:
f32 Side;
};
classrectangle
:
publicshape_base
{
public
:
rectangle(f32 WidthInit, f32 HeightInit) : Width(WidthInit), Height(HeightInit) {}
virtual f32 Area()
{
return
Width*Height;}
virtual u32 CornerCount()
{
return4
;}
private
:
f32 Width, Height;
};
classtriangle
:
publicshape_base
{
public
:
triangle(f32 BaseInit, f32 HeightInit) : Base(BaseInit), Height(HeightInit) {}
virtual f32 Area()
{
return0.5f
*Base*Height;}
virtual u32 CornerCount()
{
return3
;}
private
:
f32 Base, Height;
};
classcircle
:
publicshape_base
{
public
:
circle(f32 RadiusInit) : Radius(RadiusInit) {}
virtual f32 Area()
{
return
Pi32*Radius*Radius;}
virtual u32 CornerCount()
{
return0
;}
private
:
f32 Radius;
};
f32
CornerAreaVTBL(u32 ShapeCount, shape_base **Shapes)
{
f32 Accum =
0.0f
;
for
(u32 ShapeIndex =
0
; ShapeIndex < ShapeCount; ++ShapeIndex)
{
Accum += (
1.0f
/ (
1.0f
+ (f32)Shapes[ShapeIndex]->CornerCount())) * Shapes[ShapeIndex]->Area();
}
return
Accum;
}
f32
CornerAreaVTBL4(u32 ShapeCount, shape_base **Shapes)
{
f32 Accum0 =
0.0f
;
f32 Accum1 =
0.0f
;
f32 Accum2 =
0.0f
;
f32 Accum3 =
0.0f
;
u32 Count = ShapeCount/
4
;
while
(Count--)
{
Accum0 += (
1.0f
/ (
1.0f
+ (f32)Shapes[
0
]->CornerCount())) * Shapes[
0
]->Area();
Accum1 += (
1.0f
/ (
1.0f
+ (f32)Shapes[
1
]->CornerCount())) * Shapes[
1
]->Area();
Accum2 += (
1.0f
/ (
1.0f
+ (f32)Shapes[
2
]->CornerCount())) * Shapes[
2
]->Area();
Accum3 += (
1.0f
/ (
1.0f
+ (f32)Shapes[
3
]->CornerCount())) * Shapes[
3
]->Area();
Shapes +=
4
;
}
f32 Result = (Accum0 + Accum1 + Accum2 + Accum3);
return
Result;
}
/* ========================================================================
LISTING 34
======================================================================== */
u32
GetCornerCountSwitch(shape_type Type)
{
u32 Result =
0
;
switch
(Type)
{
case
Shape_Square: {Result =
4
;}
break
;
case
Shape_Rectangle: {Result =
4
;}
break
;
case
Shape_Triangle: {Result =
3
;}
break
;
case
Shape_Circle: {Result =
0
;}
break
;
case
Shape_Count: {}
break
;
}
return
Result;
}
/* ========================================================================
LISTING 35
======================================================================== */
f32
CornerAreaSwitch(u32 ShapeCount, shape_union *Shapes)
{
f32 Accum =
0.0f
;
for
(u32 ShapeIndex =
0
; ShapeIndex < ShapeCount; ++ShapeIndex)
{
Accum += (
1.0f
/ (
1.0f
+ (f32)GetCornerCountSwitch(Shapes[ShapeIndex].Type))) * GetAreaSwitch(Shapes[ShapeIndex]);
}
return
Accum;
}
f32
CornerAreaSwitch4(u32 ShapeCount, shape_union *Shapes)
{
f32 Accum0 =
0.0f
;
f32 Accum1 =
0.0f
;
f32 Accum2 =
0.0f
;
f32 Accum3 =
0.0f
;
ShapeCount /=
4
;
while
(ShapeCount--)
{
Accum0 += (
1.0f
/ (
1.0f
+ (f32)GetCornerCountSwitch(Shapes[
0
].Type))) * GetAreaSwitch(Shapes[
0
]);
Accum1 += (
1.0f
/ (
1.0f
+ (f32)GetCornerCountSwitch(Shapes[
1
].Type))) * GetAreaSwitch(Shapes[
1
]);
Accum2 += (
1.0f
/ (
1.0f
+ (f32)GetCornerCountSwitch(Shapes[
2
].Type))) * GetAreaSwitch(Shapes[
2
]);
Accum3 += (
1.0f
/ (
1.0f
+ (f32)GetCornerCountSwitch(Shapes[
3
].Type))) * GetAreaSwitch(Shapes[
3
]);
Shapes +=
4
;
}
f32 Result = (Accum0 + Accum1 + Accum2 + Accum3);
return
Result;
}
/* ========================================================================
LISTING 36
======================================================================== */
f32
const
CTable[Shape_Count] = {
1.0f
/ (
1.0f
+
4.0f
),
1.0f
/ (
1.0f
+
4.0f
),
0.5f
/ (
1.0f
+
3.0f
), Pi32};
f32
GetCornerAreaUnion(shape_union Shape)
{
f32 Result = CTable[Shape.Type]*Shape.Width*Shape.Height;
return
Result;
}


“遵循整潔程式碼的規則,你的程式碼執行速度會降低15倍。”
“整潔”的程式碼和效能可否兼得?
網友 2:





